| 1 | # rollup-plugin-terser [![Travis Build Status][travis-img]][travis]
|
|---|
| 2 |
|
|---|
| 3 | [travis-img]: https://travis-ci.org/TrySound/rollup-plugin-terser.svg
|
|---|
| 4 | [travis]: https://travis-ci.org/TrySound/rollup-plugin-terser
|
|---|
| 5 |
|
|---|
| 6 | [Rollup](https://github.com/rollup/rollup) plugin to minify generated es bundle. Uses [terser](https://github.com/fabiosantoscode/terser) under the hood.
|
|---|
| 7 |
|
|---|
| 8 | ## Install
|
|---|
| 9 |
|
|---|
| 10 | ```sh
|
|---|
| 11 | yarn add rollup-plugin-terser --dev
|
|---|
| 12 | # Or with npm:
|
|---|
| 13 | npm i rollup-plugin-terser --save-dev
|
|---|
| 14 | ```
|
|---|
| 15 |
|
|---|
| 16 | _Note: this package requires rollup@0.66 and higher (including rollup@2.0.0)_
|
|---|
| 17 |
|
|---|
| 18 | ## Usage
|
|---|
| 19 |
|
|---|
| 20 | ```js
|
|---|
| 21 | import { rollup } from "rollup";
|
|---|
| 22 | import { terser } from "rollup-plugin-terser";
|
|---|
| 23 |
|
|---|
| 24 | rollup({
|
|---|
| 25 | input: "main.js",
|
|---|
| 26 | plugins: [terser()],
|
|---|
| 27 | });
|
|---|
| 28 | ```
|
|---|
| 29 |
|
|---|
| 30 | ## Why named export?
|
|---|
| 31 |
|
|---|
| 32 | 1. Module is a namespace. Default export often leads to function/component per file dogma and makes code less maintainable.
|
|---|
| 33 | 2. Interop with commonjs is broken in many cases or hard to maintain.
|
|---|
| 34 | 3. Show me any good language with default exports. It's historical javascriptism.
|
|---|
| 35 |
|
|---|
| 36 | ## Options
|
|---|
| 37 |
|
|---|
| 38 | > ⚠️ **Caveat:** any function used in options object cannot rely on its surrounding scope, since it is executed in an isolated context.
|
|---|
| 39 |
|
|---|
| 40 | ```js
|
|---|
| 41 | terser(options);
|
|---|
| 42 | ```
|
|---|
| 43 |
|
|---|
| 44 | `options` - [terser API options](https://github.com/fabiosantoscode/terser#minify-options)
|
|---|
| 45 |
|
|---|
| 46 | Note: some terser options are set by the plugin automatically:
|
|---|
| 47 |
|
|---|
| 48 | - `module: true` is set when `format` is `esm` or `es`
|
|---|
| 49 | - `toplevel: true` is set when `format` is `cjs`
|
|---|
| 50 |
|
|---|
| 51 | `options.numWorkers: number`
|
|---|
| 52 |
|
|---|
| 53 | Amount of workers to spawn. Defaults to the number of CPUs minus 1.
|
|---|
| 54 |
|
|---|
| 55 | ## Examples
|
|---|
| 56 |
|
|---|
| 57 | ### Using as output plugin
|
|---|
| 58 |
|
|---|
| 59 | ```js
|
|---|
| 60 | // rollup.config.js
|
|---|
| 61 | import { terser } from "rollup-plugin-terser";
|
|---|
| 62 |
|
|---|
| 63 | export default {
|
|---|
| 64 | input: "index.js",
|
|---|
| 65 | output: [
|
|---|
| 66 | { file: "lib.js", format: "cjs" },
|
|---|
| 67 | { file: "lib.min.js", format: "cjs", plugins: [terser()] },
|
|---|
| 68 | { file: "lib.esm.js", format: "esm" },
|
|---|
| 69 | ],
|
|---|
| 70 | };
|
|---|
| 71 | ```
|
|---|
| 72 |
|
|---|
| 73 | ### Comments
|
|---|
| 74 |
|
|---|
| 75 | If you'd like to preserve comments (for licensing for example), then you can specify a function to do this like so:
|
|---|
| 76 |
|
|---|
| 77 | ```js
|
|---|
| 78 | terser({
|
|---|
| 79 | output: {
|
|---|
| 80 | comments: function (node, comment) {
|
|---|
| 81 | var text = comment.value;
|
|---|
| 82 | var type = comment.type;
|
|---|
| 83 | if (type == "comment2") {
|
|---|
| 84 | // multiline comment
|
|---|
| 85 | return /@preserve|@license|@cc_on/i.test(text);
|
|---|
| 86 | }
|
|---|
| 87 | },
|
|---|
| 88 | },
|
|---|
| 89 | });
|
|---|
| 90 | ```
|
|---|
| 91 |
|
|---|
| 92 | Alternatively, you can also choose to keep all comments (e.g. if a licensing header has already been prepended by a previous rollup plugin):
|
|---|
| 93 |
|
|---|
| 94 | ```js
|
|---|
| 95 | terser({
|
|---|
| 96 | output: {
|
|---|
| 97 | comments: "all",
|
|---|
| 98 | },
|
|---|
| 99 | });
|
|---|
| 100 | ```
|
|---|
| 101 |
|
|---|
| 102 | See [Terser documentation](https://github.com/fabiosantoscode/terser#terser) for further reference.
|
|---|
| 103 |
|
|---|
| 104 | # License
|
|---|
| 105 |
|
|---|
| 106 | MIT © [Bogdan Chadkin](mailto:trysound@yandex.ru)
|
|---|