mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
chore(minifier): add --whitespace option to example
This commit is contained in:
parent
c95f2e0937
commit
06efb77d57
1 changed files with 11 additions and 7 deletions
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue