or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-rollup-plugin-terser

Rollup plugin to minify generated ES bundles using Terser with worker-based parallel processing

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/rollup-plugin-terser@7.0.x

To install, run

npx @tessl/cli install tessl/npm-rollup-plugin-terser@7.0.0

index.mddocs/

Rollup Plugin Terser

Rollup Plugin Terser is a plugin for Rollup that integrates Terser for minifying JavaScript bundles during the build process. It provides worker-based parallel processing for efficient minification, automatic configuration based on output format, and comprehensive options for controlling the minification process.

Package Information

  • Package Name: rollup-plugin-terser
  • Package Type: npm
  • Language: JavaScript/TypeScript
  • Installation: npm install rollup-plugin-terser --save-dev

Core Imports

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

For CommonJS:

const { terser } = require("rollup-plugin-terser");

Basic Usage

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

// Basic usage with default options
export default {
  input: "src/index.js",
  output: {
    file: "dist/bundle.min.js",
    format: "iife"
  },
  plugins: [terser()]
};

// With custom options
export default {
  input: "src/index.js",
  output: {
    file: "dist/bundle.min.js",
    format: "esm"
  },
  plugins: [
    terser({
      compress: {
        drop_console: true
      },
      format: {
        comments: false
      },
      numWorkers: 4
    })
  ]
};

Architecture

Rollup Plugin Terser is built around several key components:

  • Plugin Factory: The terser() function that creates and configures the Rollup plugin instance
  • Worker Management: Uses jest-worker for multi-threaded minification to improve performance
  • Format Detection: Automatically configures Terser options based on Rollup's output format (ESM, CommonJS, etc.)
  • Options Processing: Merges user options with format-specific defaults and handles plugin-specific options
  • Error Handling: Enhanced error reporting with code frame context using Babel's code-frame

Capabilities

Terser Plugin Factory

Creates a Rollup plugin instance configured for JavaScript minification using Terser.

/**
 * Creates a Rollup plugin for minifying JavaScript code using Terser
 * @param options - Configuration options extending Terser's MinifyOptions
 * @returns Rollup Plugin object with renderChunk method
 */
function terser(options?: Options): Plugin;

interface Options extends Omit<MinifyOptions, "sourceMap"> {
  /**
   * Amount of workers to spawn for parallel processing
   * Defaults to the number of CPUs minus 1
   */
  numWorkers?: number;
}

interface Plugin {
  /** Plugin identifier */
  name: "terser";
  /** Async method called by Rollup to process each chunk */
  renderChunk(code: string, chunk: ChunkInfo, outputOptions: OutputOptions): Promise<TransformResult>;
}

Configuration Features:

  • Automatic Format Detection: The plugin automatically sets optimal Terser options based on Rollup's output format:

    • module: true when format is esm or es
    • toplevel: true when format is cjs
    • sourceMap inferred from Rollup's sourcemap settings
  • Worker-based Processing: Uses jest-worker to spawn multiple processes for parallel minification, improving performance on multi-core systems

  • Name Cache Support: Supports Terser's name caching feature for consistent variable names across builds

Usage Examples:

// Basic minification
terser()

// Custom compression settings
terser({
  compress: {
    drop_console: true,
    drop_debugger: true,
    pure_funcs: ["console.log", "console.warn"]
  }
})

// Preserve specific comments (e.g., licenses)
terser({
  format: {
    comments: function(node, comment) {
      const text = comment.value;
      const type = comment.type;
      if (type === "comment2") {
        // multiline comment
        return /@preserve|@license|@cc_on/i.test(text);
      }
    }
  }
})

// Keep all comments
terser({
  format: {
    comments: "all"
  }
})

// Custom worker configuration
terser({
  numWorkers: 2,
  compress: {
    passes: 2
  }
})

// Name cache for consistent builds
const nameCache = {};
terser({
  nameCache: nameCache,
  mangle: {
    properties: true
  }
})

Types

interface Options extends Omit<MinifyOptions, "sourceMap"> {
  /**
   * Amount of workers to spawn for parallel processing
   * Defaults to the number of CPUs minus 1
   */
  numWorkers?: number;
}

interface Plugin {
  /** Plugin identifier */
  name: "terser";
  /** Async method called by Rollup to process each chunk */
  renderChunk(
    code: string,
    chunk: ChunkInfo,
    outputOptions: OutputOptions
  ): Promise<TransformResult>;
}

interface ChunkInfo {
  fileName: string;
  name: string;
  isEntry: boolean;
  isDynamicEntry: boolean;
  facadeModuleId: string | null;
  moduleIds: string[];
  exports: string[];
  [key: string]: any;
}

interface OutputOptions {
  format: "es" | "esm" | "cjs" | "iife" | "umd" | "system" | "amd";
  sourcemap?: boolean | "inline" | "hidden";
  [key: string]: any;
}

interface TransformResult {
  code: string;
  map?: any; // Source map object
}

Error Handling

The plugin provides enhanced error reporting with code context:

  • Parse Errors: When Terser encounters syntax errors, the plugin displays the problematic code with line numbers and context using Babel's code-frame
  • Worker Errors: Errors from worker processes are properly propagated and formatted
  • Configuration Errors: Invalid configuration options (like deprecated sourceMap or sourcemap options) throw descriptive errors

Deprecated Options:

The plugin will throw errors for these deprecated options:

  • sourceMap - Now inferred from Rollup options
  • sourcemap - Now inferred from Rollup options

Performance Notes

  • Worker Management: Workers are created per plugin instance and reused across chunks in the same build
  • Memory Efficiency: Workers are terminated when all chunks are processed to free memory
  • CPU Utilization: Default worker count is CPU cores minus 1 for optimal performance without blocking the system