Skip to content

Commit 1337811

Browse files
committed
feat(minifier): evaluate .concat calls that has subsequent method calls (#14074)
```js x = ''.concat('a', ' ').concat('b').split(/[\\s\\n]+/) ``` was not compressed to ```js x = 'a b'.split(/[\\s\\n]+/) ``` . This PR changes the code to allow that.
1 parent 73b8bcc commit 1337811

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

crates/oxc_minifier/src/peephole/replace_known_methods.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ impl<'a> PeepholeOptimizations {
127127
return;
128128
};
129129

130-
if matches!(ctx.parent(), Ancestor::StaticMemberExpressionObject(_)) {
130+
if matches!(ctx.parent(), Ancestor::StaticMemberExpressionObject(member) if member.property().name == "concat")
131+
{
131132
return;
132133
}
133134

@@ -1389,15 +1390,22 @@ mod test {
13891390
// array
13901391
test("x = [1,2].concat(1).concat(2,['abc']).concat('abc')", "x = [1,2,1,2,'abc','abc']");
13911392
test("x = [].concat(['abc']).concat(1).concat([2,3])", "x = ['abc',1,2,3]");
1393+
test("x = [].concat(1).concat(2).join(',')", "x = [1,2].join(',')");
13921394

13931395
test("var x, y; [1].concat(x).concat(y)", "var x, y; [1].concat(x, y)");
13941396
test("var y; [1].concat(x).concat(y)", "var y; [1].concat(x, y)"); // x might have a getter that updates y, but that side effect is preserved correctly
13951397
test("var x; [1].concat(x.a).concat(x)", "var x; [1].concat(x.a, x)"); // x.a might have a getter that updates x, but that side effect is preserved correctly
1398+
test_same("x = [].map(a => a + 1).concat(1)");
13961399

13971400
// string
13981401
test("x = '1'.concat(1).concat(2,['abc']).concat('abc')", "x = '112abcabc'");
13991402
test("x = ''.concat(['abc']).concat(1).concat([2,3])", "x = 'abc12,3'");
14001403
test("x = ''.concat(1)", "x = '1'");
1404+
test(
1405+
"x = ''.concat('a', ' ').concat('b').split(/[\\s\\n]+/)",
1406+
"x = 'a b'.split(/[\\s\\n]+/)",
1407+
);
1408+
test_same("x = ''.split().concat(1)");
14011409

14021410
test("var x, y; v = ''.concat(x).concat(y)", "var x, y; v = `${x}${y}`");
14031411
test("var y; v = ''.concat(x).concat(y)", "var y; v = `${x}${y}`"); // x might have a getter that updates y, but that side effect is preserved correctly

tasks/minsize/minsize.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Original | minified | minified | gzip | gzip | Iterations | Fi
2121

2222
3.20 MB | 1.00 MB | 1.01 MB | 323.10 kB | 331.56 kB | 3 | echarts.js
2323

24-
6.69 MB | 2.22 MB | 2.31 MB | 459.28 kB | 488.28 kB | 4 | antd.js
24+
6.69 MB | 2.22 MB | 2.31 MB | 459.26 kB | 488.28 kB | 4 | antd.js
2525

2626
10.95 MB | 3.34 MB | 3.49 MB | 855.24 kB | 915.50 kB | 4 | typescript.js
2727

tasks/track_memory_allocations/allocs_minifier.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ File | File size || Sys allocs | Sys reallocs |
22
-------------------------------------------------------------------------------------------------------------------------------------------
33
checker.ts | 2.92 MB || 84074 | 14190 || 153691 | 29463 | 5.625 MB
44

5-
cal.com.tsx | 1.06 MB || 40526 | 3033 || 37074 | 4733 | 1.654 MB
5+
cal.com.tsx | 1.06 MB || 40526 | 3033 || 37077 | 4736 | 1.654 MB
66

77
RadixUIAdoptionSection.jsx | 2.52 kB || 83 | 8 || 30 | 6 | 992 B
88

99
pdf.mjs | 567.30 kB || 19823 | 2900 || 47400 | 7781 | 1.624 MB
1010

11-
antd.js | 6.69 MB || 99855 | 13518 || 331725 | 70117 | 17.407 MB
11+
antd.js | 6.69 MB || 99862 | 13523 || 331767 | 70119 | 17.415 MB
1212

1313
binder.ts | 193.08 kB || 4769 | 974 || 7059 | 834 | 201.192 kB
1414

0 commit comments

Comments
 (0)