Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

thread-loader

webpack-contrib4.1mMIT4.0.4

Runs the following loaders in a worker pool

webpack

readme

npm node tests coverage discussion size

thread-loader

Runs the following loaders in a worker pool.

Getting Started

npm install --save-dev thread-loader

or

yarn add -D thread-loader

or

pnpm add -D thread-loader

Put this loader in front of other loaders. The following loaders run in a worker pool.

Loaders running in a worker pool are limited. Examples:

  • Loaders cannot emit files.
  • Loaders cannot use custom loader API (i. e. by plugins).
  • Loaders cannot access the webpack options.

Each worker is a separate node.js process, which has an overhead of ~600ms. There is also an overhead of inter-process communication.

Use this loader only for expensive operations!

Examples

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.js$/,
        include: path.resolve('src'),
        use: [
          'thread-loader',
          // your expensive loader (e.g babel-loader)
        ],
      },
    ],
  },
};

with options

use: [
  {
    loader: 'thread-loader',
    // loaders with equal options will share worker pools
    options: {
      // the number of spawned workers, defaults to (number of cpus - 1) or
      // fallback to 1 when require('os').cpus() is undefined
      workers: 2,

      // number of jobs a worker processes in parallel
      // defaults to 20
      workerParallelJobs: 50,

      // additional node.js arguments
      workerNodeArgs: ['--max-old-space-size=1024'],

      // Allow to respawn a dead worker pool
      // respawning slows down the entire compilation
      // and should be set to false for development
      poolRespawn: false,

      // timeout for killing the worker processes when idle
      // defaults to 500 (ms)
      // can be set to Infinity for watching builds to keep workers alive
      poolTimeout: 2000,

      // number of jobs the poll distributes to the workers
      // defaults to 200
      // decrease of less efficient but more fair distribution
      poolParallelJobs: 50,

      // name of the pool
      // can be used to create different pools with elsewise identical options
      name: 'my-pool',
    },
  },
  // your expensive loader (e.g babel-loader)
];

prewarming

To prevent the high delay when booting workers it possible to warmup the worker pool.

This boots the max number of workers in the pool and loads specified modules into the node.js module cache.

const threadLoader = require('thread-loader');

threadLoader.warmup(
  {
    // pool options, like passed to loader options
    // must match loader options to boot the correct pool
  },
  [
    // modules to load
    // can be any module, i. e.
    'babel-loader',
    'babel-preset-es2015',
    'sass-loader',
  ],
);

Contributing

Please take a moment to read our contributing guidelines if you haven't yet done so.

CONTRIBUTING

License

MIT

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

4.0.4 (2024-09-18)

Bug Fixes

4.0.3 (2024-09-17)

Bug Fixes

4.0.2 (2023-05-22)

Bug Fixes

4.0.1 (2023-04-19)

Bug Fixes

4.0.0 (2023-04-18)

⚠ BREAKING CHANGES

  • minimum supported Node.js version is 16.10.0
  • minimum supported webpack version is 5

3.0.4 (2021-05-10)

Bug Fixes

  • do not crash on this.addBuildDependency usage (#117) (1c7a8a2)
  • this.addMissingDependency works fine (#119) (5a0ea0c)

3.0.3 (2021-04-13)

Bug Fixes

3.0.2 (2021-04-12)

Bug Fixes

3.0.1 (2020-10-27)

Bug Fixes

3.0.0 (2020-09-12)

⚠ BREAKING CHANGES

  • minimum supported Node.js version is 10.13

Bug Fixes

  • loadModule and fs are now available in a loader context (#88) (ea5c9ad)
  • getResolve is now available in a loader context (#99) (16bbc23)

2.1.3 (2019-08-08)

Bug Fixes

  • correct default for workerParallelJobs option (#74) (79758d0)
  • do not allow empty or invalid node args when spin up child process (#73) (b02d503)

2.1.2 (2019-01-25)

Bug Fixes

  • lifecycle handling for signals

2.1.1 (2018-12-21)

Performance Improvements

2.1.0 (2018-12-21)

Features

  • add poolRespawn flag to speed up incremental builds (#52) (76535bf)

2.0.2 (2018-12-20)

Bug Fixes

2.0.1 (2018-12-19)

Bug Fixes

  • memory leaks, worker and main process lifecycles (#51) (f10fe55)

2.0.0 (2018-12-18)

Bug Fixes

BREAKING CHANGE

  • drop support for node < 6.9

1.2.0 (2018-07-27)

Features

  • add target, minimize and resourceQuery into context (#25) (f3c7a2c)

1.1.5 (2018-02-26)

Bug Fixes

  • package: add webpack >= 4 (peerDependencies) (#22) (9345756)
  • WorkerError: handle undefined error stacks (#20) (6fb5daf)

1.1.4 (2018-02-21)

Bug Fixes

  • index: add webpack >= v4.0.0 support (#16) (5d33937)

1.1.3 (2018-02-07)

Bug Fixes

  • WorkerPool: trace stacks to avoid duplicated err.messages from workers (#13) (80dda4f)

1.1.2 (2017-10-09)

Bug Fixes

  • readBuffer: handle 0-byte reads (c7ca960)

1.1.1 (2017-08-28)

Bug Fixes

  • context: Pass context to loader (29ced70)
  • deps: pass along result for dependencies (19832ec)
  • example: fix for broken sass and add watch (47ba43e)

1.1.0 (2017-07-14)

Features

  • pool: add warmup method (a0ce440)

1.0.3 (2017-05-27)

Bug Fixes

  • resolve: fix passing error to worker (6561f57)

1.0.2 (2017-05-27)

Bug Fixes

  • resolve: fix incorrect method for sending message (bb92a28)

1.0.1 (2017-04-28)

Change Log

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

x.x.x / <year>-<month>-<day>

  • Bug fix -
  • Feature -
  • Chore -
  • Docs -