From 811308c3526003608e61c559f43408f6065ff9ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 5 Apr 2024 09:02:51 +0900 Subject: [PATCH] fix(es/minifier): Respect `top_retain` for top-level functions (#8814) --- crates/swc_ecma_minifier/src/compress/optimize/unused.rs | 8 ++++++++ crates/swc_ecma_minifier/tests/TODO.txt | 1 - .../drop_unused/drop_toplevel_all_retain/output.js | 4 +--- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/crates/swc_ecma_minifier/src/compress/optimize/unused.rs b/crates/swc_ecma_minifier/src/compress/optimize/unused.rs index 79ebff51d8f3..eaa2b047f2da 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/unused.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/unused.rs @@ -511,6 +511,14 @@ impl Optimizer<'_> { return; } + if !self.may_remove_ident(ident) { + log_abort!( + "unused: Preserving function `{}` because it's top-level", + ident.sym + ); + return; + } + // If it is not used, drop it. if self .data diff --git a/crates/swc_ecma_minifier/tests/TODO.txt b/crates/swc_ecma_minifier/tests/TODO.txt index cec001dfbdf4..4896a84541ec 100644 --- a/crates/swc_ecma_minifier/tests/TODO.txt +++ b/crates/swc_ecma_minifier/tests/TODO.txt @@ -88,7 +88,6 @@ drop_unused/delete_assign_1/input.js drop_unused/delete_assign_2/input.js drop_unused/double_assign_1/input.js drop_unused/double_assign_2/input.js -drop_unused/drop_toplevel_all_retain/input.js drop_unused/drop_toplevel_funcs/input.js drop_unused/drop_toplevel_funcs_retain/input.js drop_unused/drop_toplevel_keep_assign/input.js diff --git a/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/drop_toplevel_all_retain/output.js b/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/drop_toplevel_all_retain/output.js index 888671e8b76a..b2374bce8bb2 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/drop_toplevel_all_retain/output.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/drop_unused/drop_toplevel_all_retain/output.js @@ -1,8 +1,6 @@ var a; function f(d) { - return function () { - 2; - }; + return function() {}; } a = 2; console.log(3);