2022-01-27 11:41:43 +03:00
|
|
|
#![deny(warnings)]
|
|
|
|
|
|
|
|
use std::path::{Path, PathBuf};
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-03-11 12:10:54 +03:00
|
|
|
use swc_common::{errors::Handler, sync::Lrc, FileName, Mark, SourceFile, SourceMap};
|
2021-08-04 10:20:02 +03:00
|
|
|
use swc_ecma_ast::*;
|
|
|
|
use swc_ecma_codegen::{
|
|
|
|
text_writer::{omit_trailing_semi, JsWriter, WriteJs},
|
|
|
|
Emitter,
|
|
|
|
};
|
2021-08-13 13:03:04 +03:00
|
|
|
use swc_ecma_minifier::{
|
|
|
|
optimize,
|
|
|
|
option::{ExtraOptions, MangleOptions, ManglePropertiesOptions, MinifyOptions},
|
2021-08-04 10:20:02 +03:00
|
|
|
};
|
2022-02-26 09:41:14 +03:00
|
|
|
use swc_ecma_parser::parse_file_as_module;
|
2022-04-26 10:38:50 +03:00
|
|
|
use swc_ecma_transforms_base::resolver;
|
2022-01-27 11:41:43 +03:00
|
|
|
use swc_ecma_visit::VisitMutWith;
|
|
|
|
use testing::NormalizedOutput;
|
2021-08-04 10:20:02 +03:00
|
|
|
|
|
|
|
fn print(cm: Lrc<SourceMap>, m: &Module, minify: bool) -> String {
|
|
|
|
let mut buf = vec![];
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut wr = Box::new(JsWriter::new(cm.clone(), "\n", &mut buf, None)) as Box<dyn WriteJs>;
|
|
|
|
|
|
|
|
if minify {
|
|
|
|
wr = Box::new(omit_trailing_semi(wr));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut emitter = Emitter {
|
2022-05-15 08:07:21 +03:00
|
|
|
cfg: swc_ecma_codegen::Config {
|
|
|
|
minify,
|
|
|
|
..Default::default()
|
|
|
|
},
|
2022-01-13 17:06:11 +03:00
|
|
|
cm,
|
2021-08-04 10:20:02 +03:00
|
|
|
comments: None,
|
|
|
|
wr,
|
|
|
|
};
|
|
|
|
|
|
|
|
emitter.emit_module(m).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
String::from_utf8(buf).unwrap()
|
|
|
|
}
|
|
|
|
|
2022-03-11 12:10:54 +03:00
|
|
|
fn parse(handler: &Handler, cm: Lrc<SourceMap>, path: &Path) -> Result<Module, ()> {
|
2021-08-04 10:20:02 +03:00
|
|
|
let fm = cm.load_file(path).unwrap();
|
2022-03-11 12:10:54 +03:00
|
|
|
parse_fm(handler, fm)
|
2021-08-04 10:20:02 +03:00
|
|
|
}
|
|
|
|
|
2022-03-11 12:10:54 +03:00
|
|
|
fn parse_fm(handler: &Handler, fm: Lrc<SourceFile>) -> Result<Module, ()> {
|
2022-02-26 09:41:14 +03:00
|
|
|
parse_file_as_module(
|
|
|
|
&fm,
|
2021-08-08 16:53:41 +03:00
|
|
|
Default::default(),
|
2021-08-04 10:20:02 +03:00
|
|
|
EsVersion::latest(),
|
|
|
|
None,
|
2022-02-26 09:41:14 +03:00
|
|
|
&mut vec![],
|
|
|
|
)
|
2022-03-11 12:10:54 +03:00
|
|
|
.map_err(|err| {
|
|
|
|
err.into_diagnostic(handler).emit();
|
|
|
|
})
|
2021-08-04 10:20:02 +03:00
|
|
|
}
|
|
|
|
|
2022-04-20 09:52:03 +03:00
|
|
|
#[testing::fixture("tests/fixture/**/output.js")]
|
2022-05-27 10:32:23 +03:00
|
|
|
#[testing::fixture("tests/terser/**/output.js")]
|
2021-08-04 10:20:02 +03:00
|
|
|
fn compressed(compressed_file: PathBuf) {
|
2022-05-27 10:32:23 +03:00
|
|
|
let _ = testing::run_test2(false, |cm, handler| {
|
2022-04-26 10:38:50 +03:00
|
|
|
let mut m = parse(&handler, cm.clone(), &compressed_file)?;
|
2021-08-04 10:20:02 +03:00
|
|
|
|
2022-04-26 10:38:50 +03:00
|
|
|
let unresolved_mark = Mark::new();
|
|
|
|
let top_level_mark = Mark::new();
|
|
|
|
m.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, false));
|
2021-08-04 10:20:02 +03:00
|
|
|
|
|
|
|
let m = optimize(
|
|
|
|
m,
|
|
|
|
cm.clone(),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
&MinifyOptions {
|
|
|
|
mangle: Some(MangleOptions {
|
|
|
|
props: Some(ManglePropertiesOptions {
|
2022-05-09 08:38:27 +03:00
|
|
|
..Default::default()
|
2021-08-04 10:20:02 +03:00
|
|
|
}),
|
|
|
|
top_level: true,
|
|
|
|
keep_class_names: false,
|
|
|
|
keep_fn_names: false,
|
|
|
|
keep_private_props: false,
|
|
|
|
ie8: false,
|
|
|
|
safari10: false,
|
2022-02-10 07:53:47 +03:00
|
|
|
reserved: Default::default(),
|
2021-08-04 10:20:02 +03:00
|
|
|
}),
|
|
|
|
compress: None,
|
|
|
|
..Default::default()
|
|
|
|
},
|
2022-04-26 10:38:50 +03:00
|
|
|
&ExtraOptions {
|
|
|
|
unresolved_mark,
|
|
|
|
top_level_mark,
|
|
|
|
},
|
2021-08-04 10:20:02 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
let mangled = print(cm.clone(), &m, false);
|
|
|
|
let minified = print(cm.clone(), &m, true);
|
|
|
|
|
2022-03-11 12:10:54 +03:00
|
|
|
parse_fm(&handler, cm.new_source_file(FileName::Anon, mangled))?;
|
|
|
|
parse_fm(&handler, cm.new_source_file(FileName::Anon, minified))?;
|
2021-08-04 10:20:02 +03:00
|
|
|
|
|
|
|
Ok(())
|
2022-05-27 10:32:23 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[testing::fixture("tests/fixture/**/input.js")]
|
|
|
|
#[testing::fixture("tests/terser/**/input.js")]
|
|
|
|
fn snapshot_compress_fixture(input: PathBuf) {
|
|
|
|
let _ = testing::run_test2(false, |cm, handler| {
|
|
|
|
let mut m = parse(&handler, cm.clone(), &input)?;
|
|
|
|
|
|
|
|
let unresolved_mark = Mark::new();
|
|
|
|
let top_level_mark = Mark::new();
|
|
|
|
|
|
|
|
m.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, false));
|
|
|
|
|
|
|
|
let m = optimize(
|
|
|
|
m,
|
|
|
|
cm.clone(),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
&MinifyOptions {
|
|
|
|
mangle: Some(MangleOptions {
|
|
|
|
top_level: true,
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
compress: None,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
&ExtraOptions {
|
|
|
|
unresolved_mark,
|
|
|
|
top_level_mark,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
let mangled = print(cm, &m, false);
|
|
|
|
|
|
|
|
NormalizedOutput::from(mangled)
|
|
|
|
.compare_to_file(input.parent().unwrap().join("output.mangleOnly.js"))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
2021-08-04 10:20:02 +03:00
|
|
|
}
|
2021-08-11 10:24:52 +03:00
|
|
|
|
2021-10-11 10:44:42 +03:00
|
|
|
#[testing::fixture("tests/mangle/**/input.js")]
|
|
|
|
fn fixture(input: PathBuf) {
|
2022-03-11 12:10:54 +03:00
|
|
|
testing::run_test2(false, |cm, handler| {
|
|
|
|
let mut m = parse(&handler, cm.clone(), &input)?;
|
2021-10-11 10:44:42 +03:00
|
|
|
|
2022-04-26 10:38:50 +03:00
|
|
|
let unresolved_mark = Mark::new();
|
|
|
|
let top_level_mark = Mark::new();
|
2021-10-11 10:44:42 +03:00
|
|
|
|
2022-04-26 10:38:50 +03:00
|
|
|
m.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, false));
|
2021-10-24 13:52:09 +03:00
|
|
|
|
2021-10-11 10:44:42 +03:00
|
|
|
let m = optimize(
|
|
|
|
m,
|
|
|
|
cm.clone(),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
&MinifyOptions {
|
|
|
|
mangle: Some(MangleOptions {
|
|
|
|
top_level: true,
|
2021-10-24 13:52:09 +03:00
|
|
|
..Default::default()
|
2021-10-11 10:44:42 +03:00
|
|
|
}),
|
|
|
|
compress: None,
|
|
|
|
..Default::default()
|
|
|
|
},
|
2022-04-26 10:38:50 +03:00
|
|
|
&ExtraOptions {
|
|
|
|
unresolved_mark,
|
|
|
|
top_level_mark,
|
|
|
|
},
|
2021-10-11 10:44:42 +03:00
|
|
|
);
|
|
|
|
|
2022-01-13 17:06:11 +03:00
|
|
|
let mangled = print(cm, &m, false);
|
2021-10-11 10:44:42 +03:00
|
|
|
|
|
|
|
NormalizedOutput::from(mangled)
|
|
|
|
.compare_to_file(input.parent().unwrap().join("output.js"))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
}
|
2022-02-10 07:53:47 +03:00
|
|
|
|
|
|
|
fn assert_mangled(src: &str, expected: &str, opts: MangleOptions) {
|
2022-03-11 12:10:54 +03:00
|
|
|
testing::run_test2(false, |cm, handler| {
|
2022-02-10 07:53:47 +03:00
|
|
|
let fm = cm.new_source_file(FileName::Anon, src.into());
|
|
|
|
|
2022-03-11 12:10:54 +03:00
|
|
|
let m = parse_fm(&handler, fm)?;
|
2022-02-10 07:53:47 +03:00
|
|
|
|
2022-04-26 10:38:50 +03:00
|
|
|
let unresolved_mark = Mark::fresh(Mark::root());
|
2022-02-10 07:53:47 +03:00
|
|
|
let top_level_mark = Mark::fresh(Mark::root());
|
|
|
|
|
|
|
|
let m = optimize(
|
|
|
|
m,
|
|
|
|
cm.clone(),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
&MinifyOptions {
|
|
|
|
mangle: Some(opts),
|
|
|
|
compress: None,
|
|
|
|
..Default::default()
|
|
|
|
},
|
2022-04-26 10:38:50 +03:00
|
|
|
&ExtraOptions {
|
|
|
|
unresolved_mark,
|
|
|
|
top_level_mark,
|
|
|
|
},
|
2022-02-10 07:53:47 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
let mangled = print(cm, &m, false);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
NormalizedOutput::from(mangled),
|
|
|
|
NormalizedOutput::from(expected.to_owned())
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reserved_func_names() {
|
|
|
|
let src = "function func1() {
|
|
|
|
console.log(1);
|
|
|
|
}
|
|
|
|
function func2() {
|
|
|
|
console.log(2);
|
|
|
|
}";
|
|
|
|
|
|
|
|
let expected = "function func1() {
|
|
|
|
console.log(1);
|
|
|
|
}
|
|
|
|
function a() {
|
|
|
|
console.log(2);
|
|
|
|
}";
|
|
|
|
|
|
|
|
assert_mangled(
|
|
|
|
src,
|
|
|
|
expected,
|
|
|
|
MangleOptions {
|
|
|
|
top_level: true,
|
|
|
|
reserved: vec!["func1".into()],
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reserved_class_names() {
|
|
|
|
let src = "class Class1 {
|
|
|
|
hello1 = 1;
|
|
|
|
}
|
|
|
|
class Class2 {
|
|
|
|
hello2 = 2;
|
|
|
|
}";
|
|
|
|
|
|
|
|
let expected = "class Class1 {
|
|
|
|
hello1 = 1;
|
|
|
|
}
|
|
|
|
class a {
|
|
|
|
hello2 = 2;
|
|
|
|
}";
|
|
|
|
|
|
|
|
assert_mangled(
|
|
|
|
src,
|
|
|
|
expected,
|
|
|
|
MangleOptions {
|
|
|
|
top_level: true,
|
|
|
|
reserved: vec!["Class1".into()],
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reserved_class_props() {
|
|
|
|
let src = "class Class1 {
|
|
|
|
hello1 = 1;
|
|
|
|
}
|
|
|
|
class Class2 {
|
|
|
|
hello2 = 2;
|
|
|
|
}";
|
|
|
|
|
|
|
|
let expected = "class a {
|
|
|
|
a = 1;
|
|
|
|
}
|
|
|
|
class b {
|
|
|
|
hello2 = 2;
|
|
|
|
}";
|
|
|
|
|
|
|
|
assert_mangled(
|
|
|
|
src,
|
|
|
|
expected,
|
|
|
|
MangleOptions {
|
|
|
|
top_level: true,
|
|
|
|
reserved: vec!["hello1".into()],
|
|
|
|
props: Some(ManglePropertiesOptions {
|
|
|
|
reserved: vec!["hello2".into()],
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reserved_private_props() {
|
|
|
|
let src = "class Class1 {
|
|
|
|
#hello1 = 1;
|
|
|
|
}
|
|
|
|
class Class2 {
|
|
|
|
#hello2 = 2;
|
|
|
|
}";
|
|
|
|
|
|
|
|
let expected = "class a {
|
|
|
|
#a = 1;
|
|
|
|
}
|
|
|
|
class b {
|
|
|
|
#b = 2;
|
|
|
|
}";
|
|
|
|
|
|
|
|
assert_mangled(
|
|
|
|
src,
|
|
|
|
expected,
|
|
|
|
MangleOptions {
|
|
|
|
top_level: true,
|
|
|
|
reserved: vec!["hello1".into()],
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reserved_all_private_props() {
|
|
|
|
let src = "class Class1 {
|
|
|
|
#hello1 = 1;
|
|
|
|
}
|
|
|
|
class Class2 {
|
|
|
|
#hello2 = 2;
|
|
|
|
}";
|
|
|
|
|
|
|
|
let expected = "class a {
|
|
|
|
#hello1 = 1;
|
|
|
|
}
|
|
|
|
class b {
|
|
|
|
#hello2 = 2;
|
|
|
|
}";
|
|
|
|
|
|
|
|
assert_mangled(
|
|
|
|
src,
|
|
|
|
expected,
|
|
|
|
MangleOptions {
|
|
|
|
top_level: true,
|
|
|
|
keep_private_props: true,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|