diff --git a/crates/swc_bundler/examples/bundle.rs b/crates/swc_bundler/examples/bundle.rs index e5db4f8a507..e93faa74081 100644 --- a/crates/swc_bundler/examples/bundle.rs +++ b/crates/swc_bundler/examples/bundle.rs @@ -86,6 +86,7 @@ fn do_test(_entry: &Path, entries: HashMap, inline: bool, mini external_modules: Default::default(), disable_fixer: minify, disable_hygiene: minify, + disable_dce: false, module: Default::default(), }, Box::new(Hook), diff --git a/crates/swc_bundler/src/bundler/mod.rs b/crates/swc_bundler/src/bundler/mod.rs index 14c3b9bd9c8..ea926685e92 100644 --- a/crates/swc_bundler/src/bundler/mod.rs +++ b/crates/swc_bundler/src/bundler/mod.rs @@ -36,6 +36,9 @@ pub struct Config { pub disable_fixer: bool, + /// Disable tree-shaking optimization. + pub disable_dce: bool, + /// List of modules which should be preserved. pub external_modules: Vec, diff --git a/crates/swc_bundler/src/bundler/optimize.rs b/crates/swc_bundler/src/bundler/optimize.rs index c729a1f9adb..95481ce7fb6 100644 --- a/crates/swc_bundler/src/bundler/optimize.rs +++ b/crates/swc_bundler/src/bundler/optimize.rs @@ -18,12 +18,12 @@ where if !self.config.disable_inliner { node = node.fold_with(&mut constant_propagation()) } - - node = node.fold_with(&mut Repeat::new(dce::dce(dce::Config { - // TODO(kdy1): Apply mark to wrapped esms and use it at here. - module_mark: None, - }))); - + if !self.config.disable_dce { + node = node.fold_with(&mut Repeat::new(dce::dce(dce::Config { + // TODO(kdy1): Apply mark to wrapped esms and use it at here. + module_mark: None, + }))); + } node }) } diff --git a/crates/swc_bundler/src/bundler/tests.rs b/crates/swc_bundler/src/bundler/tests.rs index db0c87ed96c..f9f5a6af2c6 100644 --- a/crates/swc_bundler/src/bundler/tests.rs +++ b/crates/swc_bundler/src/bundler/tests.rs @@ -134,6 +134,7 @@ impl TestBuilder { disable_inliner: true, disable_hygiene: false, disable_fixer: false, + disable_dce: false, external_modules: vec![], module: Default::default(), },