chore(es/minifier): Fix size calculator script (#5767)

This commit is contained in:
Donny/강동윤 2022-09-06 22:23:57 +09:00 committed by GitHub
parent e654741336
commit 9d29abcd5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 41 deletions

View File

@ -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<N: swc_ecma_codegen::Node>(cm: Lrc<SourceMap>, 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 {

View File

@ -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
for f in ./benches/full/*.js; do cargo run --release --features concurrent --example minifier -- $f && gzip -9 -c output.js | wc -c; done