0
# Minification
1
2
Terser-compatible interfaces for JavaScript minification and code optimization. These types define comprehensive configuration options for reducing bundle sizes and optimizing JavaScript code while maintaining compatibility with Terser configurations.
3
4
## Capabilities
5
6
### Main Minification Options
7
8
Core minification configuration interface compatible with Terser options.
9
10
```typescript { .api }
11
/**
12
* JavaScript minification configuration
13
*/
14
interface JsMinifyOptions {
15
/** Compression optimization options */
16
compress?: TerserCompressOptions | boolean;
17
/** Output formatting options */
18
format?: JsFormatOptions & ToSnakeCaseProperties<JsFormatOptions>;
19
/** Name mangling options */
20
mangle?: TerserMangleOptions | boolean;
21
/** ECMAScript version target */
22
ecma?: TerserEcmaVersion;
23
/** Keep class names during minification */
24
keep_classnames?: boolean;
25
/** Keep function names during minification */
26
keep_fnames?: boolean;
27
/** Module mode detection */
28
module?: boolean | "unknown";
29
/** Safari 10 compatibility */
30
safari10?: boolean;
31
/** Top-level scope processing */
32
toplevel?: boolean;
33
/** Generate source maps */
34
sourceMap?: boolean;
35
/** Output file path for source maps */
36
outputPath?: string;
37
/** Include source content in source maps */
38
inlineSourcesContent?: boolean;
39
}
40
41
type TerserEcmaVersion = 5 | 2015 | 2016 | string | number;
42
```
43
44
### Compression Options
45
46
Detailed compression optimization settings for dead code elimination and code transformation.
47
48
```typescript { .api }
49
/**
50
* Terser-compatible compression options
51
*/
52
interface TerserCompressOptions {
53
/** Transform arguments references */
54
arguments?: boolean;
55
/** Convert arrow functions */
56
arrows?: boolean;
57
/** Optimize boolean contexts */
58
booleans?: boolean;
59
/** Convert booleans to integers where possible */
60
booleans_as_integers?: boolean;
61
/** Collapse single-use variables */
62
collapse_vars?: boolean;
63
/** Optimize comparisons */
64
comparisons?: boolean;
65
/** Optimize computed property access */
66
computed_props?: boolean;
67
/** Optimize conditional expressions */
68
conditionals?: boolean;
69
/** Remove unreachable code */
70
dead_code?: boolean;
71
/** Apply default optimizations */
72
defaults?: boolean;
73
/** Remove directive prologues */
74
directives?: boolean;
75
/** Remove console.* calls */
76
drop_console?: boolean;
77
/** Remove debugger statements */
78
drop_debugger?: boolean;
79
/** ECMAScript version for optimization */
80
ecma?: TerserEcmaVersion;
81
/** Evaluate constant expressions */
82
evaluate?: boolean;
83
/** Parse as expression context */
84
expression?: boolean;
85
/** Global definitions for replacement */
86
global_defs?: any;
87
/** Hoist function declarations */
88
hoist_funs?: boolean;
89
/** Hoist properties */
90
hoist_props?: boolean;
91
/** Hoist var declarations */
92
hoist_vars?: boolean;
93
/** IE8 compatibility */
94
ie8?: boolean;
95
/** Optimize if-return and if-continue */
96
if_return?: boolean;
97
/** Inline function calls (0=disabled, 1=simple, 2=with arguments, 3=always) */
98
inline?: 0 | 1 | 2 | 3;
99
/** Join consecutive var statements */
100
join_vars?: boolean;
101
/** Keep class names */
102
keep_classnames?: boolean;
103
/** Keep function arguments */
104
keep_fargs?: boolean;
105
/** Keep function names */
106
keep_fnames?: boolean;
107
/** Keep Infinity literal */
108
keep_infinity?: boolean;
109
/** Optimize loops */
110
loops?: boolean;
111
/** Negate immediately invoked function expressions */
112
negate_iife?: boolean;
113
/** Number of optimization passes */
114
passes?: number;
115
/** Optimize property access */
116
properties?: boolean;
117
/** Treat getters as pure functions */
118
pure_getters?: any;
119
/** List of pure function names */
120
pure_funcs?: string[];
121
/** Reduce function calls */
122
reduce_funcs?: boolean;
123
/** Reduce variables */
124
reduce_vars?: boolean;
125
/** Optimize sequences */
126
sequences?: any;
127
/** Remove side-effect-free statements */
128
side_effects?: boolean;
129
/** Optimize switch statements */
130
switches?: boolean;
131
/** Top-level retention */
132
top_retain?: any;
133
/** Top-level optimization */
134
toplevel?: any;
135
/** Optimize typeof expressions */
136
typeofs?: boolean;
137
/** Enable unsafe optimizations */
138
unsafe?: boolean;
139
/** Allow unsafe passes */
140
unsafe_passes?: boolean;
141
/** Optimize arrow functions unsafely */
142
unsafe_arrows?: boolean;
143
/** Unsafe comparison optimizations */
144
unsafe_comps?: boolean;
145
/** Unsafe function optimizations */
146
unsafe_function?: boolean;
147
/** Unsafe math optimizations */
148
unsafe_math?: boolean;
149
/** Unsafe symbol optimizations */
150
unsafe_symbols?: boolean;
151
/** Unsafe method optimizations */
152
unsafe_methods?: boolean;
153
/** Unsafe prototype optimizations */
154
unsafe_proto?: boolean;
155
/** Unsafe regex optimizations */
156
unsafe_regexp?: boolean;
157
/** Unsafe undefined optimizations */
158
unsafe_undefined?: boolean;
159
/** Remove unused variables */
160
unused?: boolean;
161
/** Convert const to let */
162
const_to_let?: boolean;
163
/** Module mode */
164
module?: boolean;
165
}
166
```
167
168
### Name Mangling Options
169
170
Configuration for shortening variable and property names to reduce file size.
171
172
```typescript { .api }
173
/**
174
* Name mangling configuration
175
*/
176
interface TerserMangleOptions {
177
/** Property mangling options */
178
props?: TerserManglePropertiesOptions;
179
/** Mangle names in top-level scope */
180
topLevel?: boolean;
181
/** Alias for topLevel (Terser compatibility) */
182
toplevel?: boolean;
183
/** Keep class names */
184
keepClassNames?: boolean;
185
/** Alias for keepClassNames (Terser compatibility) */
186
keep_classnames?: boolean;
187
/** Keep function names */
188
keepFnNames?: boolean;
189
/** Alias for keepFnNames (Terser compatibility) */
190
keep_fnames?: boolean;
191
/** Keep private property names */
192
keepPrivateProps?: boolean;
193
/** Alias for keepPrivateProps (Terser compatibility) */
194
keep_private_props?: boolean;
195
/** IE8 compatibility */
196
ie8?: boolean;
197
/** Safari 10 compatibility */
198
safari10?: boolean;
199
/** Reserved names that should not be mangled */
200
reserved?: string[];
201
}
202
203
/**
204
* Property mangling options (currently empty but extensible)
205
*/
206
interface TerserManglePropertiesOptions { }
207
```
208
209
### Output Formatting Options
210
211
Comprehensive output formatting configuration for controlling code style and presentation.
212
213
```typescript { .api }
214
/**
215
* Output formatting options (mostly Terser-compatible)
216
* Many options are currently no-op but exist for configuration compatibility
217
*/
218
interface JsFormatOptions {
219
/** Convert unicode to ASCII (currently noop) */
220
asciiOnly?: boolean;
221
/** Beautify output (currently noop) */
222
beautify?: boolean;
223
/** Force braces around statements (currently noop) */
224
braces?: boolean;
225
/** Comment preservation strategy */
226
comments?: false | "some" | "all" | { regex: string };
227
/** ECMAScript version for output (currently noop) */
228
ecma?: TerserEcmaVersion;
229
/** Indentation level (currently noop) */
230
indentLevel?: number;
231
/** Starting indentation (currently noop) */
232
indentStart?: number;
233
/** Inline script compatibility (currently noop) */
234
inlineScript?: boolean;
235
/** Keep number formatting (currently noop) */
236
keepNumbers?: number;
237
/** Keep quoted property names (currently noop) */
238
keepQuotedProps?: boolean;
239
/** Maximum line length (currently noop) */
240
maxLineLen?: number | false;
241
/** Output preamble (currently noop) */
242
preamble?: string;
243
/** Quote object keys (currently noop) */
244
quoteKeys?: boolean;
245
/** Quote style preference (currently noop) */
246
quoteStyle?: boolean;
247
/** Preserve annotations (currently noop) */
248
preserveAnnotations?: boolean;
249
/** Safari 10 compatibility (currently noop) */
250
safari10?: boolean;
251
/** Use semicolons (currently noop) */
252
semicolons?: boolean;
253
/** Keep shebang (currently noop) */
254
shebang?: boolean;
255
/** WebKit compatibility (currently noop) */
256
webkit?: boolean;
257
/** Wrap immediately invoked function expressions (currently noop) */
258
wrapIife?: boolean;
259
/** Wrap function arguments (currently noop) */
260
wrapFuncArgs?: boolean;
261
}
262
```
263
264
### Utility Type Helpers
265
266
Type utilities for case conversion and Terser compatibility.
267
268
```typescript { .api }
269
/**
270
* Convert camelCase string to snake_case
271
* @example ToSnakeCase<'indentLevel'> == 'indent_level'
272
*/
273
type ToSnakeCase<T extends string> = T extends `${infer A}${infer B}`
274
? `${A extends Lowercase<A> ? A : `_${Lowercase<A>}`}${ToSnakeCase<B>}`
275
: T;
276
277
/**
278
* Convert object properties from camelCase to snake_case
279
* @example ToSnakeCaseProperties<{indentLevel: 3}> == {indent_level: 3}
280
*/
281
type ToSnakeCaseProperties<T> = {
282
[K in keyof T as K extends string ? ToSnakeCase<K> : K]: T[K];
283
};
284
```
285
286
**Usage Examples:**
287
288
```typescript
289
import type { JsMinifyOptions, TerserCompressOptions, TerserMangleOptions } from "@swc/types";
290
291
// Basic minification setup
292
const basicMinify: JsMinifyOptions = {
293
compress: true,
294
mangle: true
295
};
296
297
// Advanced compression configuration
298
const advancedCompress: TerserCompressOptions = {
299
dead_code: true,
300
drop_console: true,
301
drop_debugger: true,
302
evaluate: true,
303
collapse_vars: true,
304
reduce_vars: true,
305
pure_funcs: ["console.log", "console.warn"],
306
passes: 2
307
};
308
309
// Detailed minification with custom settings
310
const productionMinify: JsMinifyOptions = {
311
compress: {
312
dead_code: true,
313
drop_console: true,
314
drop_debugger: true,
315
collapse_vars: true,
316
reduce_vars: true,
317
evaluate: true,
318
inline: 2,
319
passes: 3,
320
pure_funcs: [
321
"console.log",
322
"console.info",
323
"console.debug",
324
"console.warn"
325
]
326
},
327
mangle: {
328
topLevel: true,
329
keepClassNames: false,
330
keepFnNames: false,
331
reserved: ["$", "jQuery", "React", "ReactDOM"]
332
},
333
format: {
334
comments: false,
335
semicolons: true
336
},
337
ecma: 2020,
338
module: true,
339
sourceMap: true
340
};
341
342
// Development-friendly minification
343
const devMinify: JsMinifyOptions = {
344
compress: {
345
dead_code: true,
346
drop_debugger: true,
347
// Keep console logs in development
348
drop_console: false
349
},
350
mangle: false, // Don't mangle names for debugging
351
format: {
352
comments: "some",
353
beautify: true
354
},
355
sourceMap: true
356
};
357
358
// Library-specific minification (preserve public API)
359
const libraryMinify: JsMinifyOptions = {
360
compress: {
361
dead_code: true,
362
evaluate: true,
363
collapse_vars: true
364
},
365
mangle: {
366
// Don't mangle top-level names (public API)
367
topLevel: false,
368
keepClassNames: true,
369
keepFnNames: true
370
},
371
format: {
372
comments: /^!/ // Keep license comments
373
}
374
};
375
```