thread-loader

免责声明:thread-loader 是由社区成员维护的第三方包,它可能不支持与 webpack 相同的支持、安全策略或许可证,并且不受 webpack 维护。

npm node tests coverage discussion size

在工作池中运行以下加载器。

入门

npm install --save-dev thread-loader

yarn add -D thread-loader

pnpm add -D thread-loader

将此加载器放在其他加载器之前。以下加载器在工作池中运行。

在工作池中运行的加载器是有限的。示例

  • 加载器无法发出文件。
  • 加载器无法使用自定义加载器 API(即通过插件)。
  • 加载器无法访问 webpack 选项。

每个工作进程都是一个独立的 node.js 进程,其开销约为 600 毫秒。还有进程间通信的开销。

仅对昂贵的操作使用此加载程序!

示例

webpack.config.js

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

带选项

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)
];

预热

为了防止启动工作进程时出现高延迟,可以预热工作进程池。

这将启动池中的最大工作进程数,并将指定模块加载到 node.js 模块缓存中。

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',
  ]
);

贡献

如果您尚未阅读,请花点时间阅读我们的贡献指南。

贡献

许可证

MIT