swc_ecma_minifier/compress/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use std::borrow::Cow;
#[cfg(feature = "debug")]
use std::thread;

#[cfg(feature = "pretty_assertions")]
use pretty_assertions::assert_eq;
use swc_common::pass::{CompilerPass, Repeated};
use swc_ecma_ast::*;
use swc_ecma_usage_analyzer::marks::Marks;
use swc_ecma_visit::VisitMutWith;
#[cfg(debug_assertions)]
use swc_ecma_visit::VisitWith;
use swc_timer::timer;
use tracing::debug;

pub(crate) use self::pure::{pure_optimizer, PureOptimizerConfig};
use self::{hoist_decls::DeclHoisterConfig, optimize::optimizer};
#[cfg(debug_assertions)]
use crate::debug::AssertValid;
use crate::{
    compress::hoist_decls::decl_hoister,
    debug::dump,
    mode::Mode,
    option::{CompressOptions, MangleOptions},
    program_data::analyze,
    util::force_dump_program,
};

mod hoist_decls;
mod optimize;
mod pure;
mod util;

pub(crate) fn compressor<'a, M>(
    marks: Marks,
    options: &'a CompressOptions,
    mangle_options: Option<&'a MangleOptions>,
    mode: &'a M,
) -> impl 'a + Pass
where
    M: Mode,
{
    Compressor {
        marks,
        options,
        mangle_options,
        changed: false,
        pass: 1,
        mode,
    }
}

struct Compressor<'a> {
    marks: Marks,
    options: &'a CompressOptions,
    mangle_options: Option<&'a MangleOptions>,
    changed: bool,
    pass: usize,

    mode: &'a dyn Mode,
}

impl CompilerPass for Compressor<'_> {
    fn name(&self) -> Cow<'static, str> {
        "compressor".into()
    }
}

impl Pass for Compressor<'_> {
    fn process(&mut self, program: &mut Program) {
        self.optimize_unit_repeatedly(program);
    }
}

impl Compressor<'_> {
    fn optimize_unit_repeatedly(&mut self, n: &mut Program) {
        trace_op!(
            "Optimizing a compile unit within `{:?}`",
            thread::current().name()
        );

        if self.options.hoist_vars || self.options.hoist_fns {
            let data = analyze(&*n, Some(self.marks));

            let mut v = decl_hoister(
                DeclHoisterConfig {
                    hoist_fns: self.options.hoist_fns,
                    hoist_vars: self.options.hoist_vars,
                    _top_level: self.options.top_level(),
                },
                &data,
            );
            n.visit_mut_with(&mut v);
            self.changed |= v.changed();
        }

        loop {
            self.changed = false;
            self.optimize_unit(n);
            self.pass += 1;
            if !self.changed {
                break;
            }
        }

        self.pass = 1;
        // let last_mark = n.remove_mark();
        // assert!(
        //     N::is_module() || last_mark == self.marks.standalone,
        //     "{:?}; last={:?}",
        //     self.marks,
        //     last_mark
        // );
    }

    /// Optimize a module. `N` can be [Module] or [FnExpr].
    fn optimize_unit(&mut self, n: &mut Program) {
        let _timer = timer!("optimize", pass = self.pass);

        if self.options.passes != 0 && self.options.passes < self.pass {
            let done = dump(&*n, false);
            debug!("===== Done =====\n{}", done);
            return;
        }

        // This exists to prevent hanging.
        if self.pass > 200 {
            let code = force_dump_program(n);

            panic!(
                "Infinite loop detected (current pass = {})\n{}",
                self.pass, code
            );
        }

        #[cfg(feature = "debug")]
        let start = {
            let start = force_dump_program(n);
            debug!("===== Start =====\n{}", start);
            start
        };

        {
            let _timer = timer!("apply pure optimizer");

            let mut visitor = pure_optimizer(
                self.options,
                self.marks,
                PureOptimizerConfig {
                    enable_join_vars: self.pass > 1,
                    force_str_for_tpl: self.mode.force_str_for_tpl(),
                },
            );
            n.visit_mut_with(&mut visitor);

            self.changed |= visitor.changed();

            #[cfg(feature = "debug")]
            if visitor.changed() {
                let src = force_dump_program(n);
                debug!(
                    "===== Before pure =====\n{}\n===== After pure =====\n{}",
                    start, src
                );
            }
        }

        #[cfg(debug_assertions)]
        {
            n.visit_with(&mut AssertValid);
        }

        {
            let _timer = timer!("apply full optimizer");

            let mut data = analyze(&*n, Some(self.marks));

            // TODO: reset_opt_flags
            //
            // This is swc version of `node.optimize(this);`.

            let mut visitor = optimizer(
                self.marks,
                self.options,
                self.mangle_options,
                &mut data,
                self.mode,
            );
            n.visit_mut_with(&mut visitor);

            self.changed |= visitor.changed();

            // let done = dump(&*n);
            // debug!("===== Result =====\n{}", done);
        }
    }
}