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
use oxc_compat::EngineTargets;
use rustc_hash::FxHashSet;
pub use oxc_ecmascript::side_effects::PropertyReadSideEffects;
#[derive(Debug, Clone)]
pub struct CompressOptions {
/// Engine targets for feature detection.
///
/// Used to determine which ES features are supported by the target engines
/// and whether transformations can be applied.
///
/// Default: empty (supports all features)
pub target: EngineTargets,
/// Remove `debugger;` statements.
///
/// Default `true`
pub drop_debugger: bool,
/// Remove `console.*` statements.
///
/// Default `false`
pub drop_console: bool,
/// Join consecutive var, let and const statements.
///
/// Default `true`
pub join_vars: bool,
/// Join consecutive simple statements using the comma operator.
///
/// `a; b` -> `a, b`
///
/// Default `true`
pub sequences: bool,
/// Drop unreferenced functions and variables.
pub unused: CompressOptionsUnused,
/// Keep function / class names.
pub keep_names: CompressOptionsKeepNames,
/// Treeshake Options .
/// <https://rollupjs.org/configuration-options/#treeshake>
pub treeshake: TreeShakeOptions,
/// Set of label names to drop from the code.
///
/// Labeled statements matching these names will be removed during minification.
///
/// Default: empty (no labels dropped)
pub drop_labels: FxHashSet<String>,
/// Limit the maximum number of iterations for debugging purpose.
pub max_iterations: Option<u8>,
}
impl Default for CompressOptions {
fn default() -> Self {
Self::smallest()
}
}
impl CompressOptions {
pub fn smallest() -> Self {
Self {
target: EngineTargets::default(),
keep_names: CompressOptionsKeepNames::all_false(),
drop_debugger: true,
drop_console: false,
join_vars: true,
sequences: true,
unused: CompressOptionsUnused::Remove,
treeshake: TreeShakeOptions::default(),
drop_labels: FxHashSet::default(),
max_iterations: None,
}
}
pub fn safest() -> Self {
Self {
target: EngineTargets::default(),
keep_names: CompressOptionsKeepNames::all_true(),
drop_debugger: false,
drop_console: false,
join_vars: true,
sequences: true,
unused: CompressOptionsUnused::Keep,
treeshake: TreeShakeOptions::default(),
drop_labels: FxHashSet::default(),
max_iterations: None,
}
}
pub fn dce() -> Self {
Self {
target: EngineTargets::default(),
keep_names: CompressOptionsKeepNames::all_true(),
drop_debugger: false,
drop_console: false,
join_vars: false,
sequences: false,
unused: CompressOptionsUnused::Remove,
treeshake: TreeShakeOptions::default(),
drop_labels: FxHashSet::default(),
max_iterations: None,
}
}
}
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
pub enum CompressOptionsUnused {
#[default]
Remove,
KeepAssign,
Keep,
}
#[derive(Debug, Clone, Copy, Default)]
pub struct CompressOptionsKeepNames {
/// Keep function names so that `Function.prototype.name` is preserved.
///
/// This does not guarantee that the `undefined` name is preserved.
///
/// Default `false`
pub function: bool,
/// Keep class names so that `Class.prototype.name` is preserved.
///
/// This does not guarantee that the `undefined` name is preserved.
///
/// Default `false`
pub class: bool,
}
impl CompressOptionsKeepNames {
pub fn all_false() -> Self {
Self { function: false, class: false }
}
pub fn all_true() -> Self {
Self { function: true, class: true }
}
pub fn function_only() -> Self {
Self { function: true, class: false }
}
pub fn class_only() -> Self {
Self { function: false, class: true }
}
}
#[derive(Debug, Clone)]
pub struct TreeShakeOptions {
/// Whether to respect the pure annotations.
///
/// Pure annotations are the comments that marks that a expression is pure.
/// For example, `/* @__PURE__ */`, `/* #__NO_SIDE_EFFECTS__ */`.
///
/// <https://rollupjs.org/configuration-options/#treeshake-annotations>
///
/// Default `true`
pub annotations: bool,
/// Whether to treat this function call as pure.
///
/// This function is called for normal function calls, new calls, and
/// tagged template calls (`foo()`, `new Foo()`, ``foo`b` ``).
///
/// <https://rollupjs.org/configuration-options/#treeshake-manualpurefunctions>
pub manual_pure_functions: Vec<String>,
/// Whether property read accesses have side effects.
///
/// <https://rollupjs.org/configuration-options/#treeshake-propertyreadsideeffects>
///
/// Default [PropertyReadSideEffects::All]
pub property_read_side_effects: PropertyReadSideEffects,
/// Whether property write accesses (assignments to member expressions) have side effects.
///
/// When `false`, assignments like `obj.prop = value` are considered side-effect-free
/// (assuming the object and value expressions themselves are side-effect-free).
/// This enables dropping property assignments to unused local variables,
/// e.g. `function A() {} A.from = () => {}` can be eliminated entirely.
///
/// Requires assuming that `Object.prototype` / `Function.prototype` properties
/// are not setters with side effects.
///
/// <https://rolldown.rs/reference/InputOptions.treeshake#propertywritesideeffects>
///
/// Default `true`
pub property_write_side_effects: bool,
/// Whether accessing a global variable has side effects.
///
/// Accessing a non-existing global variable will throw an error.
/// Global variable may be a getter that has side effects.
///
/// <https://rollupjs.org/configuration-options/#treeshake-unknownglobalsideeffects>
///
/// Default `true`
pub unknown_global_side_effects: bool,
/// Whether invalid import statements have side effects.
///
/// Accessing a non-existing import name will throw an error.
/// Also import statements that cannot be resolved will throw an error.
///
/// Default `false`
pub invalid_import_side_effects: bool,
}
impl Default for TreeShakeOptions {
fn default() -> Self {
Self {
annotations: true,
manual_pure_functions: vec![],
property_read_side_effects: PropertyReadSideEffects::default(),
property_write_side_effects: true,
unknown_global_side_effects: true,
invalid_import_side_effects: false,
}
}
}