diff --git a/crates/oxc_wasm/src/lib.rs b/crates/oxc_wasm/src/lib.rs index b7bd814b8..7ecae5be5 100644 --- a/crates/oxc_wasm/src/lib.rs +++ b/crates/oxc_wasm/src/lib.rs @@ -263,10 +263,19 @@ impl Oxc { let program = allocator.alloc(program); if minifier_options.compress() || minifier_options.mangle() { + let compress_options = minifier_options.compress_options(); let options = MinifierOptions { mangle: minifier_options.mangle(), compress: if minifier_options.compress() { - CompressOptions::default() + CompressOptions { + booleans: compress_options.booleans(), + drop_console: compress_options.drop_console(), + drop_debugger: compress_options.drop_debugger(), + evaluate: compress_options.evaluate(), + join_vars: compress_options.join_vars(), + loops: compress_options.loops(), + typeofs: compress_options.typeofs(), + } } else { CompressOptions::all_false() }, diff --git a/crates/oxc_wasm/src/options.rs b/crates/oxc_wasm/src/options.rs index cdaadaeae..3ada7e35c 100644 --- a/crates/oxc_wasm/src/options.rs +++ b/crates/oxc_wasm/src/options.rs @@ -164,6 +164,7 @@ pub struct OxcMinifierOptions { whitespace: bool, mangle: bool, compress: bool, + compress_options: OxcCompressOptions, } #[wasm_bindgen] @@ -203,6 +204,121 @@ impl OxcMinifierOptions { pub fn set_compress(&mut self, yes: bool) { self.compress = yes; } + + #[wasm_bindgen(getter = compressOptions)] + pub fn compress_options(&self) -> OxcCompressOptions { + self.compress_options + } + + #[wasm_bindgen(setter = compressOptions)] + pub fn set_compress_options(&mut self, options: OxcCompressOptions) { + self.compress_options = options; + } +} + +#[wasm_bindgen] +#[derive(Clone, Copy)] +pub struct OxcCompressOptions { + booleans: bool, + drop_debugger: bool, + drop_console: bool, + evaluate: bool, + join_vars: bool, + loops: bool, + typeofs: bool, +} + +// keep same with `oxc_minifier::options::CompressOptions` +impl Default for OxcCompressOptions { + fn default() -> Self { + Self { + booleans: true, + drop_debugger: true, + drop_console: false, + evaluate: true, + join_vars: true, + loops: true, + typeofs: true, + } + } +} + +#[wasm_bindgen] +impl OxcCompressOptions { + #[wasm_bindgen(constructor)] + pub fn new() -> Self { + Self::default() + } + + #[wasm_bindgen(getter)] + pub fn booleans(self) -> bool { + self.booleans + } + + #[wasm_bindgen(setter)] + pub fn set_booleans(&mut self, yes: bool) { + self.booleans = yes; + } + + #[wasm_bindgen(getter = dropDebugger)] + pub fn drop_debugger(self) -> bool { + self.drop_debugger + } + + #[wasm_bindgen(setter = dropDebugger)] + pub fn set_drop_debugger(&mut self, yes: bool) { + self.drop_debugger = yes; + } + + #[wasm_bindgen(getter = dropConsole)] + pub fn drop_console(self) -> bool { + self.drop_console + } + + #[wasm_bindgen(setter = dropConsole)] + pub fn set_drop_console(&mut self, yes: bool) { + self.drop_console = yes; + } + + #[wasm_bindgen(getter)] + pub fn evaluate(self) -> bool { + self.evaluate + } + + #[wasm_bindgen(setter)] + pub fn set_evaluate(&mut self, yes: bool) { + self.evaluate = yes; + } + + #[wasm_bindgen(getter = joinVars)] + pub fn join_vars(self) -> bool { + self.join_vars + } + + #[wasm_bindgen(setter = joinVars)] + pub fn set_join_vars(&mut self, yes: bool) { + self.join_vars = yes; + } + + #[wasm_bindgen(getter)] + pub fn loops(self) -> bool { + self.loops + } + + #[wasm_bindgen(setter)] + pub fn set_loops(&mut self, yes: bool) { + self.loops = yes; + } + + #[wasm_bindgen(getter)] + pub fn typeofs(self) -> bool { + self.typeofs + } + + #[wasm_bindgen(setter)] + pub fn set_typeofs(&mut self, yes: bool) { + self.typeofs = yes; + } } #[wasm_bindgen] diff --git a/website/playground/index.html b/website/playground/index.html index 26e9a2e0b..375722d5d 100644 --- a/website/playground/index.html +++ b/website/playground/index.html @@ -28,6 +28,27 @@ .controls > div > button.active { background-color: #555; } .controls > div > label { margin-right: 4px; } .controls label { font-size: 14px; } + .compress-control {position: relative; display: inline-flex;} + .compression-options { + position: absolute; + top: 100%; + left: 0; + background-color: #2c3136; + border: 1px solid #ccc; + padding: 10px; + display: none; + z-index: 1; + display: none; + flex-direction: column; + min-width: 180px; + font-size: 12px; + } + .compress-control:hover .compression-options { + display: flex; + } + .compression-options label { margin-bottom: 4px; } + .compression-options label input { margin-right: 8px; } + #duration { margin-left: auto; } #viewer { flex: 1; overflow-y: auto; } #divider { width: 4px; background: #444; } @@ -80,9 +101,45 @@ +