diff --git a/crates/oxc_minifier/src/ast_passes/mod.rs b/crates/oxc_minifier/src/ast_passes/mod.rs index 447f56f5365b2..3c781f838d43d 100644 --- a/crates/oxc_minifier/src/ast_passes/mod.rs +++ b/crates/oxc_minifier/src/ast_passes/mod.rs @@ -24,6 +24,8 @@ pub use peephole_substitute_alternate_syntax::PeepholeSubstituteAlternateSyntax; pub use remove_syntax::RemoveSyntax; pub use statement_fusion::StatementFusion; +use crate::CompressOptions; + pub trait CompressorPass<'a>: Traverse<'a> { fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>); } @@ -76,7 +78,7 @@ pub struct LatePeepholeOptimizations { } impl LatePeepholeOptimizations { - pub fn new() -> Self { + pub fn new(options: CompressOptions) -> Self { let in_fixed_loop = true; Self { x0_statement_fusion: StatementFusion::new(), @@ -84,6 +86,7 @@ impl LatePeepholeOptimizations { x2_peephole_remove_dead_code: PeepholeRemoveDeadCode::new(), x3_peephole_minimize_conditions: PeepholeMinimizeConditions::new(in_fixed_loop), x4_peephole_substitute_alternate_syntax: PeepholeSubstituteAlternateSyntax::new( + options, in_fixed_loop, ), x5_peephole_replace_known_methods: PeepholeReplaceKnownMethods::new(), @@ -194,6 +197,10 @@ impl<'a> Traverse<'a> for LatePeepholeOptimizations { fn exit_property_key(&mut self, key: &mut PropertyKey<'a>, ctx: &mut TraverseCtx<'a>) { self.x4_peephole_substitute_alternate_syntax.exit_property_key(key, ctx); } + + fn exit_catch_clause(&mut self, catch: &mut CatchClause<'a>, ctx: &mut TraverseCtx<'a>) { + self.x4_peephole_substitute_alternate_syntax.exit_catch_clause(catch, ctx); + } } // See `createPeepholeOptimizationsPass` @@ -207,11 +214,12 @@ pub struct PeepholeOptimizations { } impl PeepholeOptimizations { - pub fn new() -> Self { + pub fn new(options: CompressOptions) -> Self { let in_fixed_loop = false; Self { x2_peephole_minimize_conditions: PeepholeMinimizeConditions::new(in_fixed_loop), x3_peephole_substitute_alternate_syntax: PeepholeSubstituteAlternateSyntax::new( + options, in_fixed_loop, ), x4_peephole_replace_known_methods: PeepholeReplaceKnownMethods::new(), @@ -273,6 +281,10 @@ impl<'a> Traverse<'a> for PeepholeOptimizations { fn exit_property_key(&mut self, key: &mut PropertyKey<'a>, ctx: &mut TraverseCtx<'a>) { self.x3_peephole_substitute_alternate_syntax.exit_property_key(key, ctx); } + + fn exit_catch_clause(&mut self, catch: &mut CatchClause<'a>, ctx: &mut TraverseCtx<'a>) { + self.x3_peephole_substitute_alternate_syntax.exit_catch_clause(catch, ctx); + } } pub struct DeadCodeElimination { 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 2693cb60876fa..9fc2796662fc1 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 @@ -4,19 +4,21 @@ use oxc_ecmascript::{ToInt32, ToJsString}; use oxc_semantic::IsGlobalReference; use oxc_span::{GetSpan, SPAN}; use oxc_syntax::{ + es_target::ESTarget, identifier::is_identifier_name, number::NumberBase, operator::{BinaryOperator, UnaryOperator}, }; use oxc_traverse::{traverse_mut_with_ctx, Ancestor, ReusableTraverseCtx, Traverse, TraverseCtx}; -use crate::{node_util::Ctx, CompressorPass}; +use crate::{node_util::Ctx, CompressOptions, 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 Compiler. @@ -45,6 +47,10 @@ impl<'a> Traverse<'a> for PeepholeSubstituteAlternateSyntax { self.compress_return_statement(stmt); } + fn exit_catch_clause(&mut self, catch: &mut CatchClause<'a>, _ctx: &mut TraverseCtx<'a>) { + self.compress_catch_clause(catch); + } + fn exit_variable_declaration( &mut self, decl: &mut VariableDeclaration<'a>, @@ -118,8 +124,8 @@ impl<'a> Traverse<'a> for PeepholeSubstituteAlternateSyntax { } impl<'a, 'b> PeepholeSubstituteAlternateSyntax { - pub fn new(in_fixed_loop: bool) -> Self { - Self { in_fixed_loop, in_define_export: false, changed: false } + pub fn new(options: CompressOptions, in_fixed_loop: bool) -> Self { + Self { options, in_fixed_loop, in_define_export: false, changed: false } } /* Utilities */ @@ -767,6 +773,20 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax { ctx.ast.alloc_static_member_expression(e.span, object, property, false), )) } + + fn compress_catch_clause(&mut self, catch: &mut CatchClause<'a>) { + if catch.body.body.is_empty() + && !self.in_fixed_loop + && self.options.target >= ESTarget::ES2019 + { + if let Some(param) = &catch.param { + if param.pattern.kind.is_binding_identifier() { + catch.param = None; + self.changed = true; + } + }; + } + } } /// Port from @@ -774,11 +794,12 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax { mod test { use oxc_allocator::Allocator; - use crate::tester; + use crate::{tester, CompressOptions}; fn test(source_text: &str, expected: &str) { let allocator = Allocator::default(); - let mut pass = super::PeepholeSubstituteAlternateSyntax::new(false); + let options = CompressOptions::default(); + let mut pass = super::PeepholeSubstituteAlternateSyntax::new(options, false); tester::test(&allocator, source_text, expected, &mut pass); } @@ -802,7 +823,7 @@ mod test { test("var x = undefined", "var x"); test_same("var undefined = 1;function f() {var undefined=2;var x;}"); test("function f(undefined) {}", "function f(undefined){}"); - test("try {} catch(undefined) {}", "try{}catch(undefined){}"); + test("try {} catch(undefined) {foo}", "try{}catch(undefined){foo}"); test("for (undefined in {}) {}", "for(undefined in {}){}"); test("undefined++;", "undefined++"); test("undefined += undefined;", "undefined+=void 0"); @@ -1271,6 +1292,22 @@ mod test { test_same("x['😊']"); } + #[test] + fn optional_catch_binding() { + test("try {} catch(e) {}", "try {} catch {}"); + test_same("try {} catch([e]) {}"); + test_same("try {} catch({e}) {}"); + + let allocator = Allocator::default(); + let options = CompressOptions { + target: oxc_syntax::es_target::ESTarget::ES2018, + ..CompressOptions::default() + }; + let mut pass = super::PeepholeSubstituteAlternateSyntax::new(options, false); + let code = "try {} catch(e) {}"; + tester::test(&allocator, code, code, &mut pass); + } + // ---------- /// Port from diff --git a/crates/oxc_minifier/src/compressor.rs b/crates/oxc_minifier/src/compressor.rs index e284ee5a66368..83c7213589eec 100644 --- a/crates/oxc_minifier/src/compressor.rs +++ b/crates/oxc_minifier/src/compressor.rs @@ -36,10 +36,10 @@ impl<'a> Compressor<'a> { let mut ctx = ReusableTraverseCtx::new(scopes, symbols, self.allocator); RemoveSyntax::new(self.options).build(program, &mut ctx); Normalize::new().build(program, &mut ctx); - PeepholeOptimizations::new().build(program, &mut ctx); + PeepholeOptimizations::new(self.options).build(program, &mut ctx); CollapsePass::new().build(program, &mut ctx); - LatePeepholeOptimizations::new().run_in_loop(program, &mut ctx); - PeepholeOptimizations::new().build(program, &mut ctx); + LatePeepholeOptimizations::new(self.options).run_in_loop(program, &mut ctx); + PeepholeOptimizations::new(self.options).build(program, &mut ctx); } pub fn dead_code_elimination(self, program: &mut Program<'a>) { diff --git a/tasks/minsize/minsize.snap b/tasks/minsize/minsize.snap index fcc5fd5eb42f5..93fdf6ac1f50f 100644 --- a/tasks/minsize/minsize.snap +++ b/tasks/minsize/minsize.snap @@ -1,27 +1,27 @@ | Oxc | ESBuild | Oxc | ESBuild | Original | minified | minified | gzip | gzip | Fixture ------------------------------------------------------------------------------------- -72.14 kB | 23.72 kB | 23.70 kB | 8.62 kB | 8.54 kB | react.development.js +72.14 kB | 23.71 kB | 23.70 kB | 8.62 kB | 8.54 kB | react.development.js 173.90 kB | 60.15 kB | 59.82 kB | 19.50 kB | 19.33 kB | moment.js -287.63 kB | 90.57 kB | 90.07 kB | 32.19 kB | 31.95 kB | jquery.js +287.63 kB | 90.56 kB | 90.07 kB | 32.18 kB | 31.95 kB | jquery.js -342.15 kB | 118.54 kB | 118.14 kB | 44.56 kB | 44.37 kB | vue.js +342.15 kB | 118.53 kB | 118.14 kB | 44.56 kB | 44.37 kB | vue.js -544.10 kB | 72.00 kB | 72.48 kB | 26.20 kB | 26.20 kB | lodash.js +544.10 kB | 71.98 kB | 72.48 kB | 26.19 kB | 26.20 kB | lodash.js 555.77 kB | 273.88 kB | 270.13 kB | 91.19 kB | 90.80 kB | d3.js -1.01 MB | 461.00 kB | 458.89 kB | 126.92 kB | 126.71 kB | bundle.min.js +1.01 MB | 460.99 kB | 458.89 kB | 126.92 kB | 126.71 kB | bundle.min.js -1.25 MB | 653.54 kB | 646.76 kB | 164.05 kB | 163.73 kB | three.js +1.25 MB | 653.54 kB | 646.76 kB | 164.04 kB | 163.73 kB | three.js -2.14 MB | 727.91 kB | 724.14 kB | 180.39 kB | 181.07 kB | victory.js +2.14 MB | 727.90 kB | 724.14 kB | 180.39 kB | 181.07 kB | victory.js -3.20 MB | 1.01 MB | 1.01 MB | 332.27 kB | 331.56 kB | echarts.js +3.20 MB | 1.01 MB | 1.01 MB | 332.29 kB | 331.56 kB | echarts.js -6.69 MB | 2.32 MB | 2.31 MB | 493.25 kB | 488.28 kB | antd.js +6.69 MB | 2.32 MB | 2.31 MB | 493.24 kB | 488.28 kB | antd.js -10.95 MB | 3.51 MB | 3.49 MB | 910.90 kB | 915.50 kB | typescript.js +10.95 MB | 3.51 MB | 3.49 MB | 910.88 kB | 915.50 kB | typescript.js