Skip to content

Commit 815a183

Browse files
committed
Add filename option for transformStyleAttribute
1 parent adabfc1 commit 815a183

File tree

3 files changed

+8
-0
lines changed

3 files changed

+8
-0
lines changed

node/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ export interface ErrorLocation extends Location {
202202
export declare function transform(options: TransformOptions): TransformResult;
203203

204204
export interface TransformAttributeOptions {
205+
/** The filename in which the style attribute appeared. Used for error messages and dependencies. */
206+
filename?: string,
205207
/** The source code to transform. */
206208
code: Buffer,
207209
/** Whether to enable minification. */

node/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,7 @@ fn compile_bundle<'i, P: SourceProvider>(
737737
#[derive(Debug, Deserialize)]
738738
#[serde(rename_all = "camelCase")]
739739
struct AttrConfig {
740+
pub filename: Option<String>,
740741
#[serde(with = "serde_bytes")]
741742
pub code: Vec<u8>,
742743
pub targets: Option<Browsers>,
@@ -780,9 +781,11 @@ fn compile_attr<'i>(
780781
None
781782
};
782783
let res = {
784+
let filename = config.filename.clone().unwrap_or_default();
783785
let mut attr = StyleAttribute::parse(
784786
&code,
785787
ParserOptions {
788+
filename,
786789
error_recovery: config.error_recovery,
787790
warnings: warnings.clone(),
788791
..ParserOptions::default()

src/stylesheet.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ impl<'i, 'o> StyleSheet<'i, 'o> {
265265
pub struct StyleAttribute<'i> {
266266
/// The declarations in the style attribute.
267267
pub declarations: DeclarationBlock<'i>,
268+
sources: Vec<String>,
268269
}
269270

270271
impl<'i> StyleAttribute<'i> {
@@ -277,6 +278,7 @@ impl<'i> StyleAttribute<'i> {
277278
let mut parser = Parser::new(&mut input);
278279
Ok(StyleAttribute {
279280
declarations: DeclarationBlock::parse(&mut parser, &options).map_err(|e| Error::from(e, "".into()))?,
281+
sources: vec![options.filename],
280282
})
281283
}
282284

@@ -299,6 +301,7 @@ impl<'i> StyleAttribute<'i> {
299301
// Make sure we always have capacity > 0: https://github.com/napi-rs/napi-rs/issues/1124.
300302
let mut dest = String::with_capacity(1);
301303
let mut printer = Printer::new(&mut dest, options);
304+
printer.sources = Some(&self.sources);
302305

303306
self.declarations.to_css(&mut printer)?;
304307

0 commit comments

Comments
 (0)