mirror of
https://github.com/swc-project/swc.git
synced 2024-12-20 20:22:26 +03:00
f44e25c3af
swc_common: - Add `Span.has_mark`. swc_ecma_codegen: - Emit `1e3` for `1000`. - Optimize output. (#1986) swc_ecma_minifier: - name mangler: Don't use keywords as an id. - `properties`: Optimize member expression with string properties. - `inline`: Inline some function expressions even if it's not fn-local. - `analyzer`: Track reassignment correctly. - `analyzer`: Track fn-local correctly. - `sequences`: Inject `void` if required. - `inline`: Inline function declarations correctly. - `sequences`: Merge expressions into test of if statements. - `sequences`: Reduce calls to an assigned variable. - Use `Marks` instead of `&dyn Comments`. swc_ecma_transforms_optimization: - `expr_simplifier`: Fix infinite loops. node/swc: - Ensure that `.transform` performs minification. (#1989)
78 lines
2.0 KiB
Rust
78 lines
2.0 KiB
Rust
use std::path::{Path, PathBuf};
|
|
use swc_common::input::SourceFileInput;
|
|
use swc_ecma_ast::EsVersion;
|
|
use swc_ecma_codegen::{
|
|
text_writer::{JsWriter, WriteJs},
|
|
Emitter,
|
|
};
|
|
use swc_ecma_parser::{lexer::Lexer, Parser, Syntax};
|
|
use testing::{run_test2, NormalizedOutput};
|
|
|
|
fn run(input: &Path, minify: bool) {
|
|
let dir = input.parent().unwrap();
|
|
let output = if minify {
|
|
dir.join(format!(
|
|
"output.min.{}",
|
|
input.extension().unwrap().to_string_lossy()
|
|
))
|
|
} else {
|
|
dir.join(format!(
|
|
"output.{}",
|
|
input.extension().unwrap().to_string_lossy()
|
|
))
|
|
};
|
|
|
|
run_test2(false, |cm, _| {
|
|
let fm = cm.load_file(&input).unwrap();
|
|
|
|
let lexer = Lexer::new(
|
|
Syntax::Typescript(Default::default()),
|
|
EsVersion::latest(),
|
|
SourceFileInput::from(&*fm),
|
|
None,
|
|
);
|
|
let mut parser = Parser::new_from(lexer);
|
|
let m = parser
|
|
.parse_module()
|
|
.expect("failed to parse input as a module");
|
|
|
|
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(swc_ecma_codegen::text_writer::omit_trailing_semi(wr));
|
|
}
|
|
|
|
let mut emitter = Emitter {
|
|
cfg: swc_ecma_codegen::Config { minify },
|
|
cm: cm.clone(),
|
|
comments: None,
|
|
wr,
|
|
};
|
|
|
|
emitter.emit_module(&m).unwrap();
|
|
}
|
|
|
|
NormalizedOutput::from(String::from_utf8(buf).unwrap())
|
|
.compare_to_file(&output)
|
|
.unwrap();
|
|
|
|
Ok(())
|
|
})
|
|
.unwrap();
|
|
}
|
|
|
|
#[testing::fixture("tests/fixture/**/input.ts")]
|
|
fn ts(input: PathBuf) {
|
|
run(&input, false);
|
|
}
|
|
|
|
#[testing::fixture("tests/fixture/**/input.js")]
|
|
fn js(input: PathBuf) {
|
|
run(&input, false);
|
|
run(&input, true);
|
|
}
|