chore(minifier): add --whitespace option to example

This commit is contained in:
Boshen 2023-10-20 11:33:53 +08:00
parent c95f2e0937
commit 06efb77d57
No known key found for this signature in database
GPG key ID: 234DA6A7079C6801

View file

@ -19,34 +19,38 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
// Instruction:
// create a `test.js`,
// run `cargo run -p oxc_minifier --example minifier`
// or `cargo watch -x "run -p oxc_minifier --example minifier"`
// or `just watch "run -p oxc_minifier --example minifier"`
fn main() {
let mut args = Arguments::from_env();
let name = args.subcommand().ok().flatten().unwrap_or_else(|| String::from("test.js"));
let mangle = args.contains("--mangle");
let whitespace = args.contains("--whitespace");
let twice = args.contains("--twice");
let path = Path::new(&name);
let source_text = std::fs::read_to_string(path).unwrap_or_else(|_| panic!("{name} not found"));
let source_type = SourceType::from_path(path).unwrap();
let options = MinifierOptions { mangle, ..MinifierOptions::default() };
let printed = minify(&source_text, source_type, options);
let printed = minify(&source_text, source_type, mangle, whitespace);
println!("{printed}");
if twice {
let options = MinifierOptions { mangle, ..MinifierOptions::default() };
let printed = minify(&printed, source_type, options);
let printed = minify(&printed, source_type, mangle, whitespace);
println!("{printed}");
}
}
fn minify(source_text: &str, source_type: SourceType, options: MinifierOptions) -> String {
fn minify(source_text: &str, source_type: SourceType, mangle: bool, whitespace: bool) -> String {
let allocator = Allocator::default();
let program = Parser::new(&allocator, source_text, source_type).parse().program;
let program = allocator.alloc(program);
let options = MinifierOptions { mangle, ..MinifierOptions::default() };
Minifier::new(options).build(&allocator, program);
Codegen::<true>::new(source_text.len(), CodegenOptions).build(program)
if whitespace {
Codegen::<true>::new(source_text.len(), CodegenOptions).build(program)
} else {
Codegen::<false>::new(source_text.len(), CodegenOptions).build(program)
}
}