Skip to content

Commit a685c88

Browse files
authored
fix(es/minifier): Don't drop assignments to unused top-level variables (swc-project#7581)
**Related issue:** - Closes swc-project#7568
1 parent d3b934f commit a685c88

File tree

6 files changed

+53
-1
lines changed

6 files changed

+53
-1
lines changed

crates/swc_ecma_minifier/src/compress/optimize/unused.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,8 @@ impl Optimizer<'_> {
711711
&& var.usage_count == 0
712712
&& var.declared
713713
&& (!var.declared_as_fn_param || !used_arguments || self.ctx.in_strict)
714+
&& (self.options.top_level()
715+
|| i.span.ctxt != self.marks.top_level_ctxt)
714716
{
715717
report_change!(
716718
"unused: Dropping assignment to var '{}{:?}', which is never used",

crates/swc_ecma_minifier/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ pub fn optimize(
100100
let _timer = timer!("minify");
101101

102102
let mut marks = Marks::new();
103+
marks.top_level_ctxt = SyntaxContext::empty().apply_mark(extra.top_level_mark);
103104
marks.unresolved_mark = extra.unresolved_mark;
104105

105106
debug_assert_valid(&n);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var specific_microfront;
2+
(function () { // webpackBootstrap
3+
"use strict";
4+
var __webpack_modules__ = ({
5+
6+
7+
8+
});
9+
function __webpack_require__(moduleId) {
10+
var module = __webpack_module_cache__[moduleId] = {
11+
id: moduleId,
12+
loaded: false,
13+
exports: {}
14+
15+
};
16+
17+
__webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__);
18+
module.loaded = true;
19+
return module.exports;
20+
21+
}
22+
23+
var __webpack_exports__ = __webpack_require__("webpack/container/entry/specific_page");
24+
specific_microfront = __webpack_exports__;
25+
26+
27+
})()
28+
;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var specific_microfront;
2+
!function() {
3+
"use strict";
4+
var __webpack_modules__ = {};
5+
specific_microfront = function __webpack_require__(moduleId) {
6+
var module = __webpack_module_cache__[moduleId] = {
7+
id: moduleId,
8+
loaded: !1,
9+
exports: {}
10+
};
11+
return __webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__), module.loaded = !0, module.exports;
12+
}("webpack/container/entry/specific_page");
13+
}();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"defaults": true,
3+
"toplevel": false,
4+
"passes": 0
5+
}

crates/swc_ecma_usage_analyzer/src/marks.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(dead_code)]
22

3-
use swc_common::Mark;
3+
use swc_common::{Mark, SyntaxContext};
44

55
#[derive(Debug, Clone, Copy)]
66
pub struct Marks {
@@ -47,6 +47,8 @@ pub struct Marks {
4747
/// preserve the side effects.
4848
pub fake_block: Mark,
4949

50+
pub top_level_ctxt: SyntaxContext,
51+
5052
pub unresolved_mark: Mark,
5153
}
5254

@@ -66,6 +68,7 @@ impl Marks {
6668
noinline: m(),
6769
pure: m(),
6870
fake_block: m(),
71+
top_level_ctxt: SyntaxContext::empty().apply_mark(m()),
6972
unresolved_mark: m(),
7073
}
7174
}

0 commit comments

Comments
 (0)