rollup-plugin-terser
Rollup plugin to minify generated es bundle
Last updated 10 days ago by trysound .
MIT · Repository · Original npm · Tarball · package.json
$ cnpm install rollup-plugin-terser 
SYNC missed versions from official npm registry.

rollup-plugin-terser Travis Build Status

Rollup plugin to minify generated es bundle. Uses terser under the hood.

Install

yarn add rollup-plugin-terser --dev

Note: this package requires rollup@0.66 and higher (including rollup@1.0.0)

Usage

import { rollup } from "rollup";
import { terser } from "rollup-plugin-terser";

rollup({
  input: "main.js",
  plugins: [terser()]
});

Why named export?

  1. Module is a namespace. Default export often leads to function/component per file dogma and makes code less maintainable.
  2. Interop with commonjs is broken in many cases. A ways to fight them are known.
  3. 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.

terser(options);

options - terser API options

options.sourcemap: boolean

Generates source maps and passes them to rollup. Defaults to true.

options.numWorkers: number

Amount of workers to spawn. Defaults to the number of CPUs minus 1.

options.include: Array<string | RegExp> | string | RegExp

options.exclude: Array<string | RegExp> | string | RegExp

Specifically include/exclude chunk files names (minimatch pattern, or array of minimatch patterns), By default all chunk files will be minify.

Examples

include/exclude

If you'd like that only some of the files will be minify, then you can filter by include and exclude to do this like so:

// 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' },
    { file: 'lib.esm.js', format: 'es' },
    { dir: '.', entryFileNames: 'lib-[format].js', format: 'iife'  }
  ],
  plugins: [
    terser({
      include: [/^.+\.min\.js$/, '*esm*'], 
      exclude: [ 'some*' ]
    })
  ]
};

Comments

If you'd like to preserve comments (for licensing for example), then you can specify a function to do this like so:

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):

terser({
  output: {
    comments: "all"
  }
});

See Terser documentation for further reference.

License

MIT © Bogdan Chadkin

Current Tags

  • 5.1.2                                ...           latest (10 days ago)

15 Versions

  • 5.1.2                                ...           10 days ago
  • 5.1.1                                ...           2 months ago
  • 5.1.0                                ...           3 months ago
  • 5.0.0                                ...           4 months ago
  • 4.0.4                                ...           8 months ago
  • 4.0.3                                ...           8 months ago
  • 4.0.2                                ...           8 months ago
  • 4.0.1                                ...           9 months ago
  • 4.0.0                                ...           9 months ago
  • 3.0.0                                ...           a year ago
  • 2.0.2                                ...           a year ago
  • 2.0.1                                ...           a year ago
  • 2.0.0                                ...           a year ago
  • 1.0.1                                ...           a year ago
  • 1.0.0                                ...           a year ago
Maintainers (1)
Downloads
Today 25
This Week 663
This Month 2,147
Last Day 35
Last Week 715
Last Month 3,846
Dependencies (5)
Dev Dependencies (6)
Dependents (316)

Copyright 2014 - 2017 © taobao.org |