2021-08-11 10:24:52 +03:00
|
|
|
use ansi_term::Color;
|
|
|
|
use anyhow::{bail, Context, Error};
|
2021-08-13 13:03:04 +03:00
|
|
|
use std::{
|
|
|
|
fs::read_to_string,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
process::Command,
|
|
|
|
};
|
|
|
|
use swc_common::{
|
|
|
|
comments::SingleThreadedComments, errors::Handler, input::SourceFileInput, 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
|
|
|
};
|
2021-08-13 13:03:04 +03:00
|
|
|
use swc_ecma_parser::{lexer::Lexer, Parser};
|
2021-09-08 12:45:39 +03:00
|
|
|
use swc_ecma_transforms::{
|
|
|
|
fixer,
|
|
|
|
hygiene::{self, hygiene_with_config},
|
|
|
|
resolver_with_mark,
|
|
|
|
};
|
2021-10-24 13:52:09 +03:00
|
|
|
use swc_ecma_visit::{FoldWith, VisitMutWith};
|
2021-10-11 10:44:42 +03:00
|
|
|
use testing::{DebugUsingDisplay, 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 {
|
|
|
|
cfg: swc_ecma_codegen::Config { minify },
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse(cm: Lrc<SourceMap>, path: &Path) -> Module {
|
|
|
|
let fm = cm.load_file(path).unwrap();
|
|
|
|
parse_fm(fm)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_fm(fm: Lrc<SourceFile>) -> Module {
|
|
|
|
let lexer = Lexer::new(
|
2021-08-08 16:53:41 +03:00
|
|
|
Default::default(),
|
2021-08-04 10:20:02 +03:00
|
|
|
EsVersion::latest(),
|
|
|
|
SourceFileInput::from(&*fm),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut parser = Parser::new_from(lexer);
|
|
|
|
parser.parse_module().unwrap()
|
|
|
|
}
|
|
|
|
|
2021-08-11 10:24:52 +03:00
|
|
|
fn run(
|
|
|
|
cm: Lrc<SourceMap>,
|
|
|
|
handler: &Handler,
|
|
|
|
input: &Path,
|
|
|
|
mangle: MangleOptions,
|
|
|
|
) -> Option<Module> {
|
|
|
|
let _ = rayon::ThreadPoolBuilder::new()
|
|
|
|
.thread_name(|i| format!("rayon-{}", i + 1))
|
|
|
|
.build_global();
|
|
|
|
|
2022-01-13 17:06:11 +03:00
|
|
|
let fm = cm.load_file(input).expect("failed to load input.js");
|
2021-08-11 10:24:52 +03:00
|
|
|
let comments = SingleThreadedComments::default();
|
|
|
|
|
|
|
|
eprintln!("---- {} -----\n{}", Color::Green.paint("Input"), fm.src);
|
|
|
|
|
|
|
|
let top_level_mark = Mark::fresh(Mark::root());
|
|
|
|
|
|
|
|
let lexer = Lexer::new(
|
|
|
|
Default::default(),
|
|
|
|
Default::default(),
|
|
|
|
SourceFileInput::from(&*fm),
|
|
|
|
Some(&comments),
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut parser = Parser::new_from(lexer);
|
|
|
|
let program = parser
|
|
|
|
.parse_module()
|
|
|
|
.map_err(|err| {
|
2022-01-13 17:06:11 +03:00
|
|
|
err.into_diagnostic(handler).emit();
|
2021-08-11 10:24:52 +03:00
|
|
|
})
|
|
|
|
.map(|module| module.fold_with(&mut resolver_with_mark(top_level_mark)));
|
|
|
|
|
|
|
|
// Ignore parser errors.
|
|
|
|
//
|
|
|
|
// This is typically related to strict mode caused by module context.
|
|
|
|
let program = match program {
|
|
|
|
Ok(v) => v,
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let output = optimize(
|
|
|
|
program,
|
2022-01-13 17:06:11 +03:00
|
|
|
cm,
|
2021-08-11 10:24:52 +03:00
|
|
|
Some(&comments),
|
|
|
|
None,
|
|
|
|
&MinifyOptions {
|
|
|
|
compress: None,
|
|
|
|
mangle: Some(mangle),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
&ExtraOptions { top_level_mark },
|
|
|
|
);
|
|
|
|
|
2021-09-08 12:45:39 +03:00
|
|
|
let output = output
|
|
|
|
.fold_with(&mut hygiene_with_config(hygiene::Config {
|
|
|
|
..Default::default()
|
|
|
|
}))
|
|
|
|
.fold_with(&mut fixer(None));
|
2021-08-11 10:24:52 +03:00
|
|
|
|
|
|
|
Some(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stdout_of(code: &str) -> Result<String, Error> {
|
|
|
|
let actual_output = Command::new("node")
|
|
|
|
.arg("-e")
|
|
|
|
.arg(&code)
|
|
|
|
.output()
|
|
|
|
.context("failed to execute output of minifier")?;
|
|
|
|
|
|
|
|
if !actual_output.status.success() {
|
|
|
|
bail!(
|
|
|
|
"failed to execute:\n{}\n{}",
|
|
|
|
String::from_utf8_lossy(&actual_output.stdout),
|
|
|
|
String::from_utf8_lossy(&actual_output.stderr)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(String::from_utf8_lossy(&actual_output.stdout).to_string())
|
|
|
|
}
|
|
|
|
|
2021-08-04 10:20:02 +03:00
|
|
|
#[testing::fixture("tests/compress/fixture/**/output.js")]
|
|
|
|
fn compressed(compressed_file: PathBuf) {
|
|
|
|
testing::run_test2(false, |cm, _handler| {
|
|
|
|
let m = parse(cm.clone(), &compressed_file);
|
|
|
|
|
|
|
|
let top_level_mark = Mark::fresh(Mark::root());
|
|
|
|
|
|
|
|
let m = optimize(
|
|
|
|
m,
|
|
|
|
cm.clone(),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
&MinifyOptions {
|
|
|
|
mangle: Some(MangleOptions {
|
|
|
|
props: Some(ManglePropertiesOptions {
|
|
|
|
reserved: Default::default(),
|
|
|
|
undeclared: false,
|
|
|
|
regex: Default::default(),
|
|
|
|
}),
|
|
|
|
top_level: true,
|
|
|
|
keep_class_names: false,
|
|
|
|
keep_fn_names: false,
|
|
|
|
keep_private_props: false,
|
|
|
|
ie8: false,
|
|
|
|
safari10: false,
|
|
|
|
}),
|
|
|
|
compress: None,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
&ExtraOptions { top_level_mark },
|
|
|
|
);
|
|
|
|
|
|
|
|
let mangled = print(cm.clone(), &m, false);
|
|
|
|
let minified = print(cm.clone(), &m, true);
|
|
|
|
|
|
|
|
parse_fm(cm.new_source_file(FileName::Anon, mangled));
|
|
|
|
parse_fm(cm.new_source_file(FileName::Anon, minified));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
}
|
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) {
|
|
|
|
testing::run_test2(false, |cm, _handler| {
|
2021-10-24 13:52:09 +03:00
|
|
|
let mut m = parse(cm.clone(), &input);
|
2021-10-11 10:44:42 +03:00
|
|
|
|
|
|
|
let top_level_mark = Mark::fresh(Mark::root());
|
|
|
|
|
2021-10-24 13:52:09 +03:00
|
|
|
m.visit_mut_with(&mut resolver_with_mark(top_level_mark));
|
|
|
|
|
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()
|
|
|
|
},
|
|
|
|
&ExtraOptions { top_level_mark },
|
|
|
|
);
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2021-08-11 10:24:52 +03:00
|
|
|
/// Tests used to prevent regressions.
|
|
|
|
#[testing::fixture("tests/exec/**/input.js")]
|
|
|
|
fn exec(input: PathBuf) {
|
|
|
|
{
|
|
|
|
let s = input.to_string_lossy().replace("_", "-");
|
|
|
|
if s.contains("issue-1709")
|
|
|
|
|| s.contains("issue-3146-1")
|
|
|
|
|| s.contains("eval-collapse-vars")
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testing::run_test2(false, |cm, handler| {
|
|
|
|
let input_src = read_to_string(&input).expect("failed to read input.js as a string");
|
|
|
|
|
2021-10-24 13:52:09 +03:00
|
|
|
let output = run(
|
|
|
|
cm.clone(),
|
|
|
|
&handler,
|
|
|
|
&input,
|
|
|
|
MangleOptions {
|
|
|
|
keep_fn_names: true,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
2021-08-11 10:24:52 +03:00
|
|
|
|
|
|
|
let output = output.expect("Parsing in base test should not fail");
|
2022-01-13 17:06:11 +03:00
|
|
|
let output = print(cm, &output, false);
|
2021-08-11 10:24:52 +03:00
|
|
|
|
|
|
|
eprintln!(
|
|
|
|
"---- {} -----\n{}",
|
|
|
|
Color::Green.paint("Optimized code"),
|
|
|
|
output
|
|
|
|
);
|
|
|
|
|
|
|
|
println!("{}", input.display());
|
|
|
|
|
|
|
|
let expected_output = stdout_of(&input_src).unwrap();
|
|
|
|
let actual_output = stdout_of(&output).expect("failed to execute the optimized code");
|
|
|
|
assert_ne!(actual_output, "");
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
DebugUsingDisplay(&actual_output),
|
|
|
|
DebugUsingDisplay(&*expected_output)
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
}
|