rollup-plugin-terser
Rollup plugin to minify generated es bundle. Uses terser under the hood.
Install
yarn add rollup-plugin-terser --dev# Or with npm: npm i rollup-plugin-terser --save-dev
Note: this package requires rollup@0.66 and higher (including rollup@2.0.0)
Usage
;; ;
Why named export?
- Module is a namespace. Default export often leads to function/component per file dogma and makes code less maintainable.
- Interop with commonjs is broken in many cases or hard to maintain.
- Show me any good language with default exports. It's historical javascriptism.
Options
⚠️ Caveat: any function used in options object cannot rely on its surrounding scope, since it is executed in an isolated context.
;
options
- terser API options
Note: some terser options are set by the plugin automatically:
module: true
is set whenformat
isesm
ores
toplevel: true
is set whenformat
iscjs
options.numWorkers: number
Amount of workers to spawn. Defaults to the number of CPUs minus 1.
Examples
Using as output plugin
// rollup.config.js; input: "index.js" output: file: "lib.js" format: "cjs" file: "lib.min.js" format: "cjs" plugins: file: "lib.esm.js" format: "esm" ;
Comments
If you'd like to preserve comments (for licensing for example), then you can specify a function to do this like so:
;
Alternatively, you can also choose to keep all comments (e.g. if a licensing header has already been prepended by a previous rollup plugin):
;
See Terser documentation for further reference.
License
MIT © Bogdan Chadkin