A Rollup plugin that uses esbuild as a fast TypeScript/JavaScript compiler and minifier, replacing traditional tools like rollup-plugin-typescript2 and rollup-plugin-terser
—
The standalone minify function provides code compression and optimization functionality without transformation or compilation features.
Creates a Rollup plugin specifically for code minification using esbuild's fast minification engine.
/**
* Creates a standalone minification plugin
* @param options - Minification configuration options
* @returns Rollup plugin instance for minification only
*/
function minify(options?: MinifyOptions): RollupPlugin;
interface MinifyOptions {
// Source maps
sourceMap?: boolean;
// Minification control
minify?: boolean;
minifyWhitespace?: boolean;
minifyIdentifiers?: boolean;
minifySyntax?: boolean;
// Output configuration
target?: string | string[];
charset?: "ascii" | "utf8";
format?: "iife" | "cjs" | "esm";
globalName?: string;
// Code preservation
keepNames?: boolean;
legalComments?: "none" | "inline" | "eof" | "linked" | "external";
ignoreAnnotations?: boolean;
treeShaking?: boolean;
// Advanced minification
mangleProps?: RegExp;
reserveProps?: RegExp;
mangleQuoted?: boolean;
mangleCache?: { [key: string]: string | false };
// Code elimination
drop?: ("console" | "debugger")[];
dropLabels?: string[];
}Usage Examples:
import { minify } from "rollup-plugin-esbuild";
// Basic minification
export default {
plugins: [
minify()
]
};
// Custom minification options
export default {
plugins: [
minify({
target: "es2017",
keepNames: true,
legalComments: "none"
})
]
};
// Selective minification
export default {
plugins: [
minify({
minifyWhitespace: true,
minifyIdentifiers: false,
minifySyntax: true
})
]
};
// Production minification with console removal
export default {
plugins: [
minify({
minify: true,
drop: ["console", "debugger"],
legalComments: "none"
})
]
};Fine-grained control over different aspects of minification.
interface MinificationControl {
/** Enable full minification (combines all minify options) */
minify?: boolean;
/** Remove unnecessary whitespace and formatting */
minifyWhitespace?: boolean;
/** Shorten variable and function names */
minifyIdentifiers?: boolean;
/** Transform syntax for smaller output */
minifySyntax?: boolean;
}Behavior:
minify: true - Enables all minification optionsControl how function and class names are handled during minification.
interface NameOptions {
/** Preserve original function and class names */
keepNames?: boolean;
/** Pattern for properties to mangle */
mangleProps?: RegExp;
/** Pattern for properties to preserve */
reserveProps?: RegExp;
/** Whether to mangle quoted properties */
mangleQuoted?: boolean;
/** Cache for consistent property name mangling */
mangleCache?: { [key: string]: string | false };
}Examples:
// Preserve all names for debugging
{
keepNames: true
}
// Mangle private properties (starting with _)
{
mangleProps: /^_/,
mangleQuoted: false
}
// Preserve specific properties
{
mangleProps: /.*/,
reserveProps: /^(public|api)_/
}Remove specific types of code during minification.
interface CodeElimination {
/** Remove console calls and debugger statements */
drop?: ("console" | "debugger")[];
/** Remove labeled statements by label name */
dropLabels?: string[];
}Examples:
// Remove console and debugger for production
{
drop: ["console", "debugger"]
}
// Remove development-only code blocks
{
dropLabels: ["DEV", "DEBUG", "TEST"]
}Control how legal comments (copyright notices, licenses) are handled.
interface LegalCommentsOptions {
/** How to handle legal comments */
legalComments?: "none" | "inline" | "eof" | "linked" | "external";
}Options:
"none" - Remove all legal comments"inline" - Keep legal comments inline with code"eof" - Move legal comments to end of file"linked" - Create separate .LEGAL.txt file and link in bundle"external" - Create separate .LEGAL.txt file without linkingConfigure output formatting for minified code.
interface OutputFormatOptions {
/** Target JavaScript version/environment */
target?: string | string[];
/** Character encoding for output */
charset?: "ascii" | "utf8";
/** Output module format */
format?: "iife" | "cjs" | "esm";
/** Global variable name for IIFE format */
globalName?: string;
}Examples:
// Target modern browsers
{
target: ["es2020", "chrome80", "firefox78"],
charset: "utf8"
}
// IIFE bundle for browser
{
format: "iife",
globalName: "MyLibrary",
target: "es2015"
}
// CommonJS for Node.js
{
format: "cjs",
target: "node14"
}Additional optimization settings for fine-tuned minification.
interface AdvancedOptions {
/** Enable tree shaking dead code elimination */
treeShaking?: boolean;
/** Ignore @__PURE__ annotations */
ignoreAnnotations?: boolean;
}Usage:
treeShaking: true - Remove unused codeignoreAnnotations: true - Ignore pure function annotations for more aggressive minificationInstall with Tessl CLI
npx tessl i tessl/npm-rollup-plugin-esbuild