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
use crate::compress::compressor;
use crate::marks::Marks;
use crate::metadata::info_marker;
use crate::option::ExtraOptions;
use crate::option::MinifyOptions;
use crate::pass::compute_char_freq::compute_char_freq;
use crate::pass::expand_names::name_expander;
use crate::pass::global_defs;
use crate::pass::hygiene::hygiene_optimizer;
pub use crate::pass::hygiene::optimize_hygiene;
use crate::pass::mangle_names::name_mangler;
use crate::pass::mangle_props::mangle_properties;
use crate::pass::precompress::precompress_optimizer;
use crate::util::now;
use analyzer::analyze;
use pass::postcompress::postcompress_optimizer;
use std::time::Instant;
use swc_common::comments::Comments;
use swc_common::sync::Lrc;
use swc_common::SourceMap;
use swc_common::GLOBALS;
use swc_ecma_ast::Module;
use swc_ecma_visit::FoldWith;
use swc_ecma_visit::VisitMutWith;
use timing::Timings;
mod analyzer;
mod compress;
mod debug;
pub mod marks;
mod metadata;
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 {
log::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 {
log::info!("precompress took {:?}", Instant::now() - start);
}
}
m.visit_mut_with(&mut info_marker(comments, marks, extra.top_level_mark));
if options.wrap {
}
if options.enclose {
}
if let Some(ref mut _t) = timings {
}
if options.rename && false {
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)));
if let Some(start) = start {
log::info!("compressor took {:?}", Instant::now() - start);
}
let start = now();
m.visit_mut_with(&mut postcompress_optimizer(options));
if let Some(start) = start {
log::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");
}
{
let data = analyze(&m, None);
m.visit_mut_with(&mut hygiene_optimizer(data, extra.top_level_mark));
}
if let Some(ref mut t) = timings {
t.end_section();
}
m
}