Rollup plugin to minify generated ES bundles using Terser with worker-based parallel processing
npx @tessl/cli install tessl/npm-rollup-plugin-terser@7.0.0Rollup Plugin Terser is a plugin for Rollup that integrates Terser for minifying JavaScript bundles during the build process. It provides worker-based parallel processing for efficient minification, automatic configuration based on output format, and comprehensive options for controlling the minification process.
npm install rollup-plugin-terser --save-devimport { terser } from "rollup-plugin-terser";For CommonJS:
const { terser } = require("rollup-plugin-terser");import { rollup } from "rollup";
import { terser } from "rollup-plugin-terser";
// Basic usage with default options
export default {
input: "src/index.js",
output: {
file: "dist/bundle.min.js",
format: "iife"
},
plugins: [terser()]
};
// With custom options
export default {
input: "src/index.js",
output: {
file: "dist/bundle.min.js",
format: "esm"
},
plugins: [
terser({
compress: {
drop_console: true
},
format: {
comments: false
},
numWorkers: 4
})
]
};Rollup Plugin Terser is built around several key components:
terser() function that creates and configures the Rollup plugin instanceCreates a Rollup plugin instance configured for JavaScript minification using Terser.
/**
* Creates a Rollup plugin for minifying JavaScript code using Terser
* @param options - Configuration options extending Terser's MinifyOptions
* @returns Rollup Plugin object with renderChunk method
*/
function terser(options?: Options): Plugin;
interface Options extends Omit<MinifyOptions, "sourceMap"> {
/**
* Amount of workers to spawn for parallel processing
* Defaults to the number of CPUs minus 1
*/
numWorkers?: number;
}
interface Plugin {
/** Plugin identifier */
name: "terser";
/** Async method called by Rollup to process each chunk */
renderChunk(code: string, chunk: ChunkInfo, outputOptions: OutputOptions): Promise<TransformResult>;
}Configuration Features:
Automatic Format Detection: The plugin automatically sets optimal Terser options based on Rollup's output format:
module: true when format is esm or estoplevel: true when format is cjssourceMap inferred from Rollup's sourcemap settingsWorker-based Processing: Uses jest-worker to spawn multiple processes for parallel minification, improving performance on multi-core systems
Name Cache Support: Supports Terser's name caching feature for consistent variable names across builds
Usage Examples:
// Basic minification
terser()
// Custom compression settings
terser({
compress: {
drop_console: true,
drop_debugger: true,
pure_funcs: ["console.log", "console.warn"]
}
})
// Preserve specific comments (e.g., licenses)
terser({
format: {
comments: function(node, comment) {
const text = comment.value;
const type = comment.type;
if (type === "comment2") {
// multiline comment
return /@preserve|@license|@cc_on/i.test(text);
}
}
}
})
// Keep all comments
terser({
format: {
comments: "all"
}
})
// Custom worker configuration
terser({
numWorkers: 2,
compress: {
passes: 2
}
})
// Name cache for consistent builds
const nameCache = {};
terser({
nameCache: nameCache,
mangle: {
properties: true
}
})interface Options extends Omit<MinifyOptions, "sourceMap"> {
/**
* Amount of workers to spawn for parallel processing
* Defaults to the number of CPUs minus 1
*/
numWorkers?: number;
}
interface Plugin {
/** Plugin identifier */
name: "terser";
/** Async method called by Rollup to process each chunk */
renderChunk(
code: string,
chunk: ChunkInfo,
outputOptions: OutputOptions
): Promise<TransformResult>;
}
interface ChunkInfo {
fileName: string;
name: string;
isEntry: boolean;
isDynamicEntry: boolean;
facadeModuleId: string | null;
moduleIds: string[];
exports: string[];
[key: string]: any;
}
interface OutputOptions {
format: "es" | "esm" | "cjs" | "iife" | "umd" | "system" | "amd";
sourcemap?: boolean | "inline" | "hidden";
[key: string]: any;
}
interface TransformResult {
code: string;
map?: any; // Source map object
}The plugin provides enhanced error reporting with code context:
sourceMap or sourcemap options) throw descriptive errorsDeprecated Options:
The plugin will throw errors for these deprecated options:
sourceMap - Now inferred from Rollup optionssourcemap - Now inferred from Rollup options