You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
4.0 KiB

3 months ago
  1. <div align="center">
  2. <a href="https://webpack.js.org/">
  3. <img width="200" height="200" vspace="" hspace="25" src="https://cdn.rawgit.com/webpack/media/e7485eb2/logo/icon-square-big.svg">
  4. </a>
  5. </div>
  6. [![npm][npm]][npm-url]
  7. [![node][node]][node-url]
  8. [![deps][deps]][deps-url]
  9. [![tests][tests]][tests-url]
  10. [![coverage][cover]][cover-url]
  11. [![chat][chat]][chat-url]
  12. [![size][size]][size-url]
  13. # thread-loader
  14. Runs the following loaders in a worker pool.
  15. ## Getting Started
  16. ```bash
  17. npm install --save-dev thread-loader
  18. ```
  19. Put this loader in front of other loaders. The following loaders run in a worker pool.
  20. Loaders running in a worker pool are limited. Examples:
  21. - Loaders cannot emit files.
  22. - Loaders cannot use custom loader API (i. e. by plugins).
  23. - Loaders cannot access the webpack options.
  24. Each worker is a separate node.js process, which has an overhead of ~600ms. There is also an overhead of inter-process communication.
  25. Use this loader only for expensive operations!
  26. ### Examples
  27. **webpack.config.js**
  28. ```js
  29. module.exports = {
  30. module: {
  31. rules: [
  32. {
  33. test: /\.js$/,
  34. include: path.resolve('src'),
  35. use: [
  36. 'thread-loader',
  37. // your expensive loader (e.g babel-loader)
  38. ],
  39. },
  40. ],
  41. },
  42. };
  43. ```
  44. **with options**
  45. ```js
  46. use: [
  47. {
  48. loader: 'thread-loader',
  49. // loaders with equal options will share worker pools
  50. options: {
  51. // the number of spawned workers, defaults to (number of cpus - 1) or
  52. // fallback to 1 when require('os').cpus() is undefined
  53. workers: 2,
  54. // number of jobs a worker processes in parallel
  55. // defaults to 20
  56. workerParallelJobs: 50,
  57. // additional node.js arguments
  58. workerNodeArgs: ['--max-old-space-size=1024'],
  59. // Allow to respawn a dead worker pool
  60. // respawning slows down the entire compilation
  61. // and should be set to false for development
  62. poolRespawn: false,
  63. // timeout for killing the worker processes when idle
  64. // defaults to 500 (ms)
  65. // can be set to Infinity for watching builds to keep workers alive
  66. poolTimeout: 2000,
  67. // number of jobs the poll distributes to the workers
  68. // defaults to 200
  69. // decrease of less efficient but more fair distribution
  70. poolParallelJobs: 50,
  71. // name of the pool
  72. // can be used to create different pools with elsewise identical options
  73. name: 'my-pool',
  74. },
  75. },
  76. // your expensive loader (e.g babel-loader)
  77. ];
  78. ```
  79. **prewarming**
  80. To prevent the high delay when booting workers it possible to warmup the worker pool.
  81. This boots the max number of workers in the pool and loads specified modules into the node.js module cache.
  82. ```js
  83. const threadLoader = require('thread-loader');
  84. threadLoader.warmup(
  85. {
  86. // pool options, like passed to loader options
  87. // must match loader options to boot the correct pool
  88. },
  89. [
  90. // modules to load
  91. // can be any module, i. e.
  92. 'babel-loader',
  93. 'babel-preset-es2015',
  94. 'sass-loader',
  95. ]
  96. );
  97. ```
  98. ## Contributing
  99. Please take a moment to read our contributing guidelines if you haven't yet done so.
  100. [CONTRIBUTING](./.github/CONTRIBUTING.md)
  101. ## License
  102. [MIT](./LICENSE)
  103. [npm]: https://img.shields.io/npm/v/thread-loader.svg
  104. [npm-url]: https://npmjs.com/package/thread-loader
  105. [node]: https://img.shields.io/node/v/thread-loader.svg
  106. [node-url]: https://nodejs.org
  107. [deps]: https://david-dm.org/webpack-contrib/thread-loader.svg
  108. [deps-url]: https://david-dm.org/webpack-contrib/thread-loader
  109. [tests]: https://github.com/webpack-contrib/thread-loader/workflows/thread-loader/badge.svg
  110. [tests-url]: https://github.com/webpack-contrib/thread-loader/actions
  111. [cover]: https://codecov.io/gh/webpack-contrib/thread-loader/branch/master/graph/badge.svg
  112. [cover-url]: https://codecov.io/gh/webpack-contrib/thread-loader
  113. [chat]: https://badges.gitter.im/webpack/webpack.svg
  114. [chat-url]: https://gitter.im/webpack/webpack
  115. [size]: https://packagephobia.now.sh/badge?p=thread-loader
  116. [size-url]: https://packagephobia.now.sh/result?p=thread-loader