Skip to content

Commit 6b75775

Browse files
authored
fix(es/minifier): Remove unnecessary check when invoke IIFE (#10257)
**Description:** The original code explicitly disables invoking IIFE if there's a param's method is called. However, I don't think we need that check. **Related issue:** - Closes #10255
1 parent 59f4fc3 commit 6b75775

20 files changed

+2852
-3089
lines changed
+5-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
| File | Original Size | Compressed Size | Gzipped Size |
22
| --- | --- | --- | --- |
33
| antd.js | 6.38 MiB | 2.05 MiB | 444.48 KiB |
4-
| d3.js | 542.74 KiB | 259.01 KiB | 85.06 KiB |
5-
| echarts.js | 3.41 MiB | 971.23 KiB | 313.32 KiB |
4+
| d3.js | 542.74 KiB | 259.00 KiB | 85.06 KiB |
5+
| echarts.js | 3.41 MiB | 971.04 KiB | 313.32 KiB |
66
| jquery.js | 280.89 KiB | 87.11 KiB | 30.15 KiB |
77
| lodash.js | 531.35 KiB | 68.18 KiB | 24.52 KiB |
88
| moment.js | 169.83 KiB | 57.03 KiB | 18.21 KiB |
99
| react.js | 70.45 KiB | 22.28 KiB | 8.00 KiB |
10-
| terser.js | 1.08 MiB | 444.89 KiB | 120.44 KiB |
11-
| three.js | 1.19 MiB | 627.93 KiB | 154.56 KiB |
12-
| typescript.js | 10.45 MiB | 3.17 MiB | 845.82 KiB |
10+
| terser.js | 1.08 MiB | 444.88 KiB | 120.45 KiB |
11+
| three.js | 1.19 MiB | 627.90 KiB | 154.58 KiB |
12+
| typescript.js | 10.45 MiB | 3.17 MiB | 846.03 KiB |
1313
| victory.js | 2.30 MiB | 689.65 KiB | 153.64 KiB |
1414
| vue.js | 334.13 KiB | 112.95 KiB | 41.69 KiB |

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@
3535
u: 23
3636
};
3737
_ref.u;
38-
}(), function(o) {
39-
o.a(11);
40-
}({
38+
}(), ({
4139
a: function(n) {
4240
return n;
4341
}
44-
});
42+
}).a(11);

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@
3535
u: 23
3636
};
3737
_ref.u;
38-
}(), function(o) {
39-
o.a(11);
40-
}({
38+
}(), ({
4139
a: function(n) {
4240
return n;
4341
}
44-
});
42+
}).a(11);

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ export class B {
55
}
66
}
77
//// [2.ts]
8-
!function(x) {
9-
x.then((value)=>{
10-
new value.B().print();
11-
});
12-
}(import("./0"));
8+
import("./0").then((value)=>{
9+
new value.B().print();
10+
});

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@ define([
2525
], function(require, exports, _interop_require_wildcard) {
2626
Object.defineProperty(exports, "__esModule", {
2727
value: !0
28-
}), function(x) {
29-
x.then((value)=>{
30-
new value.B().print();
31-
});
32-
}(new Promise((resolve, reject)=>require([
28+
}), new Promise((resolve, reject)=>require([
3329
"./0"
34-
], (m)=>resolve(/*#__PURE__*/ _interop_require_wildcard._(m)), reject)));
30+
], (m)=>resolve(/*#__PURE__*/ _interop_require_wildcard._(m)), reject)).then((value)=>{
31+
new value.B().print();
32+
});
3533
});

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ Object.defineProperty(exports, "__esModule", {
1717
value: !0
1818
});
1919
const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
20-
!function(x) {
21-
x.then((value)=>{
22-
new value.B().print();
23-
});
24-
}(Promise.resolve().then(()=>/*#__PURE__*/ _interop_require_wildcard._(require("./0"))));
20+
Promise.resolve().then(()=>/*#__PURE__*/ _interop_require_wildcard._(require("./0"))).then((value)=>{
21+
new value.B().print();
22+
});

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@ System.register([], function(_export, _context) {
1616
return {
1717
setters: [],
1818
execute: function() {
19-
!function(x) {
20-
x.then((value)=>{
21-
new value.B().print();
22-
});
23-
}(_context.import("./0"));
19+
_context.import("./0").then((value)=>{
20+
new value.B().print();
21+
});
2422
}
2523
};
2624
});

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@ var global, factory;
2222
global = this, factory = function(exports1, _interop_require_wildcard) {
2323
Object.defineProperty(exports1, "__esModule", {
2424
value: !0
25-
}), function(x) {
26-
x.then((value)=>{
27-
new value.B().print();
28-
});
29-
}(import("./0"));
25+
}), import("./0").then((value)=>{
26+
new value.B().print();
27+
});
3028
}, "object" == typeof module && "object" == typeof module.exports ? factory(exports, require("@swc/helpers/_/_interop_require_wildcard")) : "function" == typeof define && define.amd ? define([
3129
"exports",
3230
"@swc/helpers/_/_interop_require_wildcard"

crates/swc_ecma_minifier/src/compress/optimize/iife.rs

+1-23
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::debug::dump;
1212
use crate::{
1313
compress::optimize::Ctx,
1414
program_data::{ProgramData, ScopeData},
15-
util::{idents_captured_by, idents_used_by, make_number},
15+
util::{idents_captured_by, make_number},
1616
};
1717

1818
/// Methods related to the option `negate_iife`.
@@ -868,28 +868,6 @@ impl Optimizer<'_> {
868868
Stmt::Expr(e) => match &*e.expr {
869869
Expr::Await(..) => false,
870870

871-
// TODO: Check if parameter is used and inline if call is not related to
872-
// parameters.
873-
Expr::Call(e) => {
874-
if e.callee.as_expr().and_then(|e| e.as_ident()).is_some() {
875-
return true;
876-
}
877-
878-
let used = idents_used_by(&e.callee);
879-
880-
if used.iter().all(|id| {
881-
self.data
882-
.vars
883-
.get(id)
884-
.map(|usage| usage.ref_count == 1 && usage.callee_count > 0)
885-
.unwrap_or(false)
886-
}) {
887-
return true;
888-
}
889-
890-
param_ids.iter().all(|param| !used.contains(&param.to_id()))
891-
}
892-
893871
_ => !stmt.is_use_strict(),
894872
},
895873

crates/swc_ecma_minifier/tests/benches-full/d3.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -2699,9 +2699,7 @@ function(global, factory) {
26992699
}
27002700
function ended(event) {
27012701
var extent;
2702-
if (!function(event) {
2703-
event.stopImmediatePropagation();
2704-
}(event), event.touches) {
2702+
if (event.stopImmediatePropagation(), event.touches) {
27052703
if (event.touches.length) return;
27062704
touchending && clearTimeout(touchending), touchending = setTimeout(function() {
27072705
touchending = null;

0 commit comments

Comments
 (0)