diff --git a/crates/swc_ecma_minifier/src/compress/optimize/mod.rs b/crates/swc_ecma_minifier/src/compress/optimize/mod.rs index fac088c84fb1..f9f5ce88db9a 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/mod.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/mod.rs @@ -318,6 +318,10 @@ impl From<&Function> for FnMetadata { impl Optimizer<'_> { fn may_remove_ident(&self, id: &Ident) -> bool { + if self.ctx.is_exported { + return false; + } + if id.span.ctxt != self.marks.top_level_ctxt { return true; } diff --git a/crates/swc_ecma_minifier/tests/fixture/pr/7856/1/input.js b/crates/swc_ecma_minifier/tests/fixture/pr/7856/1/input.js new file mode 100644 index 000000000000..4795623c1d0d --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/pr/7856/1/input.js @@ -0,0 +1,4 @@ +const a = () => ""; +const b = {}; +export const c = a; +b.c = c; diff --git a/crates/swc_ecma_minifier/tests/fixture/pr/7856/1/output.js b/crates/swc_ecma_minifier/tests/fixture/pr/7856/1/output.js new file mode 100644 index 000000000000..569c1de79f0a --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/pr/7856/1/output.js @@ -0,0 +1 @@ +export const c = ()=>""; diff --git a/crates/swc_ecma_minifier/tests/fixture/pr/7856/2/input.js b/crates/swc_ecma_minifier/tests/fixture/pr/7856/2/input.js new file mode 100644 index 000000000000..aa94f2e4fa30 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/pr/7856/2/input.js @@ -0,0 +1,5 @@ +export const a = 4; +export const b = 16; +export const c = 5; + +export const d = () => a; diff --git a/crates/swc_ecma_minifier/tests/fixture/pr/7856/2/output.js b/crates/swc_ecma_minifier/tests/fixture/pr/7856/2/output.js new file mode 100644 index 000000000000..54cef1e9fdd8 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/pr/7856/2/output.js @@ -0,0 +1,4 @@ +export const a = 4; +export const b = 16; +export const c = 5; +export const d = ()=>4;