CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-terser-webpack-plugin

Webpack plugin that integrates Terser JavaScript minifier for code optimization and compression

Pending
Overview
Eval results
Files

plugin-configuration.mddocs/

Plugin Configuration

Core webpack plugin functionality with comprehensive configuration options for file matching, parallel processing, and comment extraction.

Capabilities

TerserPlugin Constructor

Creates a new instance of the Terser webpack plugin with specified configuration options.

/**
 * Creates a new TerserPlugin instance
 * @param options - Plugin configuration options
 */
constructor(options?: PluginOptions);

interface PluginOptions {
  /** Test to match files against (default: /\.[cm]?js(\?.*)?$/i) */
  test?: Rules;
  /** Include all modules matching any of these conditions */
  include?: Rules;
  /** Exclude all modules matching any of these conditions */
  exclude?: Rules;
  /** Use multi-process parallel running (default: true) */
  parallel?: boolean | number;
  /** Comment extraction configuration (default: true) */
  extractComments?: ExtractCommentsOptions;
  /** Custom minify function (default: TerserPlugin.terserMinify) */
  minify?: MinimizerImplementation;
  /** Options for the minifier (default: {}) */
  terserOptions?: MinimizerOptions;
}

type Rules = RegExp | string | Array<RegExp | string>;

Usage Examples:

const TerserPlugin = require('terser-webpack-plugin');

// Basic configuration
new TerserPlugin();

// File filtering configuration
new TerserPlugin({
  test: /\.js$/i,
  include: /\/src/,
  exclude: /node_modules/,
});

// Parallel processing configuration
new TerserPlugin({
  parallel: 4, // Use 4 worker processes
});

// Custom minifier configuration
new TerserPlugin({
  minify: TerserPlugin.swcMinify,
  terserOptions: {
    compress: true,
    mangle: true,
  },
});

Apply Method

Required webpack plugin method that registers the plugin with webpack's compilation hooks.

/**
 * Applies the plugin to the webpack compiler
 * @param compiler - Webpack compiler instance
 */
apply(compiler: Compiler): void;

File Matching Options

Configure which files the plugin should process using test, include, and exclude patterns.

interface FileMatchingOptions {
  /** Test pattern to match files against */
  test?: Rules;
  /** Include pattern for files to process */
  include?: Rules;
  /** Exclude pattern for files to skip */
  exclude?: Rules;
}

type Rules = RegExp | string | Array<RegExp | string>;

Usage Examples:

// Match JavaScript and TypeScript files
new TerserPlugin({
  test: /\.(js|ts)$/i,
});

// Include only specific directories
new TerserPlugin({
  include: [/\/src/, /\/lib/],
});

// Exclude node_modules and test files
new TerserPlugin({
  exclude: [/node_modules/, /\.test\./],
});

// Combined patterns
new TerserPlugin({
  test: /\.js$/i,
  include: /\/src/,
  exclude: /\.spec\.js$/,
});

Parallel Processing Configuration

Configure multi-process parallel running to improve build speed.

interface ParallelOptions {
  /** Enable/disable parallel processing or set worker count */
  parallel?: boolean | number;
}

Usage Examples:

// Enable parallel processing (default: true)
new TerserPlugin({
  parallel: true,
});

// Disable parallel processing
new TerserPlugin({
  parallel: false,
});

// Set specific number of workers
new TerserPlugin({
  parallel: 4,
});

Comment Extraction Configuration

Configure how comments are extracted and preserved during minification.

type ExtractCommentsOptions = 
  | boolean
  | string
  | RegExp
  | ExtractCommentsFunction
  | ExtractCommentsObject;

interface ExtractCommentsObject {
  /** Condition to determine which comments to extract */
  condition?: ExtractCommentsCondition;
  /** Filename pattern for extracted comments file */
  filename?: string | ((fileData: any) => string);
  /** Banner text added to minified file */
  banner?: string | boolean | ((commentsFile: string) => string);
}

type ExtractCommentsCondition = 
  | boolean
  | 'all'
  | 'some'
  | RegExp
  | ExtractCommentsFunction;

interface ExtractCommentsFunction {
  (
    astNode: any,
    comment: {
      value: string;
      type: 'comment1' | 'comment2' | 'comment3' | 'comment4';
      pos: number;
      line: number;
      col: number;
    }
  ): boolean;
}

Usage Examples:

// Extract all comments (default)
new TerserPlugin({
  extractComments: true,
});

// Don't extract comments
new TerserPlugin({
  extractComments: false,
});

// Extract comments matching pattern
new TerserPlugin({
  extractComments: /^\**!|@preserve|@license|@cc_on/i,
});

// Custom extraction configuration
new TerserPlugin({
  extractComments: {
    condition: 'some',
    filename: '[file].LICENSE.txt',
    banner: (licenseFile) => {
      return `License information can be found in ${licenseFile}`;
    },
  },
});

// Custom extraction function
new TerserPlugin({
  extractComments: (astNode, comment) => {
    // Extract comments containing "license" or "copyright"
    return /@license|@copyright/i.test(comment.value);
  },
});

Minifier Options Configuration

Configure options passed to the selected minifier.

interface MinimizerConfiguration {
  /** Custom minify function */
  minify?: MinimizerImplementation;
  /** Options for the minifier */
  terserOptions?: MinimizerOptions;
}

interface MinimizerImplementation<T> {
  (
    input: Input,
    sourceMap: SourceMapInput | undefined,
    minifyOptions: MinimizerOptions<T>,
    extractComments?: ExtractCommentsOptions
  ): Promise<MinimizedResult>;
  getMinimizerVersion?(): string | undefined;
  supportsWorkerThreads?(): boolean | undefined;
}

type MinimizerOptions<T> = PredefinedOptions<T> & T;

interface PredefinedOptions<T> {
  /** Module format handling */
  module?: boolean | string;
  /** ECMAScript target version */
  ecma?: number | string;
}

Usage Examples:

// Terser-specific options
new TerserPlugin({
  terserOptions: {
    compress: {
      drop_console: true,
      drop_debugger: true,
    },
    mangle: {
      reserved: ['$', 'jQuery'],
    },
    format: {
      comments: false,
    },
  },
});

// Using different minifier
new TerserPlugin({
  minify: TerserPlugin.esbuildMinify,
  terserOptions: {
    target: 'es2018',
    minify: true,
    legalComments: 'none',
  },
});

Install with Tessl CLI

npx tessl i tessl/npm-terser-webpack-plugin

docs

index.md

minifiers.md

plugin-configuration.md

utilities.md

tile.json