0
# Plugin Configuration
1
2
Core webpack plugin functionality with comprehensive configuration options for file matching, parallel processing, and comment extraction.
3
4
## Capabilities
5
6
### TerserPlugin Constructor
7
8
Creates a new instance of the Terser webpack plugin with specified configuration options.
9
10
```javascript { .api }
11
/**
12
* Creates a new TerserPlugin instance
13
* @param options - Plugin configuration options
14
*/
15
constructor(options?: PluginOptions);
16
17
interface PluginOptions {
18
/** Test to match files against (default: /\.[cm]?js(\?.*)?$/i) */
19
test?: Rules;
20
/** Include all modules matching any of these conditions */
21
include?: Rules;
22
/** Exclude all modules matching any of these conditions */
23
exclude?: Rules;
24
/** Use multi-process parallel running (default: true) */
25
parallel?: boolean | number;
26
/** Comment extraction configuration (default: true) */
27
extractComments?: ExtractCommentsOptions;
28
/** Custom minify function (default: TerserPlugin.terserMinify) */
29
minify?: MinimizerImplementation;
30
/** Options for the minifier (default: {}) */
31
terserOptions?: MinimizerOptions;
32
}
33
34
type Rules = RegExp | string | Array<RegExp | string>;
35
```
36
37
**Usage Examples:**
38
39
```javascript
40
const TerserPlugin = require('terser-webpack-plugin');
41
42
// Basic configuration
43
new TerserPlugin();
44
45
// File filtering configuration
46
new TerserPlugin({
47
test: /\.js$/i,
48
include: /\/src/,
49
exclude: /node_modules/,
50
});
51
52
// Parallel processing configuration
53
new TerserPlugin({
54
parallel: 4, // Use 4 worker processes
55
});
56
57
// Custom minifier configuration
58
new TerserPlugin({
59
minify: TerserPlugin.swcMinify,
60
terserOptions: {
61
compress: true,
62
mangle: true,
63
},
64
});
65
```
66
67
### Apply Method
68
69
Required webpack plugin method that registers the plugin with webpack's compilation hooks.
70
71
```javascript { .api }
72
/**
73
* Applies the plugin to the webpack compiler
74
* @param compiler - Webpack compiler instance
75
*/
76
apply(compiler: Compiler): void;
77
```
78
79
### File Matching Options
80
81
Configure which files the plugin should process using test, include, and exclude patterns.
82
83
```javascript { .api }
84
interface FileMatchingOptions {
85
/** Test pattern to match files against */
86
test?: Rules;
87
/** Include pattern for files to process */
88
include?: Rules;
89
/** Exclude pattern for files to skip */
90
exclude?: Rules;
91
}
92
93
type Rules = RegExp | string | Array<RegExp | string>;
94
```
95
96
**Usage Examples:**
97
98
```javascript
99
// Match JavaScript and TypeScript files
100
new TerserPlugin({
101
test: /\.(js|ts)$/i,
102
});
103
104
// Include only specific directories
105
new TerserPlugin({
106
include: [/\/src/, /\/lib/],
107
});
108
109
// Exclude node_modules and test files
110
new TerserPlugin({
111
exclude: [/node_modules/, /\.test\./],
112
});
113
114
// Combined patterns
115
new TerserPlugin({
116
test: /\.js$/i,
117
include: /\/src/,
118
exclude: /\.spec\.js$/,
119
});
120
```
121
122
### Parallel Processing Configuration
123
124
Configure multi-process parallel running to improve build speed.
125
126
```javascript { .api }
127
interface ParallelOptions {
128
/** Enable/disable parallel processing or set worker count */
129
parallel?: boolean | number;
130
}
131
```
132
133
**Usage Examples:**
134
135
```javascript
136
// Enable parallel processing (default: true)
137
new TerserPlugin({
138
parallel: true,
139
});
140
141
// Disable parallel processing
142
new TerserPlugin({
143
parallel: false,
144
});
145
146
// Set specific number of workers
147
new TerserPlugin({
148
parallel: 4,
149
});
150
```
151
152
### Comment Extraction Configuration
153
154
Configure how comments are extracted and preserved during minification.
155
156
```javascript { .api }
157
type ExtractCommentsOptions =
158
| boolean
159
| string
160
| RegExp
161
| ExtractCommentsFunction
162
| ExtractCommentsObject;
163
164
interface ExtractCommentsObject {
165
/** Condition to determine which comments to extract */
166
condition?: ExtractCommentsCondition;
167
/** Filename pattern for extracted comments file */
168
filename?: string | ((fileData: any) => string);
169
/** Banner text added to minified file */
170
banner?: string | boolean | ((commentsFile: string) => string);
171
}
172
173
type ExtractCommentsCondition =
174
| boolean
175
| 'all'
176
| 'some'
177
| RegExp
178
| ExtractCommentsFunction;
179
180
interface ExtractCommentsFunction {
181
(
182
astNode: any,
183
comment: {
184
value: string;
185
type: 'comment1' | 'comment2' | 'comment3' | 'comment4';
186
pos: number;
187
line: number;
188
col: number;
189
}
190
): boolean;
191
}
192
```
193
194
**Usage Examples:**
195
196
```javascript
197
// Extract all comments (default)
198
new TerserPlugin({
199
extractComments: true,
200
});
201
202
// Don't extract comments
203
new TerserPlugin({
204
extractComments: false,
205
});
206
207
// Extract comments matching pattern
208
new TerserPlugin({
209
extractComments: /^\**!|@preserve|@license|@cc_on/i,
210
});
211
212
// Custom extraction configuration
213
new TerserPlugin({
214
extractComments: {
215
condition: 'some',
216
filename: '[file].LICENSE.txt',
217
banner: (licenseFile) => {
218
return `License information can be found in ${licenseFile}`;
219
},
220
},
221
});
222
223
// Custom extraction function
224
new TerserPlugin({
225
extractComments: (astNode, comment) => {
226
// Extract comments containing "license" or "copyright"
227
return /@license|@copyright/i.test(comment.value);
228
},
229
});
230
```
231
232
### Minifier Options Configuration
233
234
Configure options passed to the selected minifier.
235
236
```javascript { .api }
237
interface MinimizerConfiguration {
238
/** Custom minify function */
239
minify?: MinimizerImplementation;
240
/** Options for the minifier */
241
terserOptions?: MinimizerOptions;
242
}
243
244
interface MinimizerImplementation<T> {
245
(
246
input: Input,
247
sourceMap: SourceMapInput | undefined,
248
minifyOptions: MinimizerOptions<T>,
249
extractComments?: ExtractCommentsOptions
250
): Promise<MinimizedResult>;
251
getMinimizerVersion?(): string | undefined;
252
supportsWorkerThreads?(): boolean | undefined;
253
}
254
255
type MinimizerOptions<T> = PredefinedOptions<T> & T;
256
257
interface PredefinedOptions<T> {
258
/** Module format handling */
259
module?: boolean | string;
260
/** ECMAScript target version */
261
ecma?: number | string;
262
}
263
```
264
265
**Usage Examples:**
266
267
```javascript
268
// Terser-specific options
269
new TerserPlugin({
270
terserOptions: {
271
compress: {
272
drop_console: true,
273
drop_debugger: true,
274
},
275
mangle: {
276
reserved: ['$', 'jQuery'],
277
},
278
format: {
279
comments: false,
280
},
281
},
282
});
283
284
// Using different minifier
285
new TerserPlugin({
286
minify: TerserPlugin.esbuildMinify,
287
terserOptions: {
288
target: 'es2018',
289
minify: true,
290
legalComments: 'none',
291
},
292
});
293
```