Rollup plugin to minify generated bundle using UglifyJS with worker-based processing
npx @tessl/cli install tessl/npm-rollup-plugin-uglify@6.0.00
# Rollup Plugin Uglify
1
2
Rollup Plugin Uglify is a Rollup plugin that minifies generated JavaScript bundles using UglifyJS. It provides enhanced performance through worker-based processing where UglifyJS runs in separate worker threads for each chunk, improving build times for large projects.
3
4
## Package Information
5
6
- **Package Name**: rollup-plugin-uglify
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install rollup-plugin-uglify --save-dev`
10
11
## Core Imports
12
13
```javascript
14
import { uglify } from "rollup-plugin-uglify";
15
```
16
17
For CommonJS:
18
19
```javascript
20
const { uglify } = require("rollup-plugin-uglify");
21
```
22
23
## Basic Usage
24
25
```javascript
26
import { rollup } from "rollup";
27
import { uglify } from "rollup-plugin-uglify";
28
29
// Basic minification
30
rollup({
31
input: "src/main.js",
32
plugins: [
33
uglify()
34
]
35
});
36
37
// With options
38
rollup({
39
input: "src/main.js",
40
plugins: [
41
uglify({
42
sourcemap: true,
43
numWorkers: 4,
44
mangle: {
45
properties: {
46
regex: /^_/
47
}
48
},
49
output: {
50
comments: function(node, comment) {
51
if (comment.type === "comment2") {
52
return /@preserve|@license|@cc_on/i.test(comment.value);
53
}
54
return false;
55
}
56
}
57
})
58
]
59
});
60
```
61
62
## Architecture
63
64
Rollup Plugin Uglify is built around several key components that enable efficient and reliable minification:
65
66
- **Plugin Factory**: The main `uglify()` function creates and configures a Rollup plugin instance
67
- **Worker Pool**: Uses `jest-worker` to spawn multiple worker processes for parallel minification of chunks
68
- **Transform Module**: A separate `transform.js` file that runs in worker threads and performs the actual UglifyJS minification
69
- **Error Handling**: Enhanced error reporting with Babel code frames that show exact error locations in source code
70
- **Lifecycle Management**: Automatic worker cleanup on build completion or error
71
72
The worker-based architecture provides significant performance benefits for large builds by processing multiple chunks in parallel while maintaining a clean separation between the main Rollup process and CPU-intensive minification work.
73
74
## Capabilities
75
76
### Plugin Factory Function
77
78
Creates a Rollup plugin instance for minifying JavaScript code using UglifyJS.
79
80
```javascript { .api }
81
/**
82
* Creates a Rollup plugin instance for JavaScript minification
83
* @param {UglifyOptions} userOptions - Configuration options for UglifyJS and plugin behavior
84
* @returns {RollupPlugin} Rollup plugin object with lifecycle hooks
85
*/
86
function uglify(userOptions?: UglifyOptions): RollupPlugin;
87
88
interface UglifyOptions {
89
/** Enable/disable source map generation (default: true) */
90
sourcemap?: boolean;
91
/** Number of worker threads to spawn (default: CPU count - 1) */
92
numWorkers?: number;
93
/** UglifyJS parse options */
94
parse?: ParseOptions;
95
/** UglifyJS compress options */
96
compress?: CompressOptions | boolean;
97
/** UglifyJS mangle options */
98
mangle?: MangleOptions | boolean;
99
/** UglifyJS output options */
100
output?: OutputOptions;
101
/** UglifyJS top-level options */
102
toplevel?: boolean;
103
/** Keep function names */
104
keep_fnames?: boolean;
105
/** Keep class names */
106
keep_classnames?: boolean;
107
/** Safari 10 compatibility */
108
safari10?: boolean;
109
/** Internet Explorer 8 compatibility */
110
ie8?: boolean;
111
}
112
113
interface RollupPlugin {
114
name: "uglify";
115
renderStart(): void;
116
renderChunk(code: string): Promise<{ code: string; map?: string }>;
117
generateBundle(): void;
118
renderError(): void;
119
}
120
```
121
122
**Usage Examples:**
123
124
```javascript
125
// Basic minification
126
uglify()
127
128
// Disable sourcemaps
129
uglify({ sourcemap: false })
130
131
// Configure worker count
132
uglify({ numWorkers: 2 })
133
134
// Preserve license comments
135
uglify({
136
output: {
137
comments: function(node, comment) {
138
return /@preserve|@license/i.test(comment.value);
139
}
140
}
141
})
142
143
// Keep all comments
144
uglify({
145
output: {
146
comments: "all"
147
}
148
})
149
150
// Mangle private properties (starting with underscore)
151
uglify({
152
mangle: {
153
properties: {
154
regex: /^_/
155
}
156
}
157
})
158
```
159
160
### UglifyJS Option Types
161
162
Complete type definitions for UglifyJS configuration options.
163
164
```javascript { .api }
165
interface ParseOptions {
166
/** Parse bare returns in functions */
167
bare_returns?: boolean;
168
/** Support HTML comments */
169
html5_comments?: boolean;
170
/** Support hashbang */
171
shebang?: boolean;
172
}
173
174
interface CompressOptions {
175
/** Join consecutive var statements */
176
sequences?: boolean;
177
/** Remove dead code */
178
dead_code?: boolean;
179
/** Remove debugger statements */
180
drop_debugger?: boolean;
181
/** Unsafe comparisons */
182
unsafe_comps?: boolean;
183
/** Unsafe function optimizations */
184
unsafe_Function?: boolean;
185
/** Unsafe math optimizations */
186
unsafe_math?: boolean;
187
/** Unsafe prototype optimizations */
188
unsafe_proto?: boolean;
189
/** Unsafe regular expressions */
190
unsafe_regexp?: boolean;
191
/** Unsafe undefined optimizations */
192
unsafe_undefined?: boolean;
193
/** Remove unused variables */
194
unused?: boolean;
195
/** Maximum passes */
196
passes?: number;
197
/** Global definitions */
198
global_defs?: Record<string, any>;
199
/** Pure functions */
200
pure_funcs?: string[];
201
/** Pure getters */
202
pure_getters?: boolean | "strict";
203
/** Drop console statements */
204
drop_console?: boolean;
205
/** Keep function names */
206
keep_fnames?: boolean;
207
/** Keep class names */
208
keep_classnames?: boolean;
209
}
210
211
interface MangleOptions {
212
/** Mangle names */
213
reserved?: string[];
214
/** Top level mangling */
215
toplevel?: boolean;
216
/** Evaluate expressions */
217
eval?: boolean;
218
/** Keep function names */
219
keep_fnames?: boolean;
220
/** Keep class names */
221
keep_classnames?: boolean;
222
/** Safari 10 compatibility */
223
safari10?: boolean;
224
/** Property mangling options */
225
properties?: PropertyMangleOptions;
226
}
227
228
interface PropertyMangleOptions {
229
/** Regular expression for properties to mangle */
230
regex?: RegExp;
231
/** Reserved property names */
232
reserved?: string[];
233
/** Debug property mangling */
234
debug?: boolean;
235
/** Keep quoted properties */
236
keep_quoted?: boolean | "strict";
237
}
238
239
interface OutputOptions {
240
/** Output format */
241
beautify?: boolean;
242
/** Preserve AST */
243
ast?: boolean;
244
/** Output code to string */
245
code?: boolean;
246
/** Include preamble */
247
preamble?: string;
248
/** Quote style */
249
quote_style?: 0 | 1 | 2 | 3;
250
/** Wrap immediately invoked function expressions */
251
wrap_iife?: boolean;
252
/** Keep quoted properties */
253
keep_quoted_props?: boolean;
254
/** Internet Explorer 8 compatibility */
255
ie8?: boolean;
256
/** Comment preservation function or policy */
257
comments?: boolean | "all" | "some" | RegExp | ((node: any, comment: Comment) => boolean);
258
/** Safari 10 compatibility */
259
safari10?: boolean;
260
/** ASCII only output */
261
ascii_only?: boolean;
262
/** Inline script compatibility */
263
inline_script?: boolean;
264
/** Width for beautified output */
265
width?: number;
266
/** Maximum line length */
267
max_line_len?: number;
268
/** Preserve line numbers */
269
preserve_line?: boolean;
270
/** Indent level */
271
indent_level?: number;
272
/** Indent start */
273
indent_start?: number;
274
/** Quote keys */
275
quote_keys?: boolean;
276
/** Space colon */
277
space_colon?: boolean;
278
/** Bracketize */
279
bracketize?: boolean;
280
/** Semicolons */
281
semicolons?: boolean;
282
/** Shebang */
283
shebang?: boolean;
284
}
285
286
interface Comment {
287
type: "comment1" | "comment2";
288
value: string;
289
pos: number;
290
line: number;
291
col: number;
292
}
293
294
interface SourceMap {
295
version: number;
296
sources: string[];
297
names: string[];
298
mappings: string;
299
file?: string;
300
sourceRoot?: string;
301
sourcesContent?: string[];
302
}
303
```
304
305
## Features
306
307
### Worker-Based Processing
308
- Each chunk is processed in a separate worker thread for improved performance using `jest-worker`
309
- Configurable worker count via `numWorkers` option (defaults to CPU count - 1)
310
- Worker threads run the `transform.js` module which handles UglifyJS minification
311
- Automatic worker cleanup on completion or error
312
313
### Enhanced Error Reporting
314
- Error messages include babel code frames showing exact location of syntax errors
315
- Line and column information for debugging
316
317
### Source Map Support
318
- Automatic source map generation (enabled by default)
319
- Can be disabled via `sourcemap: false` option
320
- Integrates with Rollup's source map pipeline
321
322
### Comment Preservation
323
- Flexible comment preservation through `output.comments` option
324
- Support for license and copyright comment preservation
325
- Function-based filtering for fine-grained control
326
327
### UglifyJS Integration
328
- Full access to UglifyJS API options
329
- Support for all compression, mangling, and output options
330
- ES5 syntax support (use terser for ES6+)
331
332
## Error Handling
333
334
The plugin throws errors in the following cases:
335
336
1. **Deprecated Option Usage**: Using `sourceMap` instead of `sourcemap` throws an error
337
2. **Syntax Errors**: Invalid JavaScript syntax in input code
338
3. **UglifyJS Errors**: Any errors from the underlying UglifyJS library
339
340
Error messages include enhanced debugging information with code frames and precise location details.
341
342
## Compatibility
343
344
- **Rollup Version**: Requires rollup@0.66.0 or higher
345
- **JavaScript Version**: Supports ES5 syntax (use rollup-plugin-terser for ES6+)
346
- **Node.js**: Compatible with Node.js environments supporting worker threads