diff --git a/crates/swc_ecma_minifier/examples/minifier.rs b/crates/swc_ecma_minifier/examples/minifier.rs index 4307fbb310f..198bd891030 100644 --- a/crates/swc_ecma_minifier/examples/minifier.rs +++ b/crates/swc_ecma_minifier/examples/minifier.rs @@ -4,8 +4,8 @@ extern crate swc_node_base; use std::{env::args, fs, path::Path}; -use swc_common::{sync::Lrc, Mark, SourceMap}; -use swc_ecma_codegen::text_writer::JsWriter; +use swc_common::{errors::HANDLER, sync::Lrc, Mark, SourceMap}; +use swc_ecma_codegen::text_writer::{omit_trailing_semi, JsWriter}; use swc_ecma_minifier::{ optimize, option::{ExtraOptions, MangleOptions, MinifyOptions}, @@ -20,51 +20,53 @@ fn main() { eprintln!("File: {}", file); testing::run_test2(false, |cm, handler| { - let fm = cm.load_file(Path::new(&file)).expect("failed to load file"); + HANDLER.set(&handler, || { + let fm = cm.load_file(Path::new(&file)).expect("failed to load file"); - let unresolved_mark = Mark::new(); - let top_level_mark = Mark::new(); + let unresolved_mark = Mark::new(); + let top_level_mark = Mark::new(); - let program = parse_file_as_module( - &fm, - Default::default(), - Default::default(), - None, - &mut vec![], - ) - .map_err(|err| { - err.into_diagnostic(&handler).emit(); - }) - .map(|module| module.fold_with(&mut resolver(unresolved_mark, top_level_mark, false))) - .unwrap(); + let program = parse_file_as_module( + &fm, + Default::default(), + Default::default(), + None, + &mut vec![], + ) + .map_err(|err| { + err.into_diagnostic(&handler).emit(); + }) + .map(|module| module.fold_with(&mut resolver(unresolved_mark, top_level_mark, false))) + .unwrap(); - let output = optimize( - program.into(), - cm.clone(), - None, - None, - &MinifyOptions { - compress: Some(Default::default()), - mangle: Some(MangleOptions { - top_level: true, + let output = optimize( + program.into(), + cm.clone(), + None, + None, + &MinifyOptions { + compress: Some(Default::default()), + mangle: Some(MangleOptions { + top_level: true, + ..Default::default() + }), ..Default::default() - }), - ..Default::default() - }, - &ExtraOptions { - unresolved_mark, - top_level_mark, - }, - ) - .expect_module(); + }, + &ExtraOptions { + unresolved_mark, + top_level_mark, + }, + ) + .expect_module(); - let output = output.fold_with(&mut fixer(None)); + let output = output.fold_with(&mut fixer(None)); - let code = print(cm, &[output], true); + let code = print(cm, &[output], true); - fs::write("output.js", code.as_bytes()).expect("failed to write output"); + fs::write("output.js", code.as_bytes()).expect("failed to write output"); - Ok(()) + Ok(()) + }) }) .unwrap(); } @@ -80,7 +82,7 @@ fn print(cm: Lrc, nodes: &[N], minify: boo }, cm: cm.clone(), comments: None, - wr: Box::new(JsWriter::new(cm, "\n", &mut buf, None)), + wr: omit_trailing_semi(JsWriter::new(cm, "\n", &mut buf, None)), }; for n in nodes { diff --git a/crates/swc_ecma_minifier/scripts/size.sh b/crates/swc_ecma_minifier/scripts/size.sh index 3d7f0e7e727..3c9f88c936e 100755 --- a/crates/swc_ecma_minifier/scripts/size.sh +++ b/crates/swc_ecma_minifier/scripts/size.sh @@ -5,4 +5,4 @@ export UPDATE=1 export DIFF=0 export RUST_LOG=off -for f in ./benches/full/*.js; do RUST_LOG=off cargo run --example minifier -- $f && gzip -c output.js | wc -c; done \ No newline at end of file +for f in ./benches/full/*.js; do cargo run --release --features concurrent --example minifier -- $f && gzip -9 -c output.js | wc -c; done \ No newline at end of file