CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-rollup-plugin-uglify

Rollup plugin to minify generated bundle using UglifyJS with worker-based processing

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

Rollup Plugin Uglify

Rollup Plugin Uglify is a Rollup plugin that minifies generated JavaScript bundles using UglifyJS. It provides enhanced performance through worker-based processing where UglifyJS runs in separate worker threads for each chunk, improving build times for large projects.

Package Information

  • Package Name: rollup-plugin-uglify
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install rollup-plugin-uglify --save-dev

Core Imports

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

For CommonJS:

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

Basic Usage

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

// Basic minification
rollup({
  input: "src/main.js",
  plugins: [
    uglify()
  ]
});

// With options
rollup({
  input: "src/main.js",
  plugins: [
    uglify({
      sourcemap: true,
      numWorkers: 4,
      mangle: {
        properties: {
          regex: /^_/
        }
      },
      output: {
        comments: function(node, comment) {
          if (comment.type === "comment2") {
            return /@preserve|@license|@cc_on/i.test(comment.value);
          }
          return false;
        }
      }
    })
  ]
});

Architecture

Rollup Plugin Uglify is built around several key components that enable efficient and reliable minification:

  • Plugin Factory: The main uglify() function creates and configures a Rollup plugin instance
  • Worker Pool: Uses jest-worker to spawn multiple worker processes for parallel minification of chunks
  • Transform Module: A separate transform.js file that runs in worker threads and performs the actual UglifyJS minification
  • Error Handling: Enhanced error reporting with Babel code frames that show exact error locations in source code
  • Lifecycle Management: Automatic worker cleanup on build completion or error

The worker-based architecture provides significant performance benefits for large builds by processing multiple chunks in parallel while maintaining a clean separation between the main Rollup process and CPU-intensive minification work.

Capabilities

Plugin Factory Function

Creates a Rollup plugin instance for minifying JavaScript code using UglifyJS.

/**
 * Creates a Rollup plugin instance for JavaScript minification
 * @param {UglifyOptions} userOptions - Configuration options for UglifyJS and plugin behavior
 * @returns {RollupPlugin} Rollup plugin object with lifecycle hooks
 */
function uglify(userOptions?: UglifyOptions): RollupPlugin;

interface UglifyOptions {
  /** Enable/disable source map generation (default: true) */
  sourcemap?: boolean;
  /** Number of worker threads to spawn (default: CPU count - 1) */
  numWorkers?: number;
  /** UglifyJS parse options */
  parse?: ParseOptions;
  /** UglifyJS compress options */
  compress?: CompressOptions | boolean;
  /** UglifyJS mangle options */
  mangle?: MangleOptions | boolean;
  /** UglifyJS output options */
  output?: OutputOptions;
  /** UglifyJS top-level options */
  toplevel?: boolean;
  /** Keep function names */
  keep_fnames?: boolean;
  /** Keep class names */
  keep_classnames?: boolean;
  /** Safari 10 compatibility */
  safari10?: boolean;
  /** Internet Explorer 8 compatibility */
  ie8?: boolean;
}

interface RollupPlugin {
  name: "uglify";
  renderStart(): void;
  renderChunk(code: string): Promise<{ code: string; map?: string }>;
  generateBundle(): void;
  renderError(): void;
}

Usage Examples:

// Basic minification
uglify()

// Disable sourcemaps
uglify({ sourcemap: false })

// Configure worker count
uglify({ numWorkers: 2 })

// Preserve license comments
uglify({
  output: {
    comments: function(node, comment) {
      return /@preserve|@license/i.test(comment.value);
    }
  }
})

// Keep all comments
uglify({
  output: {
    comments: "all"
  }
})

// Mangle private properties (starting with underscore)
uglify({
  mangle: {
    properties: {
      regex: /^_/
    }
  }
})

UglifyJS Option Types

Complete type definitions for UglifyJS configuration options.

interface ParseOptions {
  /** Parse bare returns in functions */
  bare_returns?: boolean;
  /** Support HTML comments */
  html5_comments?: boolean;
  /** Support hashbang */
  shebang?: boolean;
}

interface CompressOptions {
  /** Join consecutive var statements */
  sequences?: boolean;
  /** Remove dead code */
  dead_code?: boolean;
  /** Remove debugger statements */
  drop_debugger?: boolean;
  /** Unsafe comparisons */
  unsafe_comps?: boolean;
  /** Unsafe function optimizations */
  unsafe_Function?: boolean;
  /** Unsafe math optimizations */
  unsafe_math?: boolean;
  /** Unsafe prototype optimizations */
  unsafe_proto?: boolean;
  /** Unsafe regular expressions */
  unsafe_regexp?: boolean;
  /** Unsafe undefined optimizations */
  unsafe_undefined?: boolean;
  /** Remove unused variables */
  unused?: boolean;
  /** Maximum passes */
  passes?: number;
  /** Global definitions */
  global_defs?: Record<string, any>;
  /** Pure functions */
  pure_funcs?: string[];
  /** Pure getters */
  pure_getters?: boolean | "strict";
  /** Drop console statements */
  drop_console?: boolean;
  /** Keep function names */
  keep_fnames?: boolean;
  /** Keep class names */
  keep_classnames?: boolean;
}

interface MangleOptions {
  /** Mangle names */
  reserved?: string[];
  /** Top level mangling */
  toplevel?: boolean;
  /** Evaluate expressions */
  eval?: boolean;
  /** Keep function names */
  keep_fnames?: boolean;
  /** Keep class names */
  keep_classnames?: boolean;
  /** Safari 10 compatibility */
  safari10?: boolean;
  /** Property mangling options */
  properties?: PropertyMangleOptions;
}

interface PropertyMangleOptions {
  /** Regular expression for properties to mangle */
  regex?: RegExp;
  /** Reserved property names */
  reserved?: string[];
  /** Debug property mangling */
  debug?: boolean;
  /** Keep quoted properties */
  keep_quoted?: boolean | "strict";
}

interface OutputOptions {
  /** Output format */
  beautify?: boolean;
  /** Preserve AST */
  ast?: boolean;
  /** Output code to string */
  code?: boolean;
  /** Include preamble */
  preamble?: string;
  /** Quote style */
  quote_style?: 0 | 1 | 2 | 3;
  /** Wrap immediately invoked function expressions */
  wrap_iife?: boolean;
  /** Keep quoted properties */
  keep_quoted_props?: boolean;
  /** Internet Explorer 8 compatibility */
  ie8?: boolean;
  /** Comment preservation function or policy */
  comments?: boolean | "all" | "some" | RegExp | ((node: any, comment: Comment) => boolean);
  /** Safari 10 compatibility */
  safari10?: boolean;
  /** ASCII only output */
  ascii_only?: boolean;
  /** Inline script compatibility */
  inline_script?: boolean;
  /** Width for beautified output */
  width?: number;
  /** Maximum line length */
  max_line_len?: number;
  /** Preserve line numbers */
  preserve_line?: boolean;
  /** Indent level */
  indent_level?: number;
  /** Indent start */
  indent_start?: number;
  /** Quote keys */
  quote_keys?: boolean;
  /** Space colon */
  space_colon?: boolean;
  /** Bracketize */
  bracketize?: boolean;
  /** Semicolons */
  semicolons?: boolean;
  /** Shebang */
  shebang?: boolean;
}

interface Comment {
  type: "comment1" | "comment2";
  value: string;
  pos: number;
  line: number;
  col: number;
}

interface SourceMap {
  version: number;
  sources: string[];
  names: string[];
  mappings: string;
  file?: string;
  sourceRoot?: string;
  sourcesContent?: string[];
}

Features

Worker-Based Processing

  • Each chunk is processed in a separate worker thread for improved performance using jest-worker
  • Configurable worker count via numWorkers option (defaults to CPU count - 1)
  • Worker threads run the transform.js module which handles UglifyJS minification
  • Automatic worker cleanup on completion or error

Enhanced Error Reporting

  • Error messages include babel code frames showing exact location of syntax errors
  • Line and column information for debugging

Source Map Support

  • Automatic source map generation (enabled by default)
  • Can be disabled via sourcemap: false option
  • Integrates with Rollup's source map pipeline

Comment Preservation

  • Flexible comment preservation through output.comments option
  • Support for license and copyright comment preservation
  • Function-based filtering for fine-grained control

UglifyJS Integration

  • Full access to UglifyJS API options
  • Support for all compression, mangling, and output options
  • ES5 syntax support (use terser for ES6+)

Error Handling

The plugin throws errors in the following cases:

  1. Deprecated Option Usage: Using sourceMap instead of sourcemap throws an error
  2. Syntax Errors: Invalid JavaScript syntax in input code
  3. UglifyJS Errors: Any errors from the underlying UglifyJS library

Error messages include enhanced debugging information with code frames and precise location details.

Compatibility

  • Rollup Version: Requires rollup@0.66.0 or higher
  • JavaScript Version: Supports ES5 syntax (use rollup-plugin-terser for ES6+)
  • Node.js: Compatible with Node.js environments supporting worker threads

docs

index.md

tile.json