Skip to content

Commit

Permalink
Add filename option for transformStyleAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Sep 12, 2022
1 parent adabfc1 commit 815a183
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions node/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ export interface ErrorLocation extends Location {
export declare function transform(options: TransformOptions): TransformResult;

export interface TransformAttributeOptions {
/** The filename in which the style attribute appeared. Used for error messages and dependencies. */
filename?: string,
/** The source code to transform. */
code: Buffer,
/** Whether to enable minification. */
Expand Down
3 changes: 3 additions & 0 deletions node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@ fn compile_bundle<'i, P: SourceProvider>(
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct AttrConfig {
pub filename: Option<String>,
#[serde(with = "serde_bytes")]
pub code: Vec<u8>,
pub targets: Option<Browsers>,
Expand Down Expand Up @@ -780,9 +781,11 @@ fn compile_attr<'i>(
None
};
let res = {
let filename = config.filename.clone().unwrap_or_default();
let mut attr = StyleAttribute::parse(
&code,
ParserOptions {
filename,
error_recovery: config.error_recovery,
warnings: warnings.clone(),
..ParserOptions::default()
Expand Down
3 changes: 3 additions & 0 deletions src/stylesheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ impl<'i, 'o> StyleSheet<'i, 'o> {
pub struct StyleAttribute<'i> {
/// The declarations in the style attribute.
pub declarations: DeclarationBlock<'i>,
sources: Vec<String>,
}

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

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

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

Expand Down

0 comments on commit 815a183

Please sign in to comment.