Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/minifier): Make name mangler respect toplevel #6774

Merged
merged 22 commits into from
Jan 10, 2023
4 changes: 3 additions & 1 deletion crates/swc/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,9 @@ impl Options {
_ => None,
})
.map(|mut c| {
c.top_level = true;
if c.top_level.is_none() {
c.top_level = Some(true);
}

c
})
Expand Down
4 changes: 3 additions & 1 deletion crates/swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,9 @@ impl Compiler {
}

if let Some(opts) = &mut min_opts.mangle {
opts.top_level = true;
if opts.top_level.is_none() {
opts.top_level = Some(true);
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/swc/tests/fixture/issues-4xxx/4953/output/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use strict";
function e(e, t) {
function _default(e, t) {
"use strict";
t.vr = function(e, t, r) {
return function() {
Expand All @@ -11,5 +11,5 @@ Object.defineProperty(exports, "__esModule", {
value: !0
}), Object.defineProperty(exports, "default", {
enumerable: !0,
get: ()=>e
get: ()=>_default
});
16 changes: 8 additions & 8 deletions crates/swc/tests/fixture/issues-6xxx/6345/1/output/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
"use strict";
function n() {}
var r = function() {
function a() {}
var te = function() {
var n = function n(n) {};
var r = null;
var t = null;
return {
init: function t(t) {
return r = new n(t);
init: function r(r) {
return t = new n(r);
}
};
}();
var t = function() {
var r = function r() {
n();
var he = function() {
var n = function n() {
a();
};
;
var t = null;
Expand Down
16 changes: 8 additions & 8 deletions crates/swc/tests/fixture/issues-6xxx/6345/2/output/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
"use strict";
function n() {}
var r = function() {
function a() {}
var te = function() {
var n = function n(n) {};
var r = null;
var t = null;
return {
init: function t(t) {
return r = new n(t);
init: function r(r) {
return t = new n(r);
}
};
}();
var t = function() {
var r = function r() {
n();
var he = function() {
var n = function n() {
a();
};
;
var t = null;
Expand Down
26 changes: 26 additions & 0 deletions crates/swc/tests/fixture/issues-6xxx/6418/1/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": false
},
"target": "es5",
"loose": false,
"minify": {
"compress": false,
"mangle": {
"toplevel": false,
"keep_classnames": false,
"keep_fnames": false,
"keep_private_props": false,
"ie8": false,
"safari10": false
}
}
},
"module": {
"type": "es6"
},
"minify": false,
"isModule": false
}
3 changes: 3 additions & 0 deletions crates/swc/tests/fixture/issues-6xxx/6418/1/input/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var foo = function () { }

console.log('PASS')
2 changes: 2 additions & 0 deletions crates/swc/tests/fixture/issues-6xxx/6418/1/output/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var foo = function o() {};
console.log("PASS");
2 changes: 1 addition & 1 deletion crates/swc_bundler/examples/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ fn do_test(_entry: &Path, entries: HashMap<String, FileName>, inline: bool, mini
..Default::default()
}),
mangle: Some(MangleOptions {
top_level: true,
top_level: Some(true),
..Default::default()
}),
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_bundler/tests/deno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ fn bundle(url: &str, minify: bool) -> String {
&swc_ecma_minifier::option::MinifyOptions {
compress: Some(Default::default()),
mangle: Some(MangleOptions {
top_level: true,
top_level: Some(true),
..Default::default()
}),
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/benches/full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn run(src: &str) {
}),
mangle: Some(MangleOptions {
props: None,
top_level: true,
top_level: Some(true),
keep_class_names: false,
keep_fn_names: false,
keep_private_props: false,
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/examples/minifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn main() {
&MinifyOptions {
compress: Some(Default::default()),
mangle: Some(MangleOptions {
top_level: true,
top_level: Some(true),
..Default::default()
}),
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/examples/minify-all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn main() {
&MinifyOptions {
compress: Some(Default::default()),
mangle: Some(MangleOptions {
top_level: true,
top_level: Some(true),
..Default::default()
}),
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/src/option/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub struct MangleOptions {
pub props: Option<ManglePropertiesOptions>,

#[serde(default, alias = "toplevel")]
pub top_level: bool,
pub top_level: Option<bool>,

#[serde(default, alias = "keep_classnames")]
pub keep_class_names: bool,
Expand Down
32 changes: 16 additions & 16 deletions crates/swc_ecma_minifier/src/pass/mangle_names/preserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,19 @@ impl Preserver {
impl Visit for Preserver {
noop_visit_type!();

fn visit_block_stmt(&mut self, n: &BlockStmt) {
let old_top_level = self.in_top_level;
for n in n.stmts.iter() {
self.in_top_level = false;
n.visit_with(self);
}
self.in_top_level = old_top_level;
}

fn visit_catch_clause(&mut self, n: &CatchClause) {
let old = self.should_preserve;

if self.options.ie8 && !self.options.top_level {
if self.options.ie8 && !self.options.top_level.unwrap_or_default() {
self.should_preserve = true;
n.param.visit_with(self);
}
Expand All @@ -52,7 +61,7 @@ impl Visit for Preserver {
fn visit_class_decl(&mut self, n: &ClassDecl) {
n.visit_children_with(self);

if (self.in_top_level && !self.options.top_level)
if (self.in_top_level && !self.options.top_level.unwrap_or_default())
|| self.options.keep_class_names
|| self.is_reserved(&n.ident)
{
Expand Down Expand Up @@ -101,7 +110,7 @@ impl Visit for Preserver {
fn visit_fn_decl(&mut self, n: &FnDecl) {
n.visit_children_with(self);

if (self.in_top_level && !self.options.top_level)
if (self.in_top_level && !self.options.top_level.unwrap_or_default())
|| self.is_reserved(&n.ident)
|| self.options.keep_fn_names
{
Expand All @@ -121,13 +130,6 @@ impl Visit for Preserver {

fn visit_ident(&mut self, _: &Ident) {}

fn visit_script(&mut self, n: &Script) {
for n in n.body.iter() {
self.in_top_level = true;
n.visit_with(self);
}
}

fn visit_module_items(&mut self, n: &[ModuleItem]) {
for n in n {
self.in_top_level = true;
Expand All @@ -145,19 +147,17 @@ impl Visit for Preserver {
}
}

fn visit_block_stmt(&mut self, n: &BlockStmt) {
let old_top_level = self.in_top_level;
for n in n.stmts.iter() {
self.in_top_level = false;
fn visit_script(&mut self, n: &Script) {
for n in n.body.iter() {
self.in_top_level = true;
n.visit_with(self);
}
self.in_top_level = old_top_level;
}

fn visit_var_declarator(&mut self, n: &VarDeclarator) {
n.visit_children_with(self);

if self.in_top_level && !self.options.top_level {
if self.in_top_level && !self.options.top_level.unwrap_or_default() {
let old = self.should_preserve;
self.should_preserve = true;
n.name.visit_with(self);
Expand Down
8 changes: 4 additions & 4 deletions crates/swc_ecma_minifier/tests/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl TestMangleOptions {
let mut data = serde_json::from_str::<Self>(s).expect("failed to deserialize mangle.json");

if let TestMangleOptions::Normal(v) = &mut data {
v.top_level = top_level.top_level;
v.top_level = Some(top_level.top_level);
}

data
Expand Down Expand Up @@ -216,7 +216,7 @@ fn run(
TestMangleOptions::Bool(v) => {
if v {
Some(MangleOptions {
top_level: false,
top_level: Some(false),
..Default::default()
})
} else {
Expand Down Expand Up @@ -359,7 +359,7 @@ fn projects(input: PathBuf) {
&input,
r#"{ "defaults": true, "toplevel": true, "passes": 3 }"#,
Some(TestMangleOptions::Normal(MangleOptions {
top_level: true,
top_level: Some(true),
..Default::default()
})),
false,
Expand Down Expand Up @@ -1690,7 +1690,7 @@ fn full(input: PathBuf) {
&input,
&config,
Some(TestMangleOptions::Normal(MangleOptions {
top_level: true,
top_level: Some(true),
..Default::default()
})),
false,
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/tests/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ fn run_exec_test(input_src: &str, config: &str, skip_mangle: bool) {
None,
Some(MangleOptions {
keep_fn_names: true,
top_level: true,
top_level: Some(true),
..Default::default()
}),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"defaults": true,
"toplevel": false
}
57 changes: 57 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/4386/1/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
var application;
/******/ (() => { // webpackBootstrap/******/ "use strict";
/******/ // The require scope
/******/ var __webpack_require__ = {};
/******/
/************************************************************************/
/******/ /* webpack/runtime/define property getters */
/******/ (() => {
/******/ // define getter functions for harmony exports
/******/ __webpack_require__.d = (exports, definition) => {
/******/ for (var key in definition) {
/******/ if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/
}
/******/
}
/******/
};
/******/
})();
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ (() => {
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/
})();
/******/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
/******/ __webpack_require__.r = (exports) => {
/******/ if (typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/
}
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/
};
/******/
})();
/******/
/************************************************************************/
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "bootstrap": () => (/* binding */ bootstrap)
/* harmony export */
});
function bootstrap() {
alert();
}

application = __webpack_exports__;
/******/
})()
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"defaults": true,
"toplevel": true
}