1. rollup-plugin-terser
Rollup plugin to minify generated es bundle
rollup-plugin-terser
Package: rollup-plugin-terser
Created by: TrySound
Last modified: Fri, 09 Jun 2023 21:51:10 GMT
Version: 7.0.2
License: MIT
Downloads: 18,586,517
Repository: https://github.com/TrySound/rollup-plugin-terser

Install

npm install rollup-plugin-terser
yarn add rollup-plugin-terser

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
# Or with npm:
npm i rollup-plugin-terser --save-dev

Note: this package requires [email protected] and higher (including [email protected])

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 or hard to maintain.
  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

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.

Examples

Using as output plugin

 // 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" },
  ],
};

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

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code