diff --git a/crates/swc_ecma_minifier/src/compress/optimize/unused.rs b/crates/swc_ecma_minifier/src/compress/optimize/unused.rs index a6ae885f44dc..54ccfbad6c87 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/unused.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/unused.rs @@ -711,6 +711,8 @@ impl Optimizer<'_> { && var.usage_count == 0 && var.declared && (!var.declared_as_fn_param || !used_arguments || self.ctx.in_strict) + && (self.options.top_level() + || i.span.ctxt != self.marks.top_level_ctxt) { report_change!( "unused: Dropping assignment to var '{}{:?}', which is never used", diff --git a/crates/swc_ecma_minifier/src/lib.rs b/crates/swc_ecma_minifier/src/lib.rs index 25887ced3a20..ef2d1b67eabe 100644 --- a/crates/swc_ecma_minifier/src/lib.rs +++ b/crates/swc_ecma_minifier/src/lib.rs @@ -100,6 +100,7 @@ pub fn optimize( let _timer = timer!("minify"); let mut marks = Marks::new(); + marks.top_level_ctxt = SyntaxContext::empty().apply_mark(extra.top_level_mark); marks.unresolved_mark = extra.unresolved_mark; debug_assert_valid(&n); diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/7568/1/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/7568/1/input.js new file mode 100644 index 000000000000..df45390ffb16 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/7568/1/input.js @@ -0,0 +1,28 @@ +var specific_microfront; +(function () { // webpackBootstrap + "use strict"; + var __webpack_modules__ = ({ + + + + }); + function __webpack_require__(moduleId) { + var module = __webpack_module_cache__[moduleId] = { + id: moduleId, + loaded: false, + exports: {} + + }; + + __webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__); + module.loaded = true; + return module.exports; + + } + + var __webpack_exports__ = __webpack_require__("webpack/container/entry/specific_page"); + specific_microfront = __webpack_exports__; + + +})() + ; \ No newline at end of file diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/7568/1/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/7568/1/output.js new file mode 100644 index 000000000000..1d5ab9106892 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/7568/1/output.js @@ -0,0 +1,13 @@ +var specific_microfront; +!function() { + "use strict"; + var __webpack_modules__ = {}; + specific_microfront = function __webpack_require__(moduleId) { + var module = __webpack_module_cache__[moduleId] = { + id: moduleId, + loaded: !1, + exports: {} + }; + return __webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__), module.loaded = !0, module.exports; + }("webpack/container/entry/specific_page"); +}(); diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/7568/config.json b/crates/swc_ecma_minifier/tests/fixture/issues/7568/config.json new file mode 100644 index 000000000000..50c2afa6cacd --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/7568/config.json @@ -0,0 +1,5 @@ +{ + "defaults": true, + "toplevel": false, + "passes": 0 +} diff --git a/crates/swc_ecma_usage_analyzer/src/marks.rs b/crates/swc_ecma_usage_analyzer/src/marks.rs index a98f6f3f760f..a904f0db056b 100644 --- a/crates/swc_ecma_usage_analyzer/src/marks.rs +++ b/crates/swc_ecma_usage_analyzer/src/marks.rs @@ -1,6 +1,6 @@ #![allow(dead_code)] -use swc_common::Mark; +use swc_common::{Mark, SyntaxContext}; #[derive(Debug, Clone, Copy)] pub struct Marks { @@ -47,6 +47,8 @@ pub struct Marks { /// preserve the side effects. pub fake_block: Mark, + pub top_level_ctxt: SyntaxContext, + pub unresolved_mark: Mark, } @@ -66,6 +68,7 @@ impl Marks { noinline: m(), pure: m(), fake_block: m(), + top_level_ctxt: SyntaxContext::empty().apply_mark(m()), unresolved_mark: m(), } }