Rollup plugin to minify generated bundle using UglifyJS with worker-based processing
npx @tessl/cli install tessl/npm-rollup-plugin-uglify@6.0.0Rollup 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.
npm install rollup-plugin-uglify --save-devimport { uglify } from "rollup-plugin-uglify";For CommonJS:
const { uglify } = require("rollup-plugin-uglify");import { rollup } from "rollup";
import { uglify } from "rollup-plugin-uglify";
// Basic minification
rollup({
input: "src/main.js",
plugins: [
uglify()
]
});
// With options
rollup({
input: "src/main.js",
plugins: [
uglify({
sourcemap: true,
numWorkers: 4,
mangle: {
properties: {
regex: /^_/
}
},
output: {
comments: function(node, comment) {
if (comment.type === "comment2") {
return /@preserve|@license|@cc_on/i.test(comment.value);
}
return false;
}
}
})
]
});Rollup Plugin Uglify is built around several key components that enable efficient and reliable minification:
uglify() function creates and configures a Rollup plugin instancejest-worker to spawn multiple worker processes for parallel minification of chunkstransform.js file that runs in worker threads and performs the actual UglifyJS minificationThe 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.
Creates a Rollup plugin instance for minifying JavaScript code using UglifyJS.
/**
* Creates a Rollup plugin instance for JavaScript minification
* @param {UglifyOptions} userOptions - Configuration options for UglifyJS and plugin behavior
* @returns {RollupPlugin} Rollup plugin object with lifecycle hooks
*/
function uglify(userOptions?: UglifyOptions): RollupPlugin;
interface UglifyOptions {
/** Enable/disable source map generation (default: true) */
sourcemap?: boolean;
/** Number of worker threads to spawn (default: CPU count - 1) */
numWorkers?: number;
/** UglifyJS parse options */
parse?: ParseOptions;
/** UglifyJS compress options */
compress?: CompressOptions | boolean;
/** UglifyJS mangle options */
mangle?: MangleOptions | boolean;
/** UglifyJS output options */
output?: OutputOptions;
/** UglifyJS top-level options */
toplevel?: boolean;
/** Keep function names */
keep_fnames?: boolean;
/** Keep class names */
keep_classnames?: boolean;
/** Safari 10 compatibility */
safari10?: boolean;
/** Internet Explorer 8 compatibility */
ie8?: boolean;
}
interface RollupPlugin {
name: "uglify";
renderStart(): void;
renderChunk(code: string): Promise<{ code: string; map?: string }>;
generateBundle(): void;
renderError(): void;
}Usage Examples:
// Basic minification
uglify()
// Disable sourcemaps
uglify({ sourcemap: false })
// Configure worker count
uglify({ numWorkers: 2 })
// Preserve license comments
uglify({
output: {
comments: function(node, comment) {
return /@preserve|@license/i.test(comment.value);
}
}
})
// Keep all comments
uglify({
output: {
comments: "all"
}
})
// Mangle private properties (starting with underscore)
uglify({
mangle: {
properties: {
regex: /^_/
}
}
})Complete type definitions for UglifyJS configuration options.
interface ParseOptions {
/** Parse bare returns in functions */
bare_returns?: boolean;
/** Support HTML comments */
html5_comments?: boolean;
/** Support hashbang */
shebang?: boolean;
}
interface CompressOptions {
/** Join consecutive var statements */
sequences?: boolean;
/** Remove dead code */
dead_code?: boolean;
/** Remove debugger statements */
drop_debugger?: boolean;
/** Unsafe comparisons */
unsafe_comps?: boolean;
/** Unsafe function optimizations */
unsafe_Function?: boolean;
/** Unsafe math optimizations */
unsafe_math?: boolean;
/** Unsafe prototype optimizations */
unsafe_proto?: boolean;
/** Unsafe regular expressions */
unsafe_regexp?: boolean;
/** Unsafe undefined optimizations */
unsafe_undefined?: boolean;
/** Remove unused variables */
unused?: boolean;
/** Maximum passes */
passes?: number;
/** Global definitions */
global_defs?: Record<string, any>;
/** Pure functions */
pure_funcs?: string[];
/** Pure getters */
pure_getters?: boolean | "strict";
/** Drop console statements */
drop_console?: boolean;
/** Keep function names */
keep_fnames?: boolean;
/** Keep class names */
keep_classnames?: boolean;
}
interface MangleOptions {
/** Mangle names */
reserved?: string[];
/** Top level mangling */
toplevel?: boolean;
/** Evaluate expressions */
eval?: boolean;
/** Keep function names */
keep_fnames?: boolean;
/** Keep class names */
keep_classnames?: boolean;
/** Safari 10 compatibility */
safari10?: boolean;
/** Property mangling options */
properties?: PropertyMangleOptions;
}
interface PropertyMangleOptions {
/** Regular expression for properties to mangle */
regex?: RegExp;
/** Reserved property names */
reserved?: string[];
/** Debug property mangling */
debug?: boolean;
/** Keep quoted properties */
keep_quoted?: boolean | "strict";
}
interface OutputOptions {
/** Output format */
beautify?: boolean;
/** Preserve AST */
ast?: boolean;
/** Output code to string */
code?: boolean;
/** Include preamble */
preamble?: string;
/** Quote style */
quote_style?: 0 | 1 | 2 | 3;
/** Wrap immediately invoked function expressions */
wrap_iife?: boolean;
/** Keep quoted properties */
keep_quoted_props?: boolean;
/** Internet Explorer 8 compatibility */
ie8?: boolean;
/** Comment preservation function or policy */
comments?: boolean | "all" | "some" | RegExp | ((node: any, comment: Comment) => boolean);
/** Safari 10 compatibility */
safari10?: boolean;
/** ASCII only output */
ascii_only?: boolean;
/** Inline script compatibility */
inline_script?: boolean;
/** Width for beautified output */
width?: number;
/** Maximum line length */
max_line_len?: number;
/** Preserve line numbers */
preserve_line?: boolean;
/** Indent level */
indent_level?: number;
/** Indent start */
indent_start?: number;
/** Quote keys */
quote_keys?: boolean;
/** Space colon */
space_colon?: boolean;
/** Bracketize */
bracketize?: boolean;
/** Semicolons */
semicolons?: boolean;
/** Shebang */
shebang?: boolean;
}
interface Comment {
type: "comment1" | "comment2";
value: string;
pos: number;
line: number;
col: number;
}
interface SourceMap {
version: number;
sources: string[];
names: string[];
mappings: string;
file?: string;
sourceRoot?: string;
sourcesContent?: string[];
}jest-workernumWorkers option (defaults to CPU count - 1)transform.js module which handles UglifyJS minificationsourcemap: false optionoutput.comments optionThe plugin throws errors in the following cases:
sourceMap instead of sourcemap throws an errorError messages include enhanced debugging information with code frames and precise location details.