Skip to content

Commit

Permalink
feat(es/minifier): Respect inline level and preserve native names (#8205
Browse files Browse the repository at this point in the history
)

**Description:**

Note: Preserving native names is a hack, but it's used by `terser` and it's the only way to preserve the name of `class AbortSignal` while mangling without `keep_classnames: true`. We can special case `AbortSignal`, but let's just follow `terser`.


**Related issue:**

 - vercel/next.js#57904
  • Loading branch information
kdy1 committed Nov 1, 2023
1 parent d3ce0f1 commit dd805e9
Show file tree
Hide file tree
Showing 40 changed files with 13,743 additions and 15,124 deletions.
1 change: 1 addition & 0 deletions crates/swc_ecma_lints/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![cfg_attr(feature = "non_critical_lints", deny(unused))]
#![cfg_attr(feature = "non_critical_lints", deny(clippy::all))]
#![allow(rustc::untranslatable_diagnostic_trivial)]

pub mod config;
pub mod rule;
Expand Down
16 changes: 14 additions & 2 deletions crates/swc_ecma_minifier/src/compress/optimize/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ impl Optimizer<'_> {
}
}

if u.declared_as_fn_expr {
if self.options.inline != 3 {
return;
}
}

should_inline
} else {
false
Expand Down Expand Up @@ -428,6 +434,11 @@ impl Optimizer<'_> {
return;
}
}
if init_usage.declared_as_fn_expr {
if self.options.inline != 3 {
return;
}
}
}
}

Expand Down Expand Up @@ -712,10 +723,11 @@ impl Optimizer<'_> {
#[allow(unused)]
match &decl {
Decl::Class(c) => {
if self.options.keep_classnames
if self.options.inline != 3
|| self.options.keep_classnames
|| self.mangle_options.map_or(false, |v| v.keep_class_names)
{
log_abort!("inline: [x] Keep fn names");
log_abort!("inline: [x] Keep class names");
return;
}

Expand Down
1 change: 0 additions & 1 deletion crates/swc_ecma_minifier/tests/TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ reduce_vars/issue_294/input.js
reduce_vars/issue_3140_4/input.js
reduce_vars/modified/input.js
reduce_vars/obj_arg_2/input.js
reduce_vars/obj_for_1/input.js
reduce_vars/obj_var_2/input.js
reduce_vars/passes/input.js
reduce_vars/perf_3/input.js
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,24 @@
return PageWithSpread;
}
});
var jsx_runtime = __webpack_require__(5893), react = __webpack_require__(7294), Form = __webpack_require__(2726);
function _defineProperty(obj, key, value) {
return key in obj ? Object.defineProperty(obj, key, {
value: value,
enumerable: !0,
configurable: !0,
writable: !0
}) : obj[key] = value, obj;
}
function _objectSpread(target) {
for(var i = 1; i < arguments.length; i++){
var source = null != arguments[i] ? arguments[i] : {}, ownKeys = Object.keys(source);
"function" == typeof Object.getOwnPropertySymbols && (ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
}))), ownKeys.forEach(function(key) {
_defineProperty(target, key, source[key]);
});
}
return target;
}
var components_Container = function(props) {
return (0, jsx_runtime.jsx)(Form.Z, _objectSpread({}, props));
var jsx_runtime = __webpack_require__(5893), react = __webpack_require__(7294), Form = __webpack_require__(2726), components_Container = function(props) {
return (0, jsx_runtime.jsx)(Form.Z, function(target) {
for(var i = 1; i < arguments.length; i++){
var source = null != arguments[i] ? arguments[i] : {}, ownKeys = Object.keys(source);
"function" == typeof Object.getOwnPropertySymbols && (ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
}))), ownKeys.forEach(function(key) {
var value;
value = source[key], key in target ? Object.defineProperty(target, key, {
value: value,
enumerable: !0,
configurable: !0,
writable: !0
}) : target[key] = value;
});
}
return target;
}({}, props));
};
function PageWithSpread() {
var ref = (0, react.useState)(""), text = ref[0], setText = ref[1], handleChange = (0, react.useCallback)(function(e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export function regexCheck(regex) {
return check;
function check(code) {
return function(code) {
return null !== code && regex.test(String.fromCharCode(code));
}
};
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
function regexCheck(regex) {
return check;
function check(code) {
return function(code) {
return null !== code && regex.test(String.fromCharCode(code));
}
};
}
console.log(regexCheck("Foo")), console.log(regexCheck("Foo")), console.log(regexCheck("Foo")), console.log(regexCheck("Foo")), console.log(regexCheck("Foo")), console.log(regexCheck("Foo")), console.log(regexCheck("Foo")), console.log(regexCheck("Foo")), console.log(regexCheck("Foo")), console.log(regexCheck("Foo"));
74 changes: 36 additions & 38 deletions crates/swc_ecma_minifier/tests/fixture/next/36127/2/3/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ export class Text {
eq(t) {
if (t == this) return !0;
if (t.length != this.length || t.lines != this.lines) return !1;
let e = this.scanIdentical(t, 1), n = this.length - this.scanIdentical(t, -1), i = new s(this), h = new s(t);
for(let t = e, s = e;;){
if (i.next(t), h.next(t), t = 0, i.lineBreak != h.lineBreak || i.done != h.done || i.value != h.value) return !1;
if (s += i.value.length, i.done || s >= n) return !0;
let e = this.scanIdentical(t, 1), n = this.length - this.scanIdentical(t, -1), s = new i(this), h = new i(t);
for(let t = e, i = e;;){
if (s.next(t), h.next(t), t = 0, s.lineBreak != h.lineBreak || s.done != h.done || s.value != h.value) return !1;
if (i += s.value.length, s.done || i >= n) return !0;
}
}
iter(t = 1) {
return new s(this, t);
return new i(this, t);
}
iterRange(t, e = this.length) {
return new h(this, t, e);
return new s(this, t, e);
}
iterLines(t, e) {
let n;
Expand All @@ -42,7 +42,7 @@ export class Text {
let i = this.line(t).from;
n = this.iterRange(i, Math.max(i, e == this.lines + 1 ? this.length : e <= 1 ? 0 : this.line(e - 1).to));
}
return new l(n);
return new h(n);
}
toString() {
return this.sliceString(0);
Expand All @@ -57,7 +57,11 @@ export class Text {
}
}
class t extends Text {
constructor(t, e = n(t)){
constructor(t, e = function(t) {
let e = -1;
for (let n of t)e += n.length + 1;
return e;
}(t)){
super(), this.text = t, this.length = e;
}
get lines() {
Expand All @@ -73,24 +77,24 @@ class t extends Text {
i = l + 1, n++;
}
}
decompose(e, n, s, h) {
let l = e <= 0 && n >= this.length ? this : new t(i(this.text, [
decompose(e, i, s, h) {
let l = e <= 0 && i >= this.length ? this : new t(n(this.text, [
""
], e, n), Math.min(n, this.length) - Math.max(0, e));
], e, i), Math.min(i, this.length) - Math.max(0, e));
if (1 & h) {
let e = s.pop(), n = i(l.text, e.text.slice(), 0, l.length);
if (n.length <= 32) s.push(new t(n, e.length + l.length));
let e = s.pop(), i = n(l.text, e.text.slice(), 0, l.length);
if (i.length <= 32) s.push(new t(i, e.length + l.length));
else {
let e = n.length >> 1;
s.push(new t(n.slice(0, e)), new t(n.slice(e)));
let e = i.length >> 1;
s.push(new t(i.slice(0, e)), new t(i.slice(e)));
}
} else s.push(l);
}
replace(n, s, h) {
if (!(h instanceof t)) return super.replace(n, s, h);
let l = i(this.text, i(h.text, i(this.text, [
replace(i, s, h) {
if (!(h instanceof t)) return super.replace(i, s, h);
let l = n(this.text, n(h.text, n(this.text, [
""
], 0, n)), s), r = this.length + h.length - (s - n);
], 0, i)), s), r = this.length + h.length - (s - i);
return l.length <= 32 ? new t(l, r) : e.from(t.split(l, []), r);
}
sliceString(t, e = this.length, n = "\n") {
Expand Down Expand Up @@ -189,24 +193,18 @@ class e extends Text {
return new t(e, i);
}
let h = Math.max(32, s >> 5), l = h << 1, r = h >> 1, o = [], f = 0, c = -1, u = [];
function a(n) {
let i;
if (n.lines > l && n instanceof e) for (let t of n.children)a(t);
else n.lines > r && (f > r || !f) ? (g(), o.push(n)) : n instanceof t && f && (i = u[u.length - 1]) instanceof t && n.lines + i.lines <= 32 ? (f += n.lines, c += n.length + 1, u[u.length - 1] = new t(i.text.concat(n.text), i.length + 1 + n.length)) : (f + n.lines > h && g(), f += n.lines, c += n.length + 1, u.push(n));
}
function g() {
function a() {
0 != f && (o.push(1 == u.length ? u[0] : e.from(u, c)), c = -1, f = u.length = 0);
}
for (let t of n)a(t);
return g(), 1 == o.length ? o[0] : new e(o, i);
for (let i of n)!function n(i) {
let s;
if (i.lines > l && i instanceof e) for (let t of i.children)n(t);
else i.lines > r && (f > r || !f) ? (a(), o.push(i)) : i instanceof t && f && (s = u[u.length - 1]) instanceof t && i.lines + s.lines <= 32 ? (f += i.lines, c += i.length + 1, u[u.length - 1] = new t(s.text.concat(i.text), s.length + 1 + i.length)) : (f + i.lines > h && a(), f += i.lines, c += i.length + 1, u.push(i));
}(i);
return a(), 1 == o.length ? o[0] : new e(o, i);
}
}
function n(t) {
let e = -1;
for (let n of t)e += n.length + 1;
return e;
}
function i(t, e, n = 0, i = 1e9) {
function n(t, e, n = 0, i = 1e9) {
for(let s = 0, h = 0, l = !0; h < t.length && s <= i; h++){
let r = t[h], o = s + r.length;
o >= n && (o > i && (r = r.slice(0, i - s)), s < n && (r = r.slice(n - s)), l ? (e[e.length - 1] += r, l = !1) : e.push(r)), s = o + 1;
Expand All @@ -216,7 +214,7 @@ function i(t, e, n = 0, i = 1e9) {
Text.empty = new t([
""
], 0);
class s {
class i {
constructor(e, n = 1){
this.dir = n, this.done = !1, this.lineBreak = !1, this.value = "", this.nodes = [
e
Expand Down Expand Up @@ -247,9 +245,9 @@ class s {
return t < 0 && (this.nextInner(-t, -this.dir), t = this.value.length), this.nextInner(t, this.dir);
}
}
class h {
class s {
constructor(t, e, n){
this.value = "", this.done = !1, this.cursor = new s(t, e > n ? -1 : 1), this.pos = e > n ? t.length : 0, this.from = Math.min(e, n), this.to = Math.max(e, n);
this.value = "", this.done = !1, this.cursor = new i(t, e > n ? -1 : 1), this.pos = e > n ? t.length : 0, this.from = Math.min(e, n), this.to = Math.max(e, n);
}
nextInner(t, e) {
if (e < 0 ? this.pos <= this.from : this.pos >= this.to) return this.value = "", this.done = !0, this;
Expand All @@ -266,7 +264,7 @@ class h {
return this.cursor.lineBreak && "" != this.value;
}
}
class l {
class h {
constructor(t){
this.inner = t, this.afterBreak = !0, this.value = "", this.done = !1;
}
Expand All @@ -280,7 +278,7 @@ class l {
}
"undefined" != typeof Symbol && (Text.prototype[Symbol.iterator] = function() {
return this.iter();
}, s.prototype[Symbol.iterator] = h.prototype[Symbol.iterator] = l.prototype[Symbol.iterator] = function() {
}, i.prototype[Symbol.iterator] = s.prototype[Symbol.iterator] = h.prototype[Symbol.iterator] = function() {
return this;
});
export class Line {
Expand Down

0 comments on commit dd805e9

Please sign in to comment.