Skip to content

Commit

Permalink
chore(minifier): add --whitespace option to example
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Oct 20, 2023
1 parent c95f2e0 commit 06efb77
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions crates/oxc_minifier/examples/minifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 06efb77

Please sign in to comment.