swc_bundler/bundler/
optimize.rs

1use swc_common::pass::Repeat;
2use swc_ecma_ast::*;
3use swc_ecma_transforms_optimization::simplify::{const_propagation::constant_propagation, dce};
4use swc_ecma_visit::VisitMutWith;
5
6use crate::{Bundler, Load, Resolve};
7
8impl<L, R> Bundler<'_, L, R>
9where
10    L: Load,
11    R: Resolve,
12{
13    /// If used_exports is [None], all exports are treated as exported.
14    ///
15    /// Note: Context of used_exports is ignored, as the specifiers comes from
16    /// other module.
17    pub(super) fn optimize(&self, mut node: Module) -> Module {
18        self.run(|| {
19            if !self.config.disable_inliner {
20                node.visit_mut_with(&mut constant_propagation())
21            }
22            if !self.config.disable_dce {
23                node.visit_mut_with(&mut Repeat::new(dce::dce(
24                    dce::Config {
25                        // TODO(kdy1): Apply mark to wrapped esms and use it at here.
26                        module_mark: None,
27                        top_level: true,
28                        top_retain: Default::default(),
29                        preserve_imports_with_side_effects: true,
30                    },
31                    self.unresolved_mark,
32                )));
33            }
34            node
35        })
36    }
37}