A rollup plugin to minify javascript with Terser
A Rollup plugin for minifying JavaScript bundles using Terser. Full TypeScript support, multiple output formats, flexible file filtering, zero configuration required for basic usage.
This plugin requires Rollup and Terser as peer dependencies:
npm install rollup terser --save-dev
npm install @lopatnov/rollup-plugin-uglify --save-dev
// rollup.config.js
import uglify from "@lopatnov/rollup-plugin-uglify";
export default {
input: "src/index.js",
output: {
file: "dist/bundle.js",
format: "cjs",
},
plugins: [uglify()],
};
import uglify from "@lopatnov/rollup-plugin-uglify";
export default {
input: "src/index.js",
output: {
file: "dist/bundle.js",
format: "es",
sourcemap: true,
},
plugins: [
uglify({
compress: {
drop_console: true,
drop_debugger: true,
},
mangle: true,
ecma: 2020,
}),
],
};
const uglify = require("@lopatnov/rollup-plugin-uglify");
The uglify() function accepts an optional configuration object that extends Terser’s MinifyOptions.
| Option | Type | Default | Description |
|---|---|---|---|
include |
string \| RegExp |
- | Pattern to match chunks that should be minified |
exclude |
string \| RegExp |
- | Pattern to match chunks that should be skipped |
hook |
"renderChunk" \| "transform" |
"transform" |
Rollup hook to use for minification. It’s recommended to use "renderChunk" value |
| Option | Type | Default | Description |
|---|---|---|---|
sourceMap |
boolean |
auto | Generate source maps (follows output sourcemap option) |
compress |
object |
- | Compression options |
mangle |
boolean |
- | Mangle variable names |
ecma |
number |
- | ECMAScript version (2015, 2020, etc.) |
For a complete list of options, see the Terser documentation.
Minify only specific files:
uglify({ include: /\.min\.js$/ });
Exclude test files:
uglify({ exclude: /\.test\.js$/ });
Use legacy transform hook (per-module minification):
uglify({ hook: "transform", compress: true });
Production build with aggressive compression:
uglify({
compress: {
drop_console: true,
drop_debugger: true,
pure_funcs: ["console.log"],
},
mangle: { properties: false },
ecma: 2020,
});
Cannot find module @rollup/pluginutils
If you’re upgrading from version 2.1.2 to 2.1.4+, the dependency has been updated from rollup-pluginutils to @rollup/pluginutils. Run npm install to resolve.
Contributions are welcome! Please read CONTRIBUTING.md before opening a pull request.
Apache-2.0 © 2019–2026 Oleksandr Lopatnov · LinkedIn