swc_ecma_minifier/
lib.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//! JavaScript minifier implemented in rust.
//!
//! # Assumptions
//!
//! Like other minification tools, swc minifier assumes some things about the
//! input code.
//!
//!  - TDZ violation does not exist.
//!
//! In other words, TDZ violation will be ignored.
//!
//!  - Acesssing top-level identifiers do not have side effects.
//!
//! If you declare a variable on `globalThis` using a getter with side-effects,
//! swc minifier will break it.
//!
//! # Debugging
//!
//! In debug build, if you set an environment variable `SWC_CHECK` to `1`, the
//! minifier will check the validity of the syntax using `node --check`
//!
//! # Cargo features
//!
//! ## `debug`
//!
//! If you enable this cargo feature and set the environment variable named
//! `SWC_RUN` to `1`, the minifier will validate the code using node before each
//! step.
#![deny(clippy::all)]
#![allow(clippy::blocks_in_conditions)]
#![allow(clippy::collapsible_else_if)]
#![allow(clippy::collapsible_if)]
#![allow(clippy::ptr_arg)]
#![allow(clippy::vec_box)]
#![allow(clippy::overly_complex_bool_expr)]
#![allow(clippy::mutable_key_type)]
#![allow(clippy::only_used_in_recursion)]
#![allow(unstable_name_collisions)]
#![allow(clippy::match_like_matches_macro)]

use once_cell::sync::Lazy;
use pass::mangle_names::mangle_names;
use swc_common::{comments::Comments, pass::Repeated, sync::Lrc, SourceMap, SyntaxContext};
use swc_ecma_ast::*;
use swc_ecma_transforms_optimization::debug_assert_valid;
use swc_ecma_usage_analyzer::marks::Marks;
use swc_ecma_visit::VisitMutWith;
use swc_timer::timer;

pub use crate::pass::global_defs::globals_defs;
use crate::{
    compress::{compressor, pure_optimizer, PureOptimizerConfig},
    metadata::info_marker,
    mode::{Minification, Mode},
    option::{CompressOptions, ExtraOptions, MinifyOptions},
    pass::{
        global_defs, mangle_names::idents_to_preserve, mangle_props::mangle_properties,
        merge_exports::merge_exports, postcompress::postcompress_optimizer,
        precompress::precompress_optimizer,
    },
    // program_data::ModuleInfo,
    timing::Timings,
    util::base54::CharFreq,
};

#[macro_use]
mod macros;
mod compress;
mod debug;
pub mod eval;
#[doc(hidden)]
pub mod js;
mod metadata;
mod mode;
pub mod option;
mod pass;
mod program_data;
mod size_hint;
pub mod timing;
mod util;

pub mod marks {
    pub use swc_ecma_usage_analyzer::marks::Marks;
}

const DISABLE_BUGGY_PASSES: bool = true;

pub(crate) static CPU_COUNT: Lazy<usize> = Lazy::new(num_cpus::get);
pub(crate) static HEAVY_TASK_PARALLELS: Lazy<usize> = Lazy::new(|| *CPU_COUNT * 8);
pub(crate) static LIGHT_TASK_PARALLELS: Lazy<usize> = Lazy::new(|| *CPU_COUNT * 100);

pub fn optimize(
    mut n: Program,
    _cm: Lrc<SourceMap>,
    comments: Option<&dyn Comments>,
    mut timings: Option<&mut Timings>,
    options: &MinifyOptions,
    extra: &ExtraOptions,
) -> Program {
    let _timer = timer!("minify");

    let mut marks = Marks::new();
    marks.top_level_ctxt = SyntaxContext::empty().apply_mark(extra.top_level_mark);
    marks.unresolved_mark = extra.unresolved_mark;

    debug_assert_valid(&n);

    if let Some(defs) = options.compress.as_ref().map(|c| &c.global_defs) {
        let _timer = timer!("inline global defs");
        // Apply global defs.
        //
        // As terser treats `CONFIG['VALUE']` and `CONFIG.VALUE` differently, we don't
        // have to see if optimized code matches global definition and we can run
        // this at startup.

        if !defs.is_empty() {
            let defs = defs.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
            n.visit_mut_with(&mut global_defs::globals_defs(
                defs,
                extra.unresolved_mark,
                extra.top_level_mark,
            ));
        }
    }

    if let Some(_options) = &options.compress {
        let _timer = timer!("precompress");

        n.visit_mut_with(&mut precompress_optimizer());
        debug_assert_valid(&n);
    }

    if options.compress.is_some() {
        n.visit_mut_with(&mut info_marker(
            options.compress.as_ref(),
            comments,
            marks,
            // extra.unresolved_mark,
        ));
        debug_assert_valid(&n);
    }

    if options.wrap {
        // TODO: wrap_common_js
        // toplevel = toplevel.wrap_commonjs(options.wrap);
    }

    if options.enclose {
        // TODO: enclose
        // toplevel = toplevel.wrap_enclose(options.enclose);
    }
    if let Some(ref mut t) = timings {
        t.section("compress");
    }
    if let Some(options) = &options.compress {
        if options.unused {
            perform_dce(&mut n, options, extra);
            debug_assert_valid(&n);
        }
    }

    // We don't need validation.

    if let Some(ref mut _t) = timings {
        // TODO: store `rename`
    }

    // Noop.
    // https://github.com/mishoo/UglifyJS2/issues/2794
    if options.rename && DISABLE_BUGGY_PASSES {
        // toplevel.figure_out_scope(options.mangle);
        // TODO: Pass `options.mangle` to name expander.
        // n.visit_mut_with(&mut name_expander());
    }

    if let Some(ref mut t) = timings {
        t.section("compress");
    }
    if let Some(c) = &options.compress {
        {
            let _timer = timer!("compress ast");

            n.visit_mut_with(&mut compressor(
                marks,
                c,
                options.mangle.as_ref(),
                &Minification,
            ))
        }

        // Again, we don't need to validate ast

        let _timer = timer!("postcompress");

        n.visit_mut_with(&mut postcompress_optimizer(c));

        let mut pass = 0;
        loop {
            pass += 1;

            let mut v = pure_optimizer(
                c,
                marks,
                PureOptimizerConfig {
                    force_str_for_tpl: Minification.force_str_for_tpl(),
                    enable_join_vars: true,
                    #[cfg(feature = "debug")]
                    debug_infinite_loop: false,
                },
            );
            n.visit_mut_with(&mut v);
            if !v.changed() || c.passes <= pass {
                break;
            }
        }
    }

    if let Some(ref mut _t) = timings {
        // TODO: store `scope`
    }
    if options.mangle.is_some() {
        // toplevel.figure_out_scope(options.mangle);
    }

    if let Some(ref mut t) = timings {
        t.section("mangle");
    }

    if let Some(mangle) = &options.mangle {
        let _timer = timer!("mangle names");
        // TODO: base54.reset();

        let preserved = idents_to_preserve(mangle, marks, &n);

        let chars = CharFreq::compute(
            &n,
            &preserved,
            SyntaxContext::empty().apply_mark(marks.unresolved_mark),
        )
        .compile();

        mangle_names(
            &mut n,
            mangle,
            preserved,
            chars,
            extra.top_level_mark,
            extra.mangle_name_cache.clone(),
        );

        if let Some(property_mangle_options) = &mangle.props {
            mangle_properties(&mut n, property_mangle_options.clone(), chars);
        }
    }

    n.visit_mut_with(&mut merge_exports());

    if let Some(ref mut t) = timings {
        t.section("hygiene");
        t.end_section();
    }

    n
}

fn perform_dce(m: &mut Program, options: &CompressOptions, extra: &ExtraOptions) {
    let _timer = timer!("remove dead code");

    let mut visitor = swc_ecma_transforms_optimization::simplify::dce::dce(
        swc_ecma_transforms_optimization::simplify::dce::Config {
            module_mark: None,
            top_level: options.top_level(),
            top_retain: options.top_retain.clone(),
            preserve_imports_with_side_effects: true,
        },
        extra.unresolved_mark,
    );

    loop {
        #[cfg(feature = "debug")]
        let start = crate::debug::dump(&*m, false);

        m.visit_mut_with(&mut visitor);

        #[cfg(feature = "debug")]
        if visitor.changed() {
            let src = crate::debug::dump(&*m, false);
            tracing::debug!(
                "===== Before DCE =====\n{}\n===== After DCE =====\n{}",
                start,
                src
            );
        }

        if !visitor.changed() {
            break;
        }

        visitor.reset();
    }

    debug_assert_valid(&*m);
}