mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
1. Copy tests from
efa3dd2d8e/internal/bundler_tests/bundler_dce_test.go (L3833-L3971)
2. Add option to preserve annotate comment like `/* #__NO_SIDE_EFFECTS__
*/` and `/* #__PURE__ */`
51 lines
1.7 KiB
Rust
51 lines
1.7 KiB
Rust
use std::path::Path;
|
|
|
|
use oxc_allocator::Allocator;
|
|
use oxc_codegen::{Codegen, CodegenOptions};
|
|
use oxc_minifier::{Minifier, MinifierOptions};
|
|
use oxc_parser::Parser;
|
|
use oxc_span::SourceType;
|
|
|
|
use pico_args::Arguments;
|
|
|
|
// Instruction:
|
|
// create a `test.js`,
|
|
// run `cargo run -p oxc_minifier --example minifier`
|
|
// or `just watch "run -p oxc_minifier --example minifier"`
|
|
|
|
fn main() -> std::io::Result<()> {
|
|
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)?;
|
|
let source_type = SourceType::from_path(path).unwrap();
|
|
|
|
let printed = minify(&source_text, source_type, mangle, whitespace);
|
|
println!("{printed}");
|
|
|
|
if twice {
|
|
let printed = minify(&printed, source_type, mangle, whitespace);
|
|
println!("{printed}");
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
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);
|
|
if whitespace {
|
|
Codegen::<true>::new("", source_text, CodegenOptions::default(), None).build(program)
|
|
} else {
|
|
Codegen::<false>::new("", source_text, CodegenOptions::default(), None).build(program)
|
|
}
|
|
.source_text
|
|
}
|