A rollup plugin to minify javascript with Terser
A Rollup plugin for minifying JavaScript bundles using Terser.
include/exclude patternsThis 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 | Description |
|---|---|---|
include |
string \| RegExp |
Pattern to match files that should be minified |
exclude |
string \| RegExp |
Pattern to match files that should be skipped |
| Option | Type | Default | Description |
|---|---|---|---|
sourceMap |
boolean |
true |
Generate source maps |
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.
uglify({
include: /\.min\.js$/,
});
uglify({
exclude: /\.test\.js$/,
});
uglify({
compress: {
drop_console: true,
drop_debugger: true,
pure_funcs: ["console.log"],
},
mangle: {
properties: false,
},
ecma: 2020,
});
If you’re upgrading from version 2.1.2 to 2.1.4+, you may encounter this error. The dependency has been updated from rollup-pluginutils to @rollup/pluginutils. Run npm install to resolve.
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
Copyright 2019-2026 Oleksandr Lopatnov
Oleksandr Lopatnov — Full-stack developer
If you find this project useful, please consider giving it a star on GitHub!