Runs the following loaders in a worker pool
npx @tessl/cli install tessl/npm-thread-loader@4.0.0thread-loader is a webpack loader that runs subsequent loaders in a worker pool to improve build performance for expensive operations. It creates separate Node.js processes (workers) to execute loaders in parallel, providing significant performance benefits for computationally intensive tasks like Babel transpilation, TypeScript compilation, or CSS preprocessing.
npm install --save-dev thread-loaderconst threadLoader = require('thread-loader');The package exports CommonJS modules with the following functions:
// Access warmup function directly
threadLoader.warmup(options, requires);
// The pitch function is used internally by webpack - do NOT call directly
const { pitch, warmup } = threadLoader;// webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.js$/,
include: path.resolve('src'),
use: [
'thread-loader',
// your expensive loader (e.g babel-loader)
'babel-loader',
],
},
],
},
};// webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.js$/,
use: [
{
loader: 'thread-loader',
options: {
workers: 2,
workerParallelJobs: 50,
poolTimeout: 2000,
name: 'js-pool',
},
},
'babel-loader',
],
},
],
},
};const threadLoader = require('thread-loader');
threadLoader.warmup(
{
// pool options, like passed to loader options
// must match loader options to boot the correct pool
workers: 2,
workerParallelJobs: 50,
},
[
// modules to load
'babel-loader',
'babel-preset-es2015',
'sass-loader',
]
);thread-loader is built around several key components:
Pre-warm worker pools to reduce initial startup delays by pre-booting workers and loading specified modules into the Node.js module cache.
/**
* Pre-warm worker pool to reduce startup delays by starting workers and loading modules
* @param options - Pool configuration options (must match loader options used in webpack config)
* @param requires - Array of module names/paths to pre-load in worker processes
* @returns void
*/
function warmup(options: WorkerPoolOptions, requires: string[]): void;Usage Examples:
const threadLoader = require('thread-loader');
// Pre-warm with basic options
threadLoader.warmup({}, ['babel-loader']);
// Pre-warm with custom pool configuration
threadLoader.warmup(
{
workers: 4,
workerParallelJobs: 20,
poolTimeout: Infinity, // Keep workers alive for watching builds
name: 'babel-pool',
},
[
'babel-loader',
'babel-preset-env',
'@babel/preset-react',
]
);
// Pre-warm multiple different pools
threadLoader.warmup({ name: 'js-pool' }, ['babel-loader']);
threadLoader.warmup({ name: 'css-pool' }, ['sass-loader', 'postcss-loader']);The main loader function that intercepts webpack loader execution and delegates to worker pool. This is automatically called by webpack when the loader is used in a configuration.
/**
* Webpack loader pitch function that executes subsequent loaders in worker pool
* This function is automatically called by webpack's loader system
* @param this - Webpack loader context with resource info and callback functions
* @returns void - Uses async callback pattern
*/
function pitch(): void;Note: The pitch function is called automatically by webpack and should not be invoked directly. It's the main entry point when thread-loader is used in a webpack configuration.
Configuration options that can be passed to the thread-loader and warmup function.
interface WorkerPoolOptions {
/** Number of spawned workers. Defaults to (number of cpus - 1) or fallback to 1 */
workers?: number;
/** Number of jobs a worker processes in parallel. Defaults to 20 */
workerParallelJobs?: number;
/** Additional node.js arguments for worker processes */
workerNodeArgs?: string[];
/** Allow respawning a dead worker pool. Defaults to false */
poolRespawn?: boolean;
/** Timeout for killing idle worker processes in milliseconds. Defaults to 500 */
poolTimeout?: number;
/** Number of jobs the pool distributes to workers. Defaults to 200 */
poolParallelJobs?: number;
/** Name of the pool for creating different pools with identical options */
name?: string;
}--max-old-space-size=1024 to worker processesInfinity for watching builds to keep workers aliveUse thread-loader for:
Don't use thread-loader for:
Loaders running in workers have limitations:
Errors from worker processes are enhanced with stack trace information from both the main process and worker process, providing clear debugging information.
/**
* Enhanced error class for worker process errors with combined stack traces
*/
class WorkerError extends Error {
/**
* Create a new WorkerError with enhanced stack trace information
* @param err - Original error object or message from worker
* @param workerId - ID of the worker process that generated the error
*/
constructor(err: ErrorLike | string, workerId: number);
/** Error name, inherited from original error */
name: string;
/** Error message, inherited from original error */
message: string;
/** Combined stack trace from main process and worker process */
stack: string;
}
interface ErrorLike {
name?: string;
message: string;
stack?: string;
}Worker errors include: