From 4e989807d71aa9a6c019c6a5a01acbbee9ec6020 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sun, 4 Sep 2022 22:18:05 +0900 Subject: [PATCH] chore(es/minifier): Fix example (#5739) --- Cargo.toml | 2 +- .../swc_ecma_minifier/examples/minify-all.rs | 101 ++++++++++-------- 2 files changed, 55 insertions(+), 48 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1374aeb9833..8fb74282a56 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,7 @@ members = [ [profile.bench] debug = true -lto = true +# lto = true # Without this, printing diff consumes more than a minute. diff --git a/crates/swc_ecma_minifier/examples/minify-all.rs b/crates/swc_ecma_minifier/examples/minify-all.rs index 76952b975f1..4dc031e80ba 100644 --- a/crates/swc_ecma_minifier/examples/minify-all.rs +++ b/crates/swc_ecma_minifier/examples/minify-all.rs @@ -6,7 +6,7 @@ use std::{env, fs, path::PathBuf, time::Instant}; use anyhow::Result; use rayon::prelude::*; -use swc_common::{sync::Lrc, Mark, SourceMap, GLOBALS}; +use swc_common::{errors::HANDLER, sync::Lrc, Mark, SourceMap, GLOBALS}; use swc_ecma_codegen::text_writer::JsWriter; use swc_ecma_minifier::{ optimize, @@ -25,62 +25,69 @@ fn main() { let start = Instant::now(); testing::run_test2(false, |cm, handler| { GLOBALS.with(|globals| { - let _ = files - .into_iter() - .map(|path| -> Result<_> { - GLOBALS.set(globals, || { - let fm = cm.load_file(&path).expect("failed to load file"); + HANDLER.set(&handler, || { + let _ = files + .into_iter() + .map(|path| -> Result<_> { + GLOBALS.set(globals, || { + let fm = cm.load_file(&path).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.clone(), &[output], true); + let code = print(cm.clone(), &[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(()) + }) }) - }) - .collect::>(); + .collect::>(); - Ok(()) + Ok(()) + }) }) }) .unwrap();