Webpack plugin that integrates Terser JavaScript minifier for code optimization and compression
—
Helper functions for task management, memoization, and internal plugin operations that support the core minification functionality.
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);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); // trueInternal 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>;
};
}All built-in minifier functions are available as exports from the utils module.
/**
* 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 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 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 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 falseconst { 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);
}// 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 }
);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-mappingInstall with Tessl CLI
npx tessl i tessl/npm-terser-webpack-plugin