From d800dfc5b6f858e783da89b8fb5b811f815f74c5 Mon Sep 17 00:00:00 2001 From: pshu Date: Sat, 14 Sep 2024 14:21:23 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=F0=9F=90=9B=20deconstructing=20need?= =?UTF-8?q?s=20object=20spread=20transform=20first?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/plugins/tree_shaking/remove_useless_stmts.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/crates/mako/src/plugins/tree_shaking/remove_useless_stmts.rs b/crates/mako/src/plugins/tree_shaking/remove_useless_stmts.rs index 6947b0157..dde449438 100644 --- a/crates/mako/src/plugins/tree_shaking/remove_useless_stmts.rs +++ b/crates/mako/src/plugins/tree_shaking/remove_useless_stmts.rs @@ -7,7 +7,8 @@ use swc_core::ecma::ast::{ Module, ModuleExportName, }; use swc_core::ecma::transforms::compat::es2015::destructuring; -use swc_core::ecma::visit::{Fold, VisitMut, VisitMutWith, VisitWith}; +use swc_core::ecma::transforms::compat::es2018::object_rest_spread; +use swc_core::ecma::visit::{VisitMut, VisitMutWith, VisitWith}; use super::collect_explicit_prop::IdExplicitPropAccessCollector; use crate::plugins::tree_shaking::module::TreeShakeModule; @@ -268,8 +269,12 @@ fn optimize_import_namespace(import_infos: &mut [ImportInfo], module: &mut Modul if !ids.is_empty() { let mut v = IdExplicitPropAccessCollector::new(ids); - let destucturing_module = destructuring(Default::default()).fold_module(module.clone()); - destucturing_module.visit_with(&mut v); + let mut shadow = module.clone(); + + shadow.visit_mut_with(&mut object_rest_spread(Default::default())); + shadow.visit_mut_with(&mut destructuring(Default::default())); + shadow.visit_with(&mut v); + let explicit_prop_accessed_ids = v.explicit_accessed_props(); import_infos.iter_mut().for_each(|ii| { From 0ae9e8041916469c97aca4535b1feebba8e52c4f Mon Sep 17 00:00:00 2001 From: pshu Date: Sat, 14 Sep 2024 14:41:31 +0800 Subject: [PATCH 2/2] =?UTF-8?q?test:=20=E2=9C=85=20add=20guardian=20syntax?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tree-shaking.import_namespace/src/index.tsx | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/e2e/fixtures/tree-shaking.import_namespace/src/index.tsx b/e2e/fixtures/tree-shaking.import_namespace/src/index.tsx index 71f1e1a90..cfba35f0a 100644 --- a/e2e/fixtures/tree-shaking.import_namespace/src/index.tsx +++ b/e2e/fixtures/tree-shaking.import_namespace/src/index.tsx @@ -5,3 +5,17 @@ const { shouldKeep2 } = mod console.log(mod.shouldKeep1(42)); console.log(shouldKeep2(42)) +// Guardian don't remove +let array= [1,2,3,] +console.log(array) +let [first, ...rest] = array +console.log(first,rest) +let copiedArray = [...array] +console.log(copiedArray) + +let object = {a: 1,b: 2,c: 3} +console.log(object) +let {a, ...restObject} = object +console.log(a,restObject) +let copiedObject = {...object} +console.log(copiedObject)