0
# Minification Functions
1
2
Built-in minification implementations for different JavaScript minifiers, each with specific optimization capabilities and performance characteristics.
3
4
## Capabilities
5
6
### Terser Minify
7
8
Default minification function using the Terser JavaScript minifier for comprehensive code optimization.
9
10
```javascript { .api }
11
/**
12
* Minifies JavaScript using Terser
13
* @param input - Object mapping filenames to source code
14
* @param sourceMap - Optional source map input
15
* @param minimizerOptions - Terser-specific options
16
* @param extractComments - Comment extraction configuration
17
* @returns Promise resolving to minification result
18
*/
19
static terserMinify(
20
input: Input,
21
sourceMap?: SourceMapInput,
22
minimizerOptions?: TerserOptions,
23
extractComments?: ExtractCommentsOptions
24
): Promise<MinimizedResult>;
25
```
26
27
**Usage Examples:**
28
29
```javascript
30
const TerserPlugin = require('terser-webpack-plugin');
31
32
// Using default terser minifier (implicit)
33
new TerserPlugin();
34
35
// Explicit terser configuration
36
new TerserPlugin({
37
minify: TerserPlugin.terserMinify,
38
terserOptions: {
39
compress: {
40
drop_console: true,
41
drop_debugger: true,
42
pure_funcs: ['console.log'],
43
},
44
mangle: {
45
reserved: ['$', 'jQuery'],
46
},
47
format: {
48
comments: false,
49
},
50
},
51
});
52
53
// Direct function usage
54
const result = await TerserPlugin.terserMinify(
55
{ 'main.js': 'function test() { console.log("hello"); }' },
56
undefined,
57
{ compress: { drop_console: true } }
58
);
59
```
60
61
### UglifyJS Minify
62
63
Minification function using UglifyJS for JavaScript code compression and optimization.
64
65
```javascript { .api }
66
/**
67
* Minifies JavaScript using UglifyJS
68
* @param input - Object mapping filenames to source code
69
* @param sourceMap - Optional source map input
70
* @param minimizerOptions - UglifyJS-specific options
71
* @param extractComments - Comment extraction configuration
72
* @returns Promise resolving to minification result
73
*/
74
static uglifyJsMinify(
75
input: Input,
76
sourceMap?: SourceMapInput,
77
minimizerOptions?: UglifyJSOptions,
78
extractComments?: ExtractCommentsOptions
79
): Promise<MinimizedResult>;
80
```
81
82
**Usage Examples:**
83
84
```javascript
85
const TerserPlugin = require('terser-webpack-plugin');
86
87
// Using UglifyJS minifier
88
new TerserPlugin({
89
minify: TerserPlugin.uglifyJsMinify,
90
terserOptions: {
91
compress: {
92
drop_console: true,
93
},
94
mangle: true,
95
output: {
96
beautify: false,
97
comments: false,
98
},
99
},
100
});
101
```
102
103
### SWC Minify
104
105
Minification function using SWC (Speedy Web Compiler) for fast JavaScript/TypeScript minification.
106
107
```javascript { .api }
108
/**
109
* Minifies JavaScript using SWC
110
* @param input - Object mapping filenames to source code
111
* @param sourceMap - Optional source map input
112
* @param minimizerOptions - SWC-specific options
113
* @returns Promise resolving to minification result
114
*/
115
static swcMinify(
116
input: Input,
117
sourceMap?: SourceMapInput,
118
minimizerOptions?: SWCOptions
119
): Promise<MinimizedResult>;
120
```
121
122
**Usage Examples:**
123
124
```javascript
125
const TerserPlugin = require('terser-webpack-plugin');
126
127
// Using SWC minifier
128
new TerserPlugin({
129
minify: TerserPlugin.swcMinify,
130
terserOptions: {
131
compress: true,
132
mangle: true,
133
},
134
});
135
```
136
137
### ESBuild Minify
138
139
Minification function using ESBuild for extremely fast JavaScript/TypeScript minification.
140
141
```javascript { .api }
142
/**
143
* Minifies JavaScript using ESBuild
144
* @param input - Object mapping filenames to source code
145
* @param sourceMap - Optional source map input
146
* @param minimizerOptions - ESBuild-specific options
147
* @returns Promise resolving to minification result
148
*/
149
static esbuildMinify(
150
input: Input,
151
sourceMap?: SourceMapInput,
152
minimizerOptions?: ESBuildOptions
153
): Promise<MinimizedResult>;
154
```
155
156
**Usage Examples:**
157
158
```javascript
159
const TerserPlugin = require('terser-webpack-plugin');
160
161
// Using ESBuild minifier
162
new TerserPlugin({
163
minify: TerserPlugin.esbuildMinify,
164
terserOptions: {
165
target: 'es2018',
166
minify: true,
167
legalComments: 'none',
168
},
169
});
170
```
171
172
### Custom Minify Function
173
174
Create custom minification implementations by following the minifier interface.
175
176
```javascript { .api }
177
/**
178
* Custom minification function interface
179
* @param input - Object mapping filenames to source code
180
* @param sourceMap - Optional source map input
181
* @param minifyOptions - Custom minifier options
182
* @param extractComments - Comment extraction configuration
183
* @returns Promise resolving to minification result
184
*/
185
interface MinimizerImplementation<T> {
186
(
187
input: Input,
188
sourceMap: SourceMapInput | undefined,
189
minifyOptions: MinimizerOptions<T>,
190
extractComments?: ExtractCommentsOptions
191
): Promise<MinimizedResult>;
192
getMinimizerVersion?(): string | undefined;
193
supportsWorkerThreads?(): boolean | undefined;
194
}
195
```
196
197
**Usage Examples:**
198
199
```javascript
200
const TerserPlugin = require('terser-webpack-plugin');
201
202
// Custom minifier function
203
const customMinify = async (input, sourceMap, options) => {
204
// Custom minification logic
205
const [[filename, code]] = Object.entries(input);
206
207
// Your custom minification implementation
208
const minifiedCode = yourCustomMinifier(code, options);
209
210
return {
211
code: minifiedCode,
212
map: sourceMap,
213
extractedComments: [],
214
};
215
};
216
217
// Add version and worker support info
218
customMinify.getMinimizerVersion = () => '1.0.0';
219
customMinify.supportsWorkerThreads = () => true;
220
221
// Use custom minifier
222
new TerserPlugin({
223
minify: customMinify,
224
terserOptions: {
225
// Your custom options
226
level: 'aggressive',
227
},
228
});
229
```
230
231
## Minifier Comparison
232
233
### Worker Thread Support
234
235
Different minifiers have different worker thread capabilities:
236
237
```javascript { .api }
238
// Worker thread support by minifier
239
TerserPlugin.terserMinify.supportsWorkerThreads(); // true
240
TerserPlugin.uglifyJsMinify.supportsWorkerThreads(); // true
241
TerserPlugin.swcMinify.supportsWorkerThreads(); // false
242
TerserPlugin.esbuildMinify.supportsWorkerThreads(); // false
243
```
244
245
### Version Information
246
247
Get version information for installed minifiers:
248
249
```javascript { .api }
250
// Get minifier versions
251
TerserPlugin.terserMinify.getMinimizerVersion(); // e.g., "5.31.1"
252
TerserPlugin.uglifyJsMinify.getMinimizerVersion(); // e.g., "3.18.0"
253
TerserPlugin.swcMinify.getMinimizerVersion(); // e.g., "1.3.102"
254
TerserPlugin.esbuildMinify.getMinimizerVersion(); // e.g., "0.19.11"
255
```
256
257
## Types
258
259
```javascript { .api }
260
interface Input {
261
[file: string]: string;
262
}
263
264
interface MinimizedResult {
265
code: string;
266
map?: SourceMapInput;
267
errors?: (string | Error)[];
268
warnings?: (string | Error)[];
269
extractedComments?: string[];
270
}
271
272
type MinimizerOptions<T> = PredefinedOptions<T> & T;
273
274
interface PredefinedOptions<T> {
275
module?: boolean | string;
276
ecma?: number | string;
277
}
278
279
type SourceMapInput = any; // From @jridgewell/trace-mapping
280
281
type ExtractCommentsOptions =
282
| boolean
283
| string
284
| RegExp
285
| ExtractCommentsFunction
286
| ExtractCommentsObject;
287
```