2022-04-24 14:40:39 +03:00
|
|
|
extern crate swc_node_base;
|
|
|
|
|
|
|
|
use std::{
|
|
|
|
fmt::Debug,
|
|
|
|
fs::read_to_string,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
process::Command,
|
|
|
|
sync::mpsc,
|
|
|
|
thread,
|
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
|
|
|
|
|
|
|
use ansi_term::Color;
|
|
|
|
use anyhow::{anyhow, bail, Context, Error};
|
|
|
|
use serde::Deserialize;
|
2022-09-01 07:36:30 +03:00
|
|
|
use swc_common::{
|
|
|
|
comments::SingleThreadedComments,
|
|
|
|
errors::{Handler, HANDLER},
|
|
|
|
sync::Lrc,
|
|
|
|
Mark, SourceMap,
|
|
|
|
};
|
2022-04-24 14:40:39 +03:00
|
|
|
use swc_ecma_ast::*;
|
|
|
|
use swc_ecma_codegen::{
|
|
|
|
text_writer::{omit_trailing_semi, JsWriter, WriteJs},
|
|
|
|
Emitter,
|
|
|
|
};
|
|
|
|
use swc_ecma_minifier::{
|
|
|
|
optimize,
|
|
|
|
option::{terser::TerserCompressorOptions, CompressOptions, ExtraOptions, MinifyOptions},
|
|
|
|
};
|
|
|
|
use swc_ecma_parser::{
|
|
|
|
lexer::{input::SourceFileInput, Lexer},
|
|
|
|
EsConfig, Parser, Syntax,
|
|
|
|
};
|
2022-04-26 10:38:50 +03:00
|
|
|
use swc_ecma_transforms_base::{fixer::fixer, hygiene::hygiene, resolver};
|
2022-04-24 14:40:39 +03:00
|
|
|
use swc_ecma_visit::{FoldWith, VisitMutWith};
|
|
|
|
use testing::assert_eq;
|
|
|
|
|
|
|
|
#[testing::fixture(
|
|
|
|
"tests/terser/compress/**/input.js",
|
|
|
|
exclude(
|
2022-05-09 12:15:12 +03:00
|
|
|
// We don't care about ie8
|
|
|
|
"ie8",
|
2022-04-27 05:21:39 +03:00
|
|
|
// tests with infinite loops
|
|
|
|
"reduce_vars/toplevel_off_loops_2",
|
|
|
|
"reduce_vars/toplevel_on_loops_2",
|
|
|
|
"drop_unused/issue_1656",
|
|
|
|
"transform/condition_evaluate",
|
|
|
|
"reduce_vars/var_assign_3",
|
2022-07-05 08:04:49 +03:00
|
|
|
// should error
|
|
|
|
"blocks/issue_1672_if_strict",
|
|
|
|
"blocks/issue_1672_for_strict",
|
|
|
|
// need support for script mode
|
|
|
|
"blocks/issue_1672_if",
|
|
|
|
"blocks/issue_1672_for",
|
2022-04-24 14:40:39 +03:00
|
|
|
)
|
|
|
|
)]
|
|
|
|
fn terser_exec(input: PathBuf) {
|
|
|
|
let dir = input.parent().unwrap();
|
|
|
|
|
|
|
|
let config = dir.join("config.json");
|
|
|
|
let config = read_to_string(&config).expect("failed to read config.json");
|
|
|
|
eprintln!("---- {} -----\n{}", Color::Green.paint("Config"), config);
|
|
|
|
|
|
|
|
let _ = testing::run_test2(false, |cm, handler| {
|
|
|
|
let input_src = read_to_string(&input).map_err(|_| {
|
|
|
|
eprintln!("WTF?");
|
|
|
|
})?;
|
|
|
|
// This can change the output
|
|
|
|
if input_src.contains("/*#/PURE/*/") {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2022-05-09 12:15:12 +03:00
|
|
|
let input_stdout = stdout_of(&input_src, Duration::from_millis(1000)).map_err(|_| {
|
|
|
|
eprintln!("This test is not executable test: \n{}", input_src);
|
2022-04-24 18:27:51 +03:00
|
|
|
})?;
|
|
|
|
|
2022-04-25 18:31:59 +03:00
|
|
|
// Formmating
|
|
|
|
if input_stdout.contains("function") {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2022-05-12 10:28:52 +03:00
|
|
|
let mut expected_src = None;
|
|
|
|
|
|
|
|
let expected_stdout = read_to_string(&dir.join("expected.stdout"))
|
|
|
|
.or_else(|_| {
|
|
|
|
let src = read_to_string(&dir.join("output.terser.js"))
|
|
|
|
.context("This test does not have `output.terser.js`")?;
|
|
|
|
|
|
|
|
expected_src = Some(src.clone());
|
2022-04-24 14:40:39 +03:00
|
|
|
|
2022-05-12 10:28:52 +03:00
|
|
|
stdout_of(&src, Duration::from_millis(500))
|
|
|
|
.context("This test is not executable test")
|
|
|
|
})
|
|
|
|
.context("This test is not an excutable test")
|
|
|
|
.map_err(|err| {
|
|
|
|
eprintln!("{}", err);
|
2022-04-24 14:40:39 +03:00
|
|
|
})?;
|
|
|
|
|
2022-04-24 18:27:51 +03:00
|
|
|
if input_stdout != expected_stdout {
|
|
|
|
eprintln!("This test is not for execution");
|
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
|
2022-04-24 14:40:39 +03:00
|
|
|
eprintln!("Optimizing");
|
|
|
|
|
|
|
|
let output = run(cm.clone(), &handler, &input, &config);
|
|
|
|
let output_module = match output {
|
|
|
|
Some(v) => v,
|
|
|
|
None => return Err(()),
|
|
|
|
};
|
|
|
|
|
|
|
|
let actual = print(cm, &[output_module], false, false);
|
|
|
|
let actual_stdout = stdout_of(&actual, Duration::from_secs(5)).unwrap();
|
|
|
|
|
2022-05-12 10:28:52 +03:00
|
|
|
if let Some(expected_src) = expected_src {
|
|
|
|
eprintln!(
|
|
|
|
"---- {} -----\n{}",
|
|
|
|
Color::Green.paint("Expected"),
|
|
|
|
expected_src
|
|
|
|
);
|
|
|
|
}
|
2022-04-24 18:27:51 +03:00
|
|
|
|
|
|
|
eprintln!("---- {} -----\n{}", Color::Green.paint("Actual"), actual);
|
|
|
|
|
2022-04-24 14:40:39 +03:00
|
|
|
eprintln!(
|
|
|
|
"---- {} -----\n{}",
|
|
|
|
Color::Green.paint("Expected stdout"),
|
|
|
|
expected_stdout
|
|
|
|
);
|
|
|
|
eprintln!(
|
|
|
|
"---- {} -----\n{}",
|
|
|
|
Color::Green.paint("Actual stdout"),
|
|
|
|
actual_stdout
|
|
|
|
);
|
|
|
|
|
|
|
|
println!("{}", input.display());
|
|
|
|
|
|
|
|
assert_eq!(expected_stdout, actual_stdout);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
|
|
struct TestOptions {
|
|
|
|
#[serde(default)]
|
|
|
|
defaults: bool,
|
|
|
|
|
|
|
|
#[serde(default)]
|
|
|
|
passes: usize,
|
|
|
|
}
|
|
|
|
|
2022-04-25 09:56:19 +03:00
|
|
|
fn parse_compressor_config(cm: Lrc<SourceMap>, s: &str) -> Result<(bool, CompressOptions), Error> {
|
2022-04-24 14:40:39 +03:00
|
|
|
let opts: TestOptions =
|
2022-04-25 09:56:19 +03:00
|
|
|
serde_json::from_str(s).context("failed to deserialize value into a compressor config")?;
|
2022-04-24 14:40:39 +03:00
|
|
|
let mut c: TerserCompressorOptions =
|
2022-04-25 09:56:19 +03:00
|
|
|
serde_json::from_str(s).context("failed to deserialize value into a compressor config")?;
|
2022-04-24 14:40:39 +03:00
|
|
|
|
|
|
|
c.defaults = opts.defaults;
|
|
|
|
c.const_to_let = Some(false);
|
|
|
|
c.passes = opts.passes;
|
|
|
|
|
2022-04-25 09:56:19 +03:00
|
|
|
Ok((c.module, c.into_config(cm)))
|
2022-04-24 14:40:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run(cm: Lrc<SourceMap>, handler: &Handler, input: &Path, config: &str) -> Option<Module> {
|
2022-09-01 07:36:30 +03:00
|
|
|
HANDLER.set(handler, || {
|
|
|
|
let (_module, config) = parse_compressor_config(cm.clone(), config).ok()?;
|
|
|
|
if config.ie8 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let fm = cm.load_file(input).expect("failed to load input.js");
|
|
|
|
let comments = SingleThreadedComments::default();
|
|
|
|
|
|
|
|
eprintln!("---- {} -----\n{}", Color::Green.paint("Input"), fm.src);
|
|
|
|
|
|
|
|
let unresolved_mark = Mark::new();
|
|
|
|
let top_level_mark = Mark::new();
|
|
|
|
|
|
|
|
let minification_start = Instant::now();
|
|
|
|
|
|
|
|
let lexer = Lexer::new(
|
|
|
|
Syntax::Es(EsConfig {
|
|
|
|
jsx: true,
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
Default::default(),
|
|
|
|
SourceFileInput::from(&*fm),
|
|
|
|
Some(&comments),
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut parser = Parser::new_from(lexer);
|
|
|
|
let program = parser
|
|
|
|
.parse_module()
|
|
|
|
.map_err(|err| {
|
|
|
|
err.into_diagnostic(handler).emit();
|
|
|
|
})
|
|
|
|
.map(|module| module.fold_with(&mut resolver(unresolved_mark, top_level_mark, false)));
|
|
|
|
|
|
|
|
// Ignore parser errors.
|
|
|
|
//
|
|
|
|
// This is typically related to strict mode caused by module context.
|
|
|
|
let program = match program {
|
|
|
|
Ok(v) => v,
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let optimization_start = Instant::now();
|
|
|
|
let mut output = optimize(
|
|
|
|
program.into(),
|
|
|
|
cm,
|
|
|
|
Some(&comments),
|
|
|
|
None,
|
|
|
|
&MinifyOptions {
|
|
|
|
compress: Some(config),
|
|
|
|
mangle: None,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
&ExtraOptions {
|
|
|
|
unresolved_mark,
|
|
|
|
top_level_mark,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.expect_module();
|
|
|
|
let end = Instant::now();
|
|
|
|
tracing::info!(
|
|
|
|
"optimize({}) took {:?}",
|
|
|
|
input.display(),
|
|
|
|
end - optimization_start
|
|
|
|
);
|
|
|
|
|
|
|
|
output.visit_mut_with(&mut hygiene());
|
|
|
|
|
|
|
|
let output = output.fold_with(&mut fixer(None));
|
|
|
|
|
|
|
|
let end = Instant::now();
|
|
|
|
tracing::info!(
|
|
|
|
"process({}) took {:?}",
|
|
|
|
input.display(),
|
|
|
|
end - minification_start
|
|
|
|
);
|
|
|
|
|
|
|
|
Some(output)
|
|
|
|
})
|
2022-04-24 14:40:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn stdout_of(code: &str, timeout: Duration) -> Result<String, Error> {
|
|
|
|
eprintln!("Executing node with timeout: {:?}", timeout);
|
|
|
|
|
|
|
|
let code = code.to_string();
|
|
|
|
let (sender, receiver) = mpsc::channel();
|
|
|
|
let timer_sender = sender.clone();
|
|
|
|
|
|
|
|
let _t = thread::spawn(move || {
|
|
|
|
let res = (|| {
|
|
|
|
let actual_output = Command::new("node")
|
|
|
|
.arg("-e")
|
2022-05-08 11:27:14 +03:00
|
|
|
.arg(format!(
|
|
|
|
"
|
|
|
|
{}
|
|
|
|
{}",
|
|
|
|
include_str!("./terser_exec_base.js"),
|
|
|
|
code
|
|
|
|
))
|
2022-04-24 14:40:39 +03:00
|
|
|
.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())
|
|
|
|
})();
|
|
|
|
|
|
|
|
let _ = sender.send(res);
|
|
|
|
});
|
|
|
|
|
|
|
|
let _timer = thread::spawn(move || {
|
|
|
|
thread::sleep(timeout);
|
|
|
|
let _ = timer_sender.send(Err(anyhow!("node timed out")));
|
|
|
|
});
|
|
|
|
|
|
|
|
receiver.recv().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print<N: swc_ecma_codegen::Node>(
|
|
|
|
cm: Lrc<SourceMap>,
|
|
|
|
nodes: &[N],
|
|
|
|
minify: bool,
|
|
|
|
skip_semi: bool,
|
|
|
|
) -> String {
|
|
|
|
let mut buf = vec![];
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut wr: Box<dyn WriteJs> = Box::new(JsWriter::new(cm.clone(), "\n", &mut buf, None));
|
|
|
|
if minify || skip_semi {
|
|
|
|
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-04-24 14:40:39 +03:00
|
|
|
cm,
|
|
|
|
comments: None,
|
|
|
|
wr,
|
|
|
|
};
|
|
|
|
|
|
|
|
for n in nodes {
|
|
|
|
n.emit_with(&mut emitter).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String::from_utf8(buf).unwrap()
|
|
|
|
}
|