diff --git a/crates/swc_ecma_minifier/scripts/test.sh b/crates/swc_ecma_minifier/scripts/test.sh index 7286403638e4..249ea55eb6d2 100755 --- a/crates/swc_ecma_minifier/scripts/test.sh +++ b/crates/swc_ecma_minifier/scripts/test.sh @@ -2,7 +2,7 @@ export DIFF=0 -export UPDATE=1 +# export UPDATE=1 export SWC_CHECK=0 export SWC_RUN=0 diff --git a/crates/swc_ecma_minifier/tests/fixture/next/outstatic/1/config.json b/crates/swc_ecma_minifier/tests/fixture/next/outstatic/1/config.json new file mode 100644 index 000000000000..2cab6824de34 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/next/outstatic/1/config.json @@ -0,0 +1,8 @@ +{ + "defaults": true, + "conditionals": false, + "collapse_vars": false, + "properties": false, + "join_vars": false, + "passes": 1 +} diff --git a/crates/swc_ecma_minifier/tests/fixture/next/outstatic/1/input.js b/crates/swc_ecma_minifier/tests/fixture/next/outstatic/1/input.js new file mode 100644 index 000000000000..207afff56aff --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/next/outstatic/1/input.js @@ -0,0 +1,73 @@ +(self["webpackChunk_N_E"] = self["webpackChunk_N_E"] || []).push([ + [285], { + 1973: + (function (module) { + eval(module.code) + }), + + 1985: + (function (__unused_webpack_module, exports, __webpack_require__) { + "use strict"; + + var t = 0, + r = e => "checkbox" === e.type; + var u = (e, t) => e.has((e => e.substring(0, e.search(/\.\d+(\.|$)/)) || e)(t)), + d = () => { + }; + + function H(e, t, r) { + } + var ee = e => "radio" === e.type; + + function Me(e = {}) { + let t, n = { + ...e + }, + y = {}, + h = 0, + p = { + }, + _ = { + }; + const E = 0, $ = async e => { + const r = e.target; + let s = r.name; + const a = 0; + if (a) { + let l, c; + const p = 0; + p ? (a._f.onBlur && a._f.onBlur(e), t && t(0)) : a._f.onChange && a._f.onChange(e); + const A = 0; + + a._f.deps && E(s, c, l, A) + } + }, te = (e, t = {}) => { + let s = 0; + const a = 0; + return s ? a && H(h, e, t.disabled ? void 0 : 0) : 0, { + onChange: $, + onBlur: $, + ref: a => { + if (a) { + te(e, t), s = d(y, e); + const n = 0; + (e => ee(e) || r(e))(n), + s._f.refs; + } else s = d(y, e, {}), s._f && (s._f.mount = !1), (n.shouldUnregister || t.shouldUnregister) && (!u(_.array, e) || !p.action) && _.unMount.add(e) + } + } + }; + + } + exports.useForm = function (e = {}) { + const r = t.default.useRef(); + r.current ? r.current.control._options = e : r.current = { + ...Me(e), + }; + + }; + }), + + + } +]); \ No newline at end of file diff --git a/crates/swc_ecma_minifier/tests/fixture/next/outstatic/1/mangle.json b/crates/swc_ecma_minifier/tests/fixture/next/outstatic/1/mangle.json new file mode 100644 index 000000000000..0967ef424bce --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/next/outstatic/1/mangle.json @@ -0,0 +1 @@ +{} diff --git a/crates/swc_ecma_minifier/tests/fixture/next/outstatic/1/output.js b/crates/swc_ecma_minifier/tests/fixture/next/outstatic/1/output.js new file mode 100644 index 000000000000..f1f79a0113c4 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/next/outstatic/1/output.js @@ -0,0 +1,43 @@ +(self["webpackChunk_N_E"] = self["webpackChunk_N_E"] || []).push([ + [ + 285 + ], + { + 1973: function(module) { + eval(module.code); + }, + 1985: function(o, f, i) { + "use strict"; + var k = 0, v = (o)=>"checkbox" === o.type; + var b = (o, f)=>{ + var i; + return o.has((i = f, i.substring(0, i.search(/\.\d+(\.|$)/)) || i)); + }, g = ()=>{}; + function C(o, f, i) {} + var m = (o)=>"radio" === o.type; + function w(o = {}) { + let f, i = { + ...o + }, k = {}, v = 0, b = {}, g = {}; + const C = 0, m = async (o)=>{ + const i = o.target; + let k = i.name; + const v = 0; + if (v) { + let i, b; + const g = 0; + g ? (v._f.onBlur && v._f.onBlur(o), f && f(0)) : v._f.onChange && v._f.onChange(o); + const m = 0; + v._f.deps && C(k, b, i, m); + } + }; + } + f.useForm = function(o = {}) { + const f = k.default.useRef(); + f.current ? f.current.control._options = o : f.current = { + ...w(o) + }; + }; + } + } +]); diff --git a/crates/swc_ecma_transforms_base/src/rename/mod.rs b/crates/swc_ecma_transforms_base/src/rename/mod.rs index 250954c48731..a35cf30dfdeb 100644 --- a/crates/swc_ecma_transforms_base/src/rename/mod.rs +++ b/crates/swc_ecma_transforms_base/src/rename/mod.rs @@ -243,15 +243,34 @@ where self.unresolved = self.get_unresolved(m, has_eval); - { - let map = self.get_map(m, false, true, has_eval); - - m.visit_mut_with(&mut rename_with_config(&map, self.config.clone())); - } - + let map = self.get_map(m, false, true, has_eval); + + // If we have eval, we cannot rename a whole program at once. + // + // Still, we can, and should rename some identifiers, if the containing scope + // (function-like nodes) does not have eval. This `eval` check includes + // `eval` in children. + // + // We calculate the top level map first, rename children, and then rename the + // top level. + // + // + // Order: + // + // 1. Top level map calculation + // 2. Per-unit map calculation + // 3. Per-unit renaming + // 4. Top level renaming + // + // This is because the the top level map may contain a mapping which conflicts + // with a map from one of the children. + // + // See https://github.com/swc-project/swc/pull/7615 if has_eval { m.visit_mut_children_with(self); } + + m.visit_mut_with(&mut rename_with_config(&map, self.config.clone())); } fn visit_mut_script(&mut self, m: &mut Script) { @@ -261,15 +280,13 @@ where self.unresolved = self.get_unresolved(m, has_eval); - { - let map = self.get_map(m, false, true, has_eval); - - m.visit_mut_with(&mut rename_with_config(&map, self.config.clone())); - } + let map = self.get_map(m, false, true, has_eval); if has_eval { m.visit_mut_children_with(self); } + + m.visit_mut_with(&mut rename_with_config(&map, self.config.clone())); } }