0
# Terser Webpack Plugin
1
2
Terser Webpack Plugin integrates the Terser JavaScript minifier into webpack's optimization pipeline, enabling JavaScript code compression and optimization during the build process. It supports multiple minification engines (terser, uglify-js, swc, esbuild) with extensive configuration options for production builds.
3
4
## Package Information
5
6
- **Package Name**: terser-webpack-plugin
7
- **Package Type**: npm
8
- **Language**: JavaScript/TypeScript
9
- **Installation**: `npm install terser-webpack-plugin --save-dev`
10
11
## Core Imports
12
13
```javascript
14
const TerserPlugin = require('terser-webpack-plugin');
15
```
16
17
For ESM environments:
18
19
```javascript
20
import TerserPlugin from 'terser-webpack-plugin';
21
```
22
23
## Basic Usage
24
25
```javascript
26
const TerserPlugin = require('terser-webpack-plugin');
27
28
module.exports = {
29
optimization: {
30
minimize: true,
31
minimizer: [
32
new TerserPlugin({
33
test: /\.js(\?.*)?$/i,
34
parallel: true,
35
extractComments: true,
36
terserOptions: {
37
compress: {
38
drop_console: true,
39
},
40
mangle: true,
41
},
42
}),
43
],
44
},
45
};
46
```
47
48
## Architecture
49
50
Terser Webpack Plugin is built around several key components:
51
52
- **Plugin Class**: Main `TerserPlugin` class that integrates with webpack's compilation hooks
53
- **Minification Functions**: Static methods for different minifiers (terser, uglify-js, swc, esbuild)
54
- **Worker System**: Parallel processing support using jest-worker for improved build performance
55
- **Configuration Schema**: JSON schema validation for plugin options
56
- **Comment Extraction**: Separate system for extracting and preserving license comments
57
58
## Capabilities
59
60
### Plugin Configuration
61
62
Core webpack plugin functionality with comprehensive configuration options for file matching, parallel processing, and comment extraction.
63
64
```javascript { .api }
65
class TerserPlugin {
66
constructor(options?: PluginOptions);
67
apply(compiler: Compiler): void;
68
}
69
70
interface PluginOptions {
71
test?: Rules;
72
include?: Rules;
73
exclude?: Rules;
74
parallel?: boolean | number;
75
extractComments?: ExtractCommentsOptions;
76
minify?: MinimizerImplementation;
77
terserOptions?: MinimizerOptions;
78
}
79
80
type Rules = RegExp | string | Array<RegExp | string>;
81
```
82
83
[Plugin Configuration](./plugin-configuration.md)
84
85
### Minification Functions
86
87
Built-in minification implementations for different JavaScript minifiers, each with specific optimization capabilities.
88
89
```javascript { .api }
90
// Static methods on TerserPlugin class
91
static terserMinify(
92
input: Input,
93
sourceMap?: SourceMapInput,
94
minimizerOptions?: any,
95
extractComments?: ExtractCommentsOptions
96
): Promise<MinimizedResult>;
97
98
static uglifyJsMinify(
99
input: Input,
100
sourceMap?: SourceMapInput,
101
minimizerOptions?: any,
102
extractComments?: ExtractCommentsOptions
103
): Promise<MinimizedResult>;
104
105
static swcMinify(
106
input: Input,
107
sourceMap?: SourceMapInput,
108
minimizerOptions?: any
109
): Promise<MinimizedResult>;
110
111
static esbuildMinify(
112
input: Input,
113
sourceMap?: SourceMapInput,
114
minimizerOptions?: any
115
): Promise<MinimizedResult>;
116
```
117
118
[Minification Functions](./minifiers.md)
119
120
### Utility Functions
121
122
Helper functions for task management, memoization, and internal plugin operations.
123
124
```javascript { .api }
125
function throttleAll<T>(limit: number, tasks: Task<T>[]): Promise<T[]>;
126
function memoize<T>(fn: () => any): () => T;
127
```
128
129
[Utility Functions](./utilities.md)
130
131
## Core Types
132
133
```javascript { .api }
134
interface MinimizedResult {
135
code: string;
136
map?: SourceMapInput;
137
errors?: (string | Error)[];
138
warnings?: (string | Error)[];
139
extractedComments?: string[];
140
}
141
142
interface Input {
143
[file: string]: string;
144
}
145
146
type ExtractCommentsOptions =
147
| boolean
148
| string
149
| RegExp
150
| ExtractCommentsFunction
151
| ExtractCommentsObject;
152
153
interface ExtractCommentsObject {
154
condition?: ExtractCommentsCondition;
155
filename?: string | ((fileData: any) => string);
156
banner?: string | boolean | ((commentsFile: string) => string);
157
}
158
159
type ExtractCommentsCondition =
160
| boolean
161
| 'all'
162
| 'some'
163
| RegExp
164
| ExtractCommentsFunction;
165
166
interface ExtractCommentsFunction {
167
(
168
astNode: any,
169
comment: {
170
value: string;
171
type: 'comment1' | 'comment2' | 'comment3' | 'comment4';
172
pos: number;
173
line: number;
174
col: number;
175
}
176
): boolean;
177
}
178
179
interface MinimizerImplementation<T> {
180
(
181
input: Input,
182
sourceMap: SourceMapInput | undefined,
183
minifyOptions: MinimizerOptions<T>,
184
extractComments?: ExtractCommentsOptions
185
): Promise<MinimizedResult>;
186
getMinimizerVersion?(): string | undefined;
187
supportsWorkerThreads?(): boolean | undefined;
188
}
189
190
type MinimizerOptions<T> = PredefinedOptions<T> & T;
191
192
interface PredefinedOptions<T> {
193
module?: boolean | string;
194
ecma?: number | string;
195
}
196
```