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