Skip to content

Commit

Permalink
fix(minifier): do not minify Object.defineProperty in sequence expr…
Browse files Browse the repository at this point in the history
…essions (#8416)
  • Loading branch information
Boshen committed Jan 10, 2025
1 parent 5b5b844 commit 357b61d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,20 @@ impl<'a> Traverse<'a> for PeepholeSubstituteAlternateSyntax {
call_expr: &mut CallExpression<'a>,
ctx: &mut TraverseCtx<'a>,
) {
if ctx.parent().is_expression_statement()
&& Self::is_object_define_property_exports(call_expr)
{
self.in_define_export = true;
if !self.in_fixed_loop {
let parent = ctx.parent();
if (parent.is_expression_statement() || parent.is_sequence_expression())
&& Self::is_object_define_property_exports(call_expr)
{
self.in_define_export = true;
}
}
}

fn exit_call_expression(&mut self, expr: &mut CallExpression<'a>, ctx: &mut TraverseCtx<'a>) {
self.in_define_export = false;
if !self.in_fixed_loop {
self.in_define_export = false;
}
self.try_compress_call_expression_arguments(expr, ctx);
}

Expand Down
28 changes: 28 additions & 0 deletions crates/oxc_minifier/tests/ast_passes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,35 @@ fn cjs() {
}
});",
);
test_same(
"Object.defineProperty(exports, 'ConnectableObservable', {
enumerable: true,
get: function() {
return ConnectableObservable_1.ConnectableObservable;
}
});",
);
// @babel/types/lib/index.js
test(
r#"
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "TargetNames", {
enumerable: true,
get: function () {
return _options.TargetNames;
}
});"#,
r#"
Object.defineProperty(exports, "__esModule", { value: true }), Object.defineProperty(exports, "TargetNames", {
enumerable: true,
get: function() {
return _options.TargetNames;
}
});"#,
);

test(
r#"Object.keys(_index6).forEach(function(key) {
if (key === "default" || key === "__esModule") return;
Expand Down
8 changes: 4 additions & 4 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ Original | minified | minified | gzip | gzip | Fixture

544.10 kB | 71.76 kB | 72.48 kB | 26.15 kB | 26.20 kB | lodash.js

555.77 kB | 273.21 kB | 270.13 kB | 90.92 kB | 90.80 kB | d3.js
555.77 kB | 273.21 kB | 270.13 kB | 90.93 kB | 90.80 kB | d3.js

1.01 MB | 460.18 kB | 458.89 kB | 126.77 kB | 126.71 kB | bundle.min.js

1.25 MB | 652.85 kB | 646.76 kB | 163.54 kB | 163.73 kB | three.js
1.25 MB | 652.86 kB | 646.76 kB | 163.54 kB | 163.73 kB | three.js

2.14 MB | 726.27 kB | 724.14 kB | 180.09 kB | 181.07 kB | victory.js
2.14 MB | 726.27 kB | 724.14 kB | 180.08 kB | 181.07 kB | victory.js

3.20 MB | 1.01 MB | 1.01 MB | 331.78 kB | 331.56 kB | echarts.js
3.20 MB | 1.01 MB | 1.01 MB | 331.79 kB | 331.56 kB | echarts.js

6.69 MB | 2.32 MB | 2.31 MB | 492.64 kB | 488.28 kB | antd.js

Expand Down
1 change: 0 additions & 1 deletion tasks/minsize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ pub fn run() -> Result<(), io::Error> {
let save_path = Path::new("./target/minifier").join(marker);

for file in files.files() {
println!("{}", &file.file_name);
let minified = minify_twice(file);

fs::create_dir_all(&save_path).unwrap();
Expand Down

0 comments on commit 357b61d

Please sign in to comment.