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
pub use crate::pass::unique_scope::unique_scope;
use crate::{
compress::compressor,
marks::Marks,
metadata::info_marker,
option::{ExtraOptions, MinifyOptions},
pass::{
compute_char_freq::compute_char_freq, expand_names::name_expander, global_defs,
mangle_names::name_mangler, mangle_props::mangle_properties,
precompress::precompress_optimizer,
},
util::now,
};
use mode::Minification;
use pass::postcompress::postcompress_optimizer;
use std::time::Instant;
use swc_common::{comments::Comments, sync::Lrc, SourceMap, GLOBALS};
use swc_ecma_ast::Module;
use swc_ecma_visit::{FoldWith, VisitMutWith};
use timing::Timings;
mod analyzer;
mod compress;
mod debug;
pub mod eval;
pub mod marks;
mod metadata;
mod mode;
pub mod option;
mod pass;
pub mod timing;
mod util;
const DISABLE_BUGGY_PASSES: bool = true;
const MAX_PAR_DEPTH: u8 = 3;
#[inline]
pub fn optimize(
mut m: Module,
_cm: Lrc<SourceMap>,
comments: Option<&dyn Comments>,
mut timings: Option<&mut Timings>,
options: &MinifyOptions,
extra: &ExtraOptions,
) -> Module {
let marks = Marks::new();
let start = now();
if let Some(defs) = options.compress.as_ref().map(|c| &c.global_defs) {
if !defs.is_empty() {
let defs = defs.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
m.visit_mut_with(&mut global_defs::globals_defs(defs, extra.top_level_mark));
}
}
if let Some(start) = start {
tracing::info!("global_defs took {:?}", Instant::now() - start);
}
if let Some(options) = &options.compress {
let start = now();
m.visit_mut_with(&mut precompress_optimizer(options, marks));
if let Some(start) = start {
tracing::info!("precompress took {:?}", Instant::now() - start);
}
}
m.visit_mut_with(&mut info_marker(comments, marks, extra.top_level_mark));
m.visit_mut_with(&mut unique_scope());
if options.wrap {
}
if options.enclose {
}
if let Some(ref mut _t) = timings {
}
if options.rename && DISABLE_BUGGY_PASSES {
m.visit_mut_with(&mut name_expander());
}
if let Some(ref mut t) = timings {
t.section("compress");
}
if let Some(options) = &options.compress {
let start = now();
m = GLOBALS
.with(|globals| m.fold_with(&mut compressor(globals, marks, &options, &Minification)));
if let Some(start) = start {
tracing::info!("compressor took {:?}", Instant::now() - start);
}
let start = now();
m.visit_mut_with(&mut postcompress_optimizer(options));
if let Some(start) = start {
tracing::info!("postcompressor took {:?}", Instant::now() - start);
}
}
if let Some(ref mut _t) = timings {
}
if options.mangle.is_some() {
}
if let Some(ref mut t) = timings {
t.section("mangle");
}
if let Some(mangle) = &options.mangle {
let char_freq_info = compute_char_freq(&m);
m.visit_mut_with(&mut name_mangler(mangle.clone(), char_freq_info, marks));
}
if let Some(property_mangle_options) = options.mangle.as_ref().and_then(|o| o.props.as_ref()) {
mangle_properties(&mut m, property_mangle_options.clone());
}
if let Some(ref mut t) = timings {
t.section("hygiene");
}
if let Some(ref mut t) = timings {
t.end_section();
}
m
}