Skip to content

Commit 4b2903e

Browse files
authored
fix(es/minifier): Fix minifier (#2564)
swc_ecma_minifier: - Don't change depth of function while negating if statements. (#2558) swc_ecma_transforms_optimization: - `dead_branch_remover`: Preserve `this`. (#2465, #2466) swc: - Make `toplevel` default to true if `module` is true. (#2254)
1 parent 0364f9a commit 4b2903e

File tree

27 files changed

+747
-212
lines changed

27 files changed

+747
-212
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ include = ["Cargo.toml", "src/**/*.rs"]
2121
license = "Apache-2.0/MIT"
2222
name = "swc"
2323
repository = "https://github.com/swc-project/swc.git"
24-
version = "0.79.1"
24+
version = "0.79.2"
2525

2626
[lib]
2727
name = "swc"

ecmascript/minifier/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ include = ["Cargo.toml", "src/**/*.rs", "src/lists/*.json"]
77
license = "Apache-2.0/MIT"
88
name = "swc_ecma_minifier"
99
repository = "https://github.com/swc-project/swc.git"
10-
version = "0.46.5"
10+
version = "0.46.6"
1111

1212
[features]
1313
debug = ["backtrace"]

ecmascript/minifier/src/compress/pure/if_return.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,19 @@ impl<M> Pure<'_, M> {
7575
);
7676

7777
let mut new = vec![];
78+
let mut fn_decls = vec![];
7879
new.extend(stmts.drain(..pos_of_if));
79-
let cons = stmts.drain(1..).collect::<Vec<_>>();
80+
let cons = stmts
81+
.drain(1..)
82+
.filter_map(|stmt| {
83+
if matches!(stmt, Stmt::Decl(Decl::Fn(..))) {
84+
fn_decls.push(stmt);
85+
return None;
86+
}
87+
88+
Some(stmt)
89+
})
90+
.collect::<Vec<_>>();
8091

8192
let if_stmt = stmts.take().into_iter().next().unwrap();
8293
match if_stmt {
@@ -101,6 +112,8 @@ impl<M> Pure<'_, M> {
101112
}
102113
}
103114

115+
new.extend(fn_decls);
116+
104117
*stmts = new;
105118
}
106119
}

ecmascript/minifier/tests/compress/fixture/issues/2011/actual/output.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,30 @@ function _defineProperties(target, props) {
77
descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor);
88
}
99
}
10-
function _createClass(Constructor, protoProps, staticProps) {
11-
return protoProps && _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), Constructor;
12-
}
13-
function _defineProperty(obj, key, value) {
14-
return key in obj ? Object.defineProperty(obj, key, {
15-
value: value,
16-
enumerable: !0,
17-
configurable: !0,
18-
writable: !0
19-
}) : obj[key] = value, obj;
20-
}
2110
var ClassA1 = function ClassA() {
2211
"use strict";
2312
_classCallCheck(this, ClassA);
2413
};
2514
module.exports = (function() {
26-
var ClassB1 = function() {
15+
var obj, value, ClassB1 = function() {
2716
"use strict";
17+
var Constructor, protoProps, staticProps;
2818
function ClassB() {
2919
_classCallCheck(this, ClassB);
3020
}
31-
return _createClass(ClassB, [
21+
return Constructor = ClassB, protoProps = [
3222
{
3323
key: "it",
3424
value: function() {
3525
this.bb = new ClassB.MyA();
3626
}
3727
}
38-
]), ClassB;
28+
], _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), ClassB;
3929
}();
40-
return _defineProperty(ClassB1, "MyA", ClassA1), ClassB1;
30+
return obj = ClassB1, value = ClassA1, "MyA" in obj ? Object.defineProperty(obj, "MyA", {
31+
value: value,
32+
enumerable: !0,
33+
configurable: !0,
34+
writable: !0
35+
}) : obj.MyA = value, ClassB1;
4136
})();

ecmascript/minifier/tests/compress/fixture/issues/2011/reduced/output.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
class ClassA {
2-
}
31
module.exports = class ClassB {
4-
static MyA = ClassA;
2+
static MyA = class {
3+
};
54
it() {
65
this.bb = new ClassB.MyA();
76
}
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
function isSymbol(s) {
2-
return null != s;
3-
}
4-
function isKey(value, object) {
5-
return !!(null == value || isSymbol(value));
6-
}
7-
module.exports = isKey;
1+
module.exports = function(value, object) {
2+
return null == value || null != value;
3+
};

ecmascript/minifier/tests/compress/fixture/issues/2044/full/output.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
function createCommonjsModule(fn) {
22
return fn();
33
}
4-
const isFile = (config)=>!0
5-
, pkgBrowserslist = {
6-
}, config = {
7-
};
84
createCommonjsModule(function(module, exports) {
95
Object.defineProperty(exports, "__esModule", {
106
value: !0
@@ -32,11 +28,9 @@ createCommonjsModule(function(module, exports) {
3228
exports.default = null, module.exports = exports.default;
3329
}), createCommonjsModule(function(module, exports) {
3430
exports.default = void 0;
35-
});
36-
var namespace$1 = createCommonjsModule(function(module, exports) {
31+
}), createCommonjsModule(function(module, exports) {
3732
exports.default = void 0, module.exports = exports.default;
38-
});
39-
createCommonjsModule(function(module, exports) {
33+
}), createCommonjsModule(function(module, exports) {
4034
exports.default = void 0, module.exports = exports.default;
4135
}), createCommonjsModule(function(module, exports) {
4236
exports.default = void 0, exports.default = String;
Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +0,0 @@
1-
function cost(oldItem, newItem) {
2-
if (newItem.val === oldItem.val) return 0;
3-
if ("object" == typeof oldItem.val && "object" == typeof newItem.val) {
4-
if (void 0 === oldItem.key || void 0 === newItem.key) return 1;
5-
if (oldItem.key === newItem.key) return 0;
6-
}
7-
return 1;
8-
}
Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +0,0 @@
1-
function f(a, b) {
2-
if (a) {
3-
if (b) return;
4-
foo();
5-
}
6-
bar();
7-
}

0 commit comments

Comments
 (0)