Skip to content

Commit c9a6c23

Browse files
authored
feat(es/minifier): Invoke IIFE into block (#10220)
**Description:** This is maybe the biggest missing feature of terser as compared to gcc because its lack of proper scope support. But we have. Further development: 1. invoke IIFE with return into labelled break 2. invoke IIFE in place where value is needed like `if (() => {} ())` 3. invoke IIFE in seq and other side effect free expressions **Related issue:** - Closes #10054
1 parent 482b63a commit c9a6c23

File tree

165 files changed

+34838
-34855
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

165 files changed

+34838
-34855
lines changed

.changeset/cold-dodos-mix.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_ecma_minifier: minor
3+
swc_core: minor
4+
---
5+
6+
feat(es/minifier): Invoke IIFE into block

crates/swc/tests/fixture/issues-4xxx/4953/output/index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
"use strict";
22
function _default(e, t) {
33
"use strict";
4-
t.vr = function(e, t, r) {
5-
return function() {
6-
return '123';
4+
!function(e) {
5+
"use strict";
6+
e.vr = function(e, t, r) {
7+
return function() {
8+
return '123';
9+
};
710
};
8-
};
11+
}(t);
912
}
1013
Object.defineProperty(exports, "__esModule", {
1114
value: !0
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
| File | Original Size | Compressed Size | Gzipped Size |
22
| --- | --- | --- | --- |
3-
| antd.js | 6.38 MiB | 2.05 MiB | 444.65 KiB |
4-
| d3.js | 542.74 KiB | 259.09 KiB | 85.07 KiB |
5-
| echarts.js | 3.41 MiB | 971.28 KiB | 313.35 KiB |
6-
| jquery.js | 280.89 KiB | 87.07 KiB | 30.17 KiB |
7-
| lodash.js | 531.35 KiB | 68.27 KiB | 24.49 KiB |
3+
| antd.js | 6.38 MiB | 2.05 MiB | 444.53 KiB |
4+
| d3.js | 542.74 KiB | 259.03 KiB | 85.06 KiB |
5+
| echarts.js | 3.41 MiB | 971.15 KiB | 313.35 KiB |
6+
| jquery.js | 280.89 KiB | 87.09 KiB | 30.15 KiB |
7+
| lodash.js | 531.35 KiB | 68.18 KiB | 24.52 KiB |
88
| moment.js | 169.83 KiB | 56.90 KiB | 18.18 KiB |
9-
| react.js | 70.45 KiB | 22.33 KiB | 7.99 KiB |
10-
| terser.js | 1.08 MiB | 444.77 KiB | 120.27 KiB |
11-
| three.js | 1.19 MiB | 628.06 KiB | 154.56 KiB |
12-
| typescript.js | 10.45 MiB | 3.17 MiB | 843.24 KiB |
13-
| victory.js | 2.30 MiB | 690.95 KiB | 153.66 KiB |
14-
| vue.js | 334.13 KiB | 113.02 KiB | 41.67 KiB |
9+
| react.js | 70.45 KiB | 22.28 KiB | 8.01 KiB |
10+
| terser.js | 1.08 MiB | 444.83 KiB | 120.44 KiB |
11+
| three.js | 1.19 MiB | 628.04 KiB | 154.56 KiB |
12+
| typescript.js | 10.45 MiB | 3.17 MiB | 845.76 KiB |
13+
| victory.js | 2.30 MiB | 689.58 KiB | 153.63 KiB |
14+
| vue.js | 334.13 KiB | 112.98 KiB | 41.66 KiB |

crates/swc/tests/tsc-references/amdImportNotAsPrimaryExpression.2.minified.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ define([
66
], function(require, exports, _class_call_check) {
77
Object.defineProperty(exports, "__esModule", {
88
value: !0
9-
}), function(target, all) {
10-
for(var name in all)Object.defineProperty(target, name, {
11-
enumerable: !0,
12-
get: all[name]
13-
});
14-
}(exports, {
9+
});
10+
var E1, all = {
1511
C1: function() {
1612
return C1;
1713
},
1814
E1: function() {
1915
return E11;
2016
}
17+
};
18+
for(var name in all)Object.defineProperty(exports, name, {
19+
enumerable: !0,
20+
get: all[name]
2121
});
22-
var E1, C1 = function C1() {
22+
var C1 = function C1() {
2323
_class_call_check._(this, C1), this.m1 = 42;
2424
};
2525
C1.s1 = !0;
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
//// [assignmentCompatWithDiscriminatedUnion.ts]
22
var Example1, Example2, Example3, Example4, Example5, GH14865, GH30170, GH12052, GH18421, GH15907, GH20889, GH39357, b1;
3-
Example1 || (Example1 = {}), t = s, Example2 || (Example2 = {}), t = s, Example3 || (Example3 = {}), t = s, Example4 || (Example4 = {}), t = s, Example5 || (Example5 = {}), t = s, GH14865 || (GH14865 = {}), b1.type, GH30170 || (GH30170 = {}), function(GH12052) {
4-
function getAxisType() {
5-
return "categorical";
6-
}
7-
getAxisType(), getAxisType();
8-
}(GH12052 || (GH12052 = {})), GH18421 || (GH18421 = {}), GH15907 || (GH15907 = {}), GH20889 || (GH20889 = {}), GH39357 || (GH39357 = {}), ("a" === b || "b" === b) && b;
3+
function getAxisType() {
4+
return "categorical";
5+
}
6+
Example1 || (Example1 = {}), t = s, Example2 || (Example2 = {}), t = s, Example3 || (Example3 = {}), t = s, Example4 || (Example4 = {}), t = s, Example5 || (Example5 = {}), t = s, GH14865 || (GH14865 = {}), b1.type, GH30170 || (GH30170 = {}), GH12052 || (GH12052 = {}), getAxisType(), getAxisType(), GH18421 || (GH18421 = {}), GH15907 || (GH15907 = {}), GH20889 || (GH20889 = {}), GH39357 || (GH39357 = {}), ("a" === b || "b" === b) && b;

crates/swc/tests/tsc-references/asyncAwaitIsolatedModules_es5.2.minified.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@ var M;
33
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
44
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
55
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
6-
!function(M) {
7-
function _f1() {
8-
return (_f1 = _async_to_generator(function() {
9-
return _ts_generator(this, function(_state) {
10-
return [
11-
2
12-
];
13-
});
14-
})).apply(this, arguments);
15-
}
16-
M.f1 = function() {
17-
return _f1.apply(this, arguments);
18-
};
19-
}(M || (M = {}));
6+
function _f1() {
7+
return (_f1 = _async_to_generator(function() {
8+
return _ts_generator(this, function(_state) {
9+
return [
10+
2
11+
];
12+
});
13+
})).apply(this, arguments);
14+
}
15+
(M || (M = {})).f1 = function() {
16+
return _f1.apply(this, arguments);
17+
};
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
//// [asyncAwaitIsolatedModules_es6.ts]
22
var M;
33
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
4-
!function(M) {
5-
function _f1() {
6-
return (_f1 = _async_to_generator(function*() {})).apply(this, arguments);
7-
}
8-
M.f1 = function() {
9-
return _f1.apply(this, arguments);
10-
};
11-
}(M || (M = {}));
4+
function _f1() {
5+
return (_f1 = _async_to_generator(function*() {})).apply(this, arguments);
6+
}
7+
(M || (M = {})).f1 = function() {
8+
return _f1.apply(this, arguments);
9+
};

crates/swc/tests/tsc-references/asyncAwait_es5.2.minified.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@ var M;
33
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
44
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
55
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
6-
!function(M) {
7-
function _f1() {
8-
return (_f1 = _async_to_generator(function() {
9-
return _ts_generator(this, function(_state) {
10-
return [
11-
2
12-
];
13-
});
14-
})).apply(this, arguments);
15-
}
16-
M.f1 = function() {
17-
return _f1.apply(this, arguments);
18-
};
19-
}(M || (M = {}));
6+
function _f1() {
7+
return (_f1 = _async_to_generator(function() {
8+
return _ts_generator(this, function(_state) {
9+
return [
10+
2
11+
];
12+
});
13+
})).apply(this, arguments);
14+
}
15+
(M || (M = {})).f1 = function() {
16+
return _f1.apply(this, arguments);
17+
};
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
//// [asyncAwait_es6.ts]
22
var M;
33
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
4-
!function(M) {
5-
function _f1() {
6-
return (_f1 = _async_to_generator(function*() {})).apply(this, arguments);
7-
}
8-
M.f1 = function() {
9-
return _f1.apply(this, arguments);
10-
};
11-
}(M || (M = {}));
4+
function _f1() {
5+
return (_f1 = _async_to_generator(function*() {})).apply(this, arguments);
6+
}
7+
(M || (M = {})).f1 = function() {
8+
return _f1.apply(this, arguments);
9+
};

crates/swc/tests/tsc-references/commonJSImportNotAsPrimaryExpression.2.minified.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
//// [foo_0.ts]
22
Object.defineProperty(exports, "__esModule", {
33
value: !0
4-
}), function(target, all) {
5-
for(var name in all)Object.defineProperty(target, name, {
6-
enumerable: !0,
7-
get: all[name]
8-
});
9-
}(exports, {
4+
});
5+
var E1, target = exports, all = {
106
C1: function() {
117
return C1;
128
},
139
E1: function() {
1410
return E11;
1511
}
12+
};
13+
for(var name in all)Object.defineProperty(target, name, {
14+
enumerable: !0,
15+
get: all[name]
1616
});
17-
var E1, _class_call_check = require("@swc/helpers/_/_class_call_check"), C1 = function C1() {
17+
var _class_call_check = require("@swc/helpers/_/_class_call_check"), C1 = function C1() {
1818
_class_call_check._(this, C1), this.m1 = 42;
1919
};
2020
C1.s1 = !0;

0 commit comments

Comments
 (0)