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

utilities.mddocs/

Utility Functions

Helper functions for task management, memoization, and internal plugin operations that support the core minification functionality.

Capabilities

Throttle All

Run multiple async tasks with limited concurrency to control resource usage and improve performance.

/**
 * Run tasks with limited concurrency
 * @param limit - Maximum number of tasks to run simultaneously
 * @param tasks - Array of task functions returning promises
 * @returns Promise resolving to array of results
 */
function throttleAll<T>(limit: number, tasks: Task<T>[]): Promise<T[]>;

type Task<T> = () => Promise<T>;

Usage Examples:

const { throttleAll } = require('terser-webpack-plugin/dist/utils');

// Limit concurrent file processing
const fileTasks = files.map(file => 
  () => processFile(file)
);

// Process max 4 files concurrently
const results = await throttleAll(4, fileTasks);

// Process tasks one at a time
const sequentialResults = await throttleAll(1, tasks);

Memoize

Memoization utility that caches the result of expensive function calls.

/**
 * Memoize function results for performance optimization
 * @param fn - Function to memoize (called once, result cached)
 * @returns Memoized function that returns cached result
 */
function memoize<T>(fn: (() => any) | undefined): () => T;

Usage Examples:

const { memoize } = require('terser-webpack-plugin/dist/utils');

// Memoize expensive computation
const getExpensiveValue = memoize(() => {
  console.log('Computing expensive value...');
  return performExpensiveComputation();
});

// First call executes the function
const result1 = getExpensiveValue(); // Logs "Computing expensive value..."

// Subsequent calls return cached result
const result2 = getExpensiveValue(); // No log, returns cached value
console.log(result1 === result2); // true

Worker Functions

Internal functions used by the plugin's worker system for parallel processing.

/**
 * Internal minification function used by worker processes
 * @param options - Internal minification options
 * @returns Promise resolving to minification result
 */
function minify<T>(options: InternalOptions<T>): Promise<MinimizedResult>;

/**
 * Transform function for worker threads with serialized options
 * @param options - Serialized options string
 * @returns Promise resolving to minification result
 */
function transform(options: string): Promise<MinimizedResult>;

interface InternalOptions<T> {
  name: string;
  input: string;
  inputSourceMap: SourceMapInput | undefined;
  extractComments: ExtractCommentsOptions | undefined;
  minimizer: {
    implementation: MinimizerImplementation<T>;
    options: MinimizerOptions<T>;
  };
}

Built-in Minifier Functions

All built-in minifier functions are available as exports from the utils module.

Terser Minify Function

/**
 * Terser minification implementation with comprehensive options
 * @param input - Object mapping filenames to source code
 * @param sourceMap - Optional source map input
 * @param minimizerOptions - Terser-specific configuration
 * @param extractComments - Comment extraction settings
 * @returns Promise resolving to minification result
 */
async function terserMinify(
  input: Input,
  sourceMap: SourceMapInput | undefined,
  minimizerOptions: any,
  extractComments?: ExtractCommentsOptions
): Promise<MinimizedResult>;

// Function properties
terserMinify.getMinimizerVersion(): string | undefined;
terserMinify.supportsWorkerThreads(): boolean; // returns true

UglifyJS Minify Function

/**
 * UglifyJS minification implementation
 * @param input - Object mapping filenames to source code
 * @param sourceMap - Optional source map input
 * @param minimizerOptions - UglifyJS-specific configuration
 * @param extractComments - Comment extraction settings
 * @returns Promise resolving to minification result
 */
async function uglifyJsMinify(
  input: Input,
  sourceMap: SourceMapInput | undefined,
  minimizerOptions: any,
  extractComments?: ExtractCommentsOptions
): Promise<MinimizedResult>;

// Function properties
uglifyJsMinify.getMinimizerVersion(): string | undefined;
uglifyJsMinify.supportsWorkerThreads(): boolean; // returns true

SWC Minify Function

/**
 * SWC minification implementation
 * @param input - Object mapping filenames to source code
 * @param sourceMap - Optional source map input
 * @param minimizerOptions - SWC-specific configuration
 * @returns Promise resolving to minification result
 */
async function swcMinify(
  input: Input,
  sourceMap: SourceMapInput | undefined,
  minimizerOptions: any
): Promise<MinimizedResult>;

// Function properties
swcMinify.getMinimizerVersion(): string | undefined;
swcMinify.supportsWorkerThreads(): boolean; // returns false

ESBuild Minify Function

/**
 * ESBuild minification implementation
 * @param input - Object mapping filenames to source code
 * @param sourceMap - Optional source map input
 * @param minimizerOptions - ESBuild-specific configuration
 * @returns Promise resolving to minification result
 */
async function esbuildMinify(
  input: Input,
  sourceMap: SourceMapInput | undefined,
  minimizerOptions: any
): Promise<MinimizedResult>;

// Function properties
esbuildMinify.getMinimizerVersion(): string | undefined;
esbuildMinify.supportsWorkerThreads(): boolean; // returns false

Usage Examples

Custom Task Processing

const { throttleAll, memoize } = require('terser-webpack-plugin/dist/utils');

// Combine utilities for optimized processing
const getProcessingConfig = memoize(() => ({
  workers: require('os').cpus().length - 1,
  chunkSize: 1000,
}));

async function processFiles(files) {
  const config = getProcessingConfig();
  
  const tasks = files.map(file => async () => {
    // Process individual file
    return await minifyFile(file);
  });
  
  // Process with limited concurrency
  return await throttleAll(config.workers, tasks);
}

Accessing Utility Functions

// Import utilities from the utils module
const { 
  throttleAll, 
  memoize, 
  terserMinify, 
  uglifyJsMinify, 
  swcMinify, 
  esbuildMinify 
} = require('terser-webpack-plugin/dist/utils');

// Use minifier functions directly
const result = await terserMinify(
  { 'main.js': sourceCode },
  undefined,
  { compress: true }
);

Types

type Task<T> = () => Promise<T>;

interface Input {
  [file: string]: string;
}

interface MinimizedResult {
  code: string;
  map?: SourceMapInput;
  errors?: (string | Error)[];
  warnings?: (string | Error)[];
  extractedComments?: string[];
}

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?: boolean | string;
  ecma?: number | string;
}

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

type SourceMapInput = any; // From @jridgewell/trace-mapping

Install with Tessl CLI

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

docs

index.md

minifiers.md

plugin-configuration.md

utilities.md

tile.json