oxc/napi/minify/src/lib.rs
翠 / green db9e93b554
feat(mangler): mangle top level variables (#7907)
Adds `top_level` option which is similar to [terser's `toplevel`
option](https://terser.org/docs/cli-usage/#cli-mangle-options).

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2024-12-15 21:31:41 +08:00

29 lines
952 B
Rust

use napi_derive::napi;
use oxc_allocator::Allocator;
use oxc_codegen::{Codegen, CodegenOptions};
use oxc_minifier::{CompressOptions, MangleOptions, Minifier, MinifierOptions};
use oxc_parser::Parser;
use oxc_span::SourceType;
#[allow(clippy::needless_pass_by_value)]
#[napi]
pub fn minify(filename: String, source_text: String) -> String {
let allocator = Allocator::default();
let source_type = SourceType::from_path(&filename).unwrap_or_default().with_typescript(true);
let mut program = Parser::new(&allocator, &source_text, source_type).parse().program;
let mangler = Minifier::new(MinifierOptions {
mangle: Some(MangleOptions::default()),
compress: CompressOptions::default(),
})
.build(&allocator, &mut program)
.mangler;
Codegen::new()
.with_options(CodegenOptions { minify: true, ..CodegenOptions::default() })
.with_mangler(mangler)
.build(&program)
.code
}