From da2f794962f11bb81849cbb6ab8810ded5629514 Mon Sep 17 00:00:00 2001 From: Philip Cox Date: Wed, 28 Feb 2024 17:39:55 +1300 Subject: [PATCH] fix(css): Call `swc_common::GLOBALS.set` in css minify (#44) Currently errors generate in the minification process get obscured because of error in the error handling because of a missing global. I've added a call to `swc_common::GLOBALS.set(&swc_common::Globals::new(), || {` in `minify_inner` of `css_node/src/lib.rs` in the same way it's set in `html_node/src/lib.rs` I'm not sure, but suspect `linter_node` will need a similar change. --- crates/css_node/src/lib.rs | 166 +++++++++++++++++++------------------ 1 file changed, 84 insertions(+), 82 deletions(-) diff --git a/crates/css_node/src/lib.rs b/crates/css_node/src/lib.rs index 9413a1c..bbbc92e 100644 --- a/crates/css_node/src/lib.rs +++ b/crates/css_node/src/lib.rs @@ -245,103 +245,105 @@ impl Task for MinifyTask { } fn minify_inner(code: &str, opts: MinifyOptions) -> anyhow::Result { - try_with(|cm, handler| { - let filename = match opts.filename { - Some(v) => FileName::Real(v.into()), - None => FileName::Anon, - }; - - let fm = cm.new_source_file(filename, code.into()); + swc_common::GLOBALS.set(&swc_common::Globals::new(), || { + try_with(|cm, handler| { + let filename = match opts.filename { + Some(v) => FileName::Real(v.into()), + None => FileName::Anon, + }; + + let fm = cm.new_source_file(filename, code.into()); + + let mut errors = vec![]; + let ss = swc_css_parser::parse_file::( + &fm, + None, + swc_css_parser::parser::ParserConfig { + allow_wrong_line_comments: false, + css_modules: false, + legacy_nesting: false, + legacy_ie: false, + }, + &mut errors, + ); - let mut errors = vec![]; - let ss = swc_css_parser::parse_file::( - &fm, - None, - swc_css_parser::parser::ParserConfig { - allow_wrong_line_comments: false, - css_modules: false, - legacy_nesting: false, - legacy_ie: false, - }, - &mut errors, - ); + let mut ss = match ss { + Ok(v) => v, + Err(err) => { + err.to_diagnostics(handler).emit(); - let mut ss = match ss { - Ok(v) => v, - Err(err) => { - err.to_diagnostics(handler).emit(); + for err in errors { + err.to_diagnostics(handler).emit(); + } - for err in errors { - err.to_diagnostics(handler).emit(); + bail!("failed to parse input as stylesheet") } + }; - bail!("failed to parse input as stylesheet") - } - }; + let mut returned_errors = None; - let mut returned_errors = None; + if !errors.is_empty() { + returned_errors = Some(Vec::with_capacity(errors.len())); - if !errors.is_empty() { - returned_errors = Some(Vec::with_capacity(errors.len())); - - for err in errors { - let mut buf = vec![]; + for err in errors { + let mut buf = vec![]; - err.to_diagnostics(handler).buffer(&mut buf); + err.to_diagnostics(handler).buffer(&mut buf); - for i in buf { - returned_errors.as_mut().unwrap().push(Diagnostic { - level: i.level.to_string(), - message: i.message(), - span: serde_json::to_value(&i.span)?, - }); + for i in buf { + returned_errors.as_mut().unwrap().push(Diagnostic { + level: i.level.to_string(), + message: i.message(), + span: serde_json::to_value(&i.span)?, + }); + } } } - } - - swc_css_minifier::minify(&mut ss, Default::default()); - - let mut src_map = vec![]; - let code = { - let mut buf = String::new(); - { - let wr = BasicCssWriter::new( - &mut buf, - if opts.source_map { - Some(&mut src_map) - } else { - None - }, - BasicCssWriterConfig { - indent_type: IndentType::Space, - indent_width: 0, - linefeed: LineFeed::LF, - }, - ); - let mut gen = CodeGenerator::new(wr, CodegenConfig { minify: true }); - gen.emit(&ss).context("failed to emit")?; - } + swc_css_minifier::minify(&mut ss, Default::default()); + + let mut src_map = vec![]; + let code = { + let mut buf = String::new(); + { + let wr = BasicCssWriter::new( + &mut buf, + if opts.source_map { + Some(&mut src_map) + } else { + None + }, + BasicCssWriterConfig { + indent_type: IndentType::Space, + indent_width: 0, + linefeed: LineFeed::LF, + }, + ); + let mut gen = CodeGenerator::new(wr, CodegenConfig { minify: true }); - buf - }; + gen.emit(&ss).context("failed to emit")?; + } - let map = if opts.source_map { - let map = cm.build_source_map(&src_map); - let mut buf = vec![]; - map.to_writer(&mut buf) - .context("failed to generate sourcemap")?; - Some(String::from_utf8(buf).context("the generated source map is not utf8")?) - } else { - None - }; + buf + }; - Ok(TransformOutput { - code, - map, - errors: returned_errors, - deps: Default::default(), - modules_mapping: Default::default(), + let map = if opts.source_map { + let map = cm.build_source_map(&src_map); + let mut buf = vec![]; + map.to_writer(&mut buf) + .context("failed to generate sourcemap")?; + Some(String::from_utf8(buf).context("the generated source map is not utf8")?) + } else { + None + }; + + Ok(TransformOutput { + code, + map, + errors: returned_errors, + deps: Default::default(), + modules_mapping: Default::default(), + }) }) }) }