From 7a520cfde4d6f39d2719668158748bda2b1d33b9 Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Sat, 26 Oct 2024 01:30:13 +0000 Subject: [PATCH] refactor(minifier): remove extra compress options (#6893) Closure Compiler and ESBuild does not have these kind of granularity. --- .../collapse_variable_declarations.rs | 14 +++--- .../peephole_substitute_alternate_syntax.rs | 22 ++++----- crates/oxc_minifier/src/compressor.rs | 10 ++-- crates/oxc_minifier/src/options.rs | 47 +------------------ crates/oxc_wasm/src/lib.rs | 5 -- tasks/minsize/src/lib.rs | 5 +- 6 files changed, 20 insertions(+), 83 deletions(-) diff --git a/crates/oxc_minifier/src/ast_passes/collapse_variable_declarations.rs b/crates/oxc_minifier/src/ast_passes/collapse_variable_declarations.rs index cf8ed22600803..c7e9de1c449cd 100644 --- a/crates/oxc_minifier/src/ast_passes/collapse_variable_declarations.rs +++ b/crates/oxc_minifier/src/ast_passes/collapse_variable_declarations.rs @@ -2,15 +2,13 @@ use oxc_allocator::Vec; use oxc_ast::ast::*; use oxc_traverse::{Traverse, TraverseCtx}; -use crate::{CompressOptions, CompressorPass}; +use crate::CompressorPass; /// Collapse variable declarations. /// /// `var a; var b = 1; var c = 2` => `var a, b = 1; c = 2` /// pub struct CollapseVariableDeclarations { - options: CompressOptions, - changed: bool, } @@ -32,8 +30,8 @@ impl<'a> Traverse<'a> for CollapseVariableDeclarations { } impl<'a> CollapseVariableDeclarations { - pub fn new(options: CompressOptions) -> Self { - Self { options, changed: false } + pub fn new() -> Self { + Self { changed: false } } fn is_require_call(var_decl: &VariableDeclaration) -> bool { @@ -58,7 +56,7 @@ impl<'a> CollapseVariableDeclarations { } fn join_vars(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) { - if !self.options.join_vars || stmts.len() < 2 { + if stmts.len() < 2 { return; } @@ -115,11 +113,11 @@ impl<'a> CollapseVariableDeclarations { mod test { use oxc_allocator::Allocator; - use crate::{tester, CompressOptions}; + use crate::tester; fn test(source_text: &str, expected: &str) { let allocator = Allocator::default(); - let mut pass = super::CollapseVariableDeclarations::new(CompressOptions::default()); + let mut pass = super::CollapseVariableDeclarations::new(); tester::test(&allocator, source_text, expected, &mut pass); } diff --git a/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs b/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs index 133e4da33b085..c9f870148097f 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs @@ -9,15 +9,13 @@ use oxc_syntax::{ }; use oxc_traverse::{Ancestor, Traverse, TraverseCtx}; -use crate::{node_util::Ctx, CompressOptions, CompressorPass}; +use crate::{node_util::Ctx, CompressorPass}; /// A peephole optimization that minimizes code by simplifying conditional /// expressions, replacing IFs with HOOKs, replacing object constructors /// with literals, and simplifying returns. /// pub struct PeepholeSubstituteAlternateSyntax { - options: CompressOptions, - /// Do not compress syntaxes that are hard to analyze inside the fixed loop. /// e.g. Do not compress `undefined -> void 0`, `true` -> `!0`. /// Opposite of `late` in Closure Compier. @@ -162,8 +160,8 @@ impl<'a> Traverse<'a> for PeepholeSubstituteAlternateSyntax { } impl<'a, 'b> PeepholeSubstituteAlternateSyntax { - pub fn new(in_fixed_loop: bool, options: CompressOptions) -> Self { - Self { options, in_fixed_loop, in_define_export: false, changed: false } + pub fn new(in_fixed_loop: bool) -> Self { + Self { in_fixed_loop, in_define_export: false, changed: false } } /* Utilities */ @@ -214,14 +212,13 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax { /* Expressions */ /// Transforms boolean expression `true` => `!0` `false` => `!1`. - /// Enabled by `compress.booleans`. /// Do not compress `true` in `Object.defineProperty(exports, 'Foo', {enumerable: true, ...})`. fn try_compress_boolean(&mut self, expr: &mut Expression<'a>, ctx: Ctx<'a, 'b>) { if self.in_fixed_loop { return; } let Expression::BooleanLiteral(lit) = expr else { return }; - if self.options.booleans && !self.in_define_export { + if !self.in_define_export { let parent = ctx.ancestry.parent(); let no_unary = { if let Ancestor::BinaryExpressionRight(u) = parent { @@ -255,10 +252,7 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax { /// Compress `typeof foo == "undefined"` into `typeof foo > "u"` /// Enabled by `compress.typeofs` - fn compress_typeof_undefined(&self, expr: &mut BinaryExpression<'a>, ctx: Ctx<'a, 'b>) { - if !self.options.typeofs { - return; - } + fn compress_typeof_undefined(&mut self, expr: &mut BinaryExpression<'a>, ctx: Ctx<'a, 'b>) { if !matches!(expr.operator, BinaryOperator::Equality | BinaryOperator::StrictEquality) { return; } @@ -289,6 +283,7 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax { ctx.ast.expression_from_string_literal(right), ); *expr = binary_expr; + self.changed = true; } fn commutative_pair( @@ -588,12 +583,11 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax { mod test { use oxc_allocator::Allocator; - use crate::{tester, CompressOptions}; + use crate::tester; fn test(source_text: &str, expected: &str) { let allocator = Allocator::default(); - let mut pass = - super::PeepholeSubstituteAlternateSyntax::new(false, CompressOptions::default()); + let mut pass = super::PeepholeSubstituteAlternateSyntax::new(false); tester::test(&allocator, source_text, expected, &mut pass); } diff --git a/crates/oxc_minifier/src/compressor.rs b/crates/oxc_minifier/src/compressor.rs index 379a062aa5e5b..c7a2bc90b8b8d 100644 --- a/crates/oxc_minifier/src/compressor.rs +++ b/crates/oxc_minifier/src/compressor.rs @@ -48,10 +48,7 @@ impl<'a> Compressor<'a> { &mut PeepholeRemoveDeadCode::new(), // TODO: MinimizeExitPoints &mut PeepholeMinimizeConditions::new(), - &mut PeepholeSubstituteAlternateSyntax::new( - /* in_fixed_loop */ true, - self.options, - ), + &mut PeepholeSubstituteAlternateSyntax::new(/* in_fixed_loop */ true), &mut PeepholeReplaceKnownMethods::new(), &mut PeepholeFoldConstants::new(), ]; @@ -77,11 +74,10 @@ impl<'a> Compressor<'a> { // Passes listed in `getFinalization` in `DefaultPassConfig` ExploitAssigns::new().build(program, &mut ctx); - CollapseVariableDeclarations::new(self.options).build(program, &mut ctx); + CollapseVariableDeclarations::new().build(program, &mut ctx); // Late latePeepholeOptimizations - PeepholeSubstituteAlternateSyntax::new(/* in_fixed_loop */ false, self.options) - .build(program, &mut ctx); + PeepholeSubstituteAlternateSyntax::new(/* in_fixed_loop */ false).build(program, &mut ctx); } fn dead_code_elimination(program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) { diff --git a/crates/oxc_minifier/src/options.rs b/crates/oxc_minifier/src/options.rs index 2dff7c98d2328..9a3c1f3b4c978 100644 --- a/crates/oxc_minifier/src/options.rs +++ b/crates/oxc_minifier/src/options.rs @@ -2,11 +2,6 @@ pub struct CompressOptions { pub dead_code_elimination: bool, - /// Various optimizations for boolean context, for example `!!a ? b : c` → `a ? b : c`. - /// - /// Default `true` - pub booleans: bool, - /// Remove `debugger;` statements. /// /// Default `true` @@ -16,26 +11,6 @@ pub struct CompressOptions { /// /// Default `false` pub drop_console: bool, - - /// Attempt to evaluate constant expressions - /// - /// Default `true` - pub evaluate: bool, - - /// Join consecutive var statements. - /// - /// Default `true` - pub join_vars: bool, - - /// Optimizations for do, while and for loops when we can statically determine the condition - /// - /// Default `true` - pub loops: bool, - - /// Transforms `typeof foo == "undefined" into `foo === void 0` - /// - /// Default `true` - pub typeofs: bool, } #[allow(clippy::derivable_impls)] @@ -47,29 +22,11 @@ impl Default for CompressOptions { impl CompressOptions { pub fn all_true() -> Self { - Self { - dead_code_elimination: false, - booleans: true, - drop_debugger: true, - drop_console: true, - evaluate: true, - join_vars: true, - loops: true, - typeofs: true, - } + Self { dead_code_elimination: false, drop_debugger: true, drop_console: true } } pub fn all_false() -> Self { - Self { - dead_code_elimination: false, - booleans: false, - drop_debugger: false, - drop_console: false, - evaluate: false, - join_vars: false, - loops: false, - typeofs: false, - } + Self { dead_code_elimination: false, drop_debugger: false, drop_console: false } } pub fn dead_code_elimination() -> Self { diff --git a/crates/oxc_wasm/src/lib.rs b/crates/oxc_wasm/src/lib.rs index 10097dbe2eaca..0c6e1d6877f73 100644 --- a/crates/oxc_wasm/src/lib.rs +++ b/crates/oxc_wasm/src/lib.rs @@ -264,13 +264,8 @@ impl Oxc { mangle: minifier_options.mangle.unwrap_or_default(), compress: if minifier_options.compress.unwrap_or_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, ..CompressOptions::default() } } else { diff --git a/tasks/minsize/src/lib.rs b/tasks/minsize/src/lib.rs index 8966c9494cf60..3d37741e09100 100644 --- a/tasks/minsize/src/lib.rs +++ b/tasks/minsize/src/lib.rs @@ -95,10 +95,7 @@ pub fn run() -> Result<(), io::Error> { fn minify_twice(file: &TestFile) -> String { let source_type = SourceType::from_path(&file.file_name).unwrap(); - let options = MinifierOptions { - mangle: true, - compress: CompressOptions { evaluate: false, ..CompressOptions::default() }, - }; + let options = MinifierOptions { mangle: true, compress: CompressOptions::default() }; // let source_text1 = minify(&file.source_text, source_type, options); // let source_text2 = minify(&source_text1, source_type, options); // assert!(source_text1 == source_text2, "Minification failed for {}", &file.file_name);