在工作人員池中執行下列載入器。
npm install --save-dev thread-loader
或
yarn add -D thread-loader
或
pnpm add -D thread-loader
將此載入器置於其他載入器之前。下列載入器會在工作人員池中執行。
在工作人員池中執行的載入器有限。範例
每個工作人員都是一個獨立的 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',
]
);
如果您尚未閱讀我們的貢獻指南,請花點時間閱讀。