Rollup plugin to minify generated es bundle. Uses terser under the hood.
```sh yarn add rollup-plugin-terser --dev
npm i rollup-plugin-terser --save-dev ```
Note: this package requires rollup@0.66 and higher (including rollup@2.0.0)
```js import { rollup } from "rollup"; import { terser } from "rollup-plugin-terser";
rollup({ input: "main.js", plugins: [terser()], }); ```
⚠️ Caveat: any function used in options object cannot rely on its surrounding scope, since it is executed in an isolated context.
js
terser(options);
options
- terser API options
Note: some terser options are set by the plugin automatically:
module: true
is set when format
is esm
or es
toplevel: true
is set when format
is cjs
options.numWorkers: number
Amount of workers to spawn. Defaults to the number of CPUs minus 1.
```js // rollup.config.js import { terser } from "rollup-plugin-terser";
export default { input: "index.js", output: [ { file: "lib.js", format: "cjs" }, { file: "lib.min.js", format: "cjs", plugins: [terser()] }, { file: "lib.esm.js", format: "esm" }, ], }; ```
If you'd like to preserve comments (for licensing for example), then you can specify a function to do this like so:
js
terser({
output: {
comments: function (node, comment) {
var text = comment.value;
var type = comment.type;
if (type == "comment2") {
// multiline comment
return /@preserve|@license|@cc_on/i.test(text);
}
},
},
});
Alternatively, you can also choose to keep all comments (e.g. if a licensing header has already been prepended by a previous rollup plugin):
js
terser({
output: {
comments: "all",
},
});
See Terser documentation for further reference.
MIT © Bogdan Chadkin