Skip to content

Commit 06770cf

Browse files
authoredJan 11, 2023
fix(es/minifier): Make AST compressor respect toplevel (#6775)
**Related issue:** - Closes #4386.
1 parent 267d256 commit 06770cf

File tree

12 files changed

+109
-2
lines changed

12 files changed

+109
-2
lines changed
 

‎crates/swc_ecma_minifier/src/compress/optimize/inline.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ where
2929
&mut self,
3030
ident: &mut Ident,
3131
init: &mut Expr,
32-
should_preserve: bool,
32+
mut should_preserve: bool,
3333
can_drop: bool,
3434
) {
3535
trace_op!(
@@ -58,6 +58,7 @@ where
5858
if !usage.var_initialized {
5959
return;
6060
}
61+
6162
if self.data.top.used_arguments && usage.declared_as_fn_param {
6263
return;
6364
}
@@ -95,6 +96,8 @@ where
9596
let is_inline_enabled =
9697
self.options.reduce_vars || self.options.collapse_vars || self.options.inline != 0;
9798

99+
should_preserve |= !self.options.top_level() && usage.is_top_level;
100+
98101
self.vars.inline_with_multi_replacer(init);
99102

100103
// We inline arrays partially if it's pure (all elements are literal), and not

‎crates/swc_ecma_minifier/src/program_data.rs

+5
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ pub(crate) struct VarUsageInfo {
121121

122122
pub(crate) pure_fn: bool,
123123

124+
/// Is the variable declared in top level?
125+
pub(crate) is_top_level: bool,
126+
124127
/// `infects_to`. This should be renamed, but it will be done with another
125128
/// PR. (because it's hard to review)
126129
infects: Vec<Access>,
@@ -168,6 +171,7 @@ impl Default for VarUsageInfo {
168171
used_in_non_child_fn: Default::default(),
169172
accessed_props: Default::default(),
170173
used_recursively: Default::default(),
174+
is_top_level: Default::default(),
171175
}
172176
}
173177
}
@@ -346,6 +350,7 @@ impl Storage for ProgramData {
346350
// }
347351

348352
let v = self.vars.entry(i.to_id()).or_default();
353+
v.is_top_level |= ctx.is_top_level;
349354

350355
if has_init && (v.declared || v.var_initialized) {
351356
#[cfg(feature = "debug")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"defaults": true,
3+
"toplevel": false
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var application;
2+
(() => {
3+
var __webpack_require__ = {};
4+
(() => {
5+
__webpack_require__.d = (exports, definition) => {
6+
};
7+
})();
8+
(() => {
9+
__webpack_require__.o = (obj, prop) => { };
10+
})();
11+
(() => {
12+
__webpack_require__.r = (exports) => {
13+
};
14+
})();
15+
var __webpack_exports__ = {};
16+
__webpack_require__.r(__webpack_exports__);
17+
__webpack_require__.d(__webpack_exports__, {
18+
"bootstrap": () => (bootstrap)
19+
});
20+
function bootstrap() {
21+
alert();
22+
}
23+
application = __webpack_exports__;
24+
})();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var application;
2+
(()=>{
3+
var __webpack_require__ = {};
4+
__webpack_require__.d = (exports, definition)=>{}, __webpack_require__.o = (obj, prop)=>{}, __webpack_require__.r = (exports)=>{};
5+
var __webpack_exports__ = {};
6+
function bootstrap() {
7+
alert();
8+
}
9+
__webpack_require__.r(__webpack_exports__), __webpack_require__.d(__webpack_exports__, {
10+
bootstrap: ()=>bootstrap
11+
}), application = __webpack_exports__;
12+
})();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"defaults": true,
3+
"toplevel": true
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var application;
2+
(() => {
3+
var __webpack_require__ = {};
4+
(() => {
5+
__webpack_require__.d = (exports, definition) => {
6+
};
7+
})();
8+
(() => {
9+
__webpack_require__.o = (obj, prop) => { };
10+
})();
11+
(() => {
12+
__webpack_require__.r = (exports) => {
13+
};
14+
})();
15+
var __webpack_exports__ = {};
16+
__webpack_require__.r(__webpack_exports__);
17+
__webpack_require__.d(__webpack_exports__, {
18+
"bootstrap": () => (bootstrap)
19+
});
20+
function bootstrap() {
21+
alert();
22+
}
23+
application = __webpack_exports__;
24+
})();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
(()=>{
2+
var __webpack_require__ = {};
3+
__webpack_require__.d = (exports, definition)=>{}, __webpack_require__.o = (obj, prop)=>{}, __webpack_require__.r = (exports)=>{};
4+
var __webpack_exports__ = {};
5+
function bootstrap() {
6+
alert();
7+
}
8+
__webpack_require__.r(__webpack_exports__), __webpack_require__.d(__webpack_exports__, {
9+
bootstrap: ()=>bootstrap
10+
});
11+
})();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"defaults": true,
3+
"toplevel": true
4+
}

‎crates/swc_ecma_transforms_optimization/src/simplify/dce/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,14 @@ impl TreeShaker {
633633
} else if !self.in_block_stmt {
634634
return false;
635635
}
636+
637+
// Abort if the variable is declared on top level scope.
638+
let ix = self.data.graph_ix.get_index_of(&name);
639+
if let Some(ix) = ix {
640+
if self.data.entries.contains(&(ix as u32)) {
641+
return false;
642+
}
643+
}
636644
}
637645

638646
if self.config.top_retain.contains(&name.0) {

‎crates/swc_ecma_usage_analyzer/src/analyzer/ctx.rs

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ where
2121
}
2222

2323
#[derive(Debug, Default, Clone, Copy)]
24+
#[non_exhaustive]
2425
pub struct Ctx {
2526
/// See [crate::marks::Marks]
2627
pub skip_standalone: bool,
@@ -60,6 +61,8 @@ pub struct Ctx {
6061
pub inline_prevented: bool,
6162

6263
pub is_op_assign: bool,
64+
65+
pub is_top_level: bool,
6366
}
6467

6568
pub(super) struct WithCtx<'a, S>

‎crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ where
7979
let mut child = UsageAnalyzer {
8080
data: Default::default(),
8181
marks: self.marks,
82-
ctx: self.ctx,
82+
ctx: Ctx {
83+
is_top_level: false,
84+
..self.ctx
85+
},
8386
scope: Default::default(),
8487
used_recursively: self.used_recursively.clone(),
8588
};
@@ -849,6 +852,7 @@ where
849852
fn visit_module(&mut self, n: &Module) {
850853
let ctx = Ctx {
851854
skip_standalone: true,
855+
is_top_level: true,
852856
..self.ctx
853857
};
854858
n.visit_children_with(&mut *self.with_ctx(ctx))
@@ -857,6 +861,7 @@ where
857861
fn visit_script(&mut self, n: &Script) {
858862
let ctx = Ctx {
859863
skip_standalone: true,
864+
is_top_level: true,
860865
..self.ctx
861866
};
862867
n.visit_children_with(&mut *self.with_ctx(ctx))

0 commit comments

Comments
 (0)
Please sign in to comment.