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 プロセスであり、〜600ms のオーバーヘッドがあります。プロセス間通信のオーバーヘッドもあります。

このローダーは、高コストの操作にのみ使用してください!

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