5c3acd95a7eed94fb061b73b904d54d76bf0e8dd9ff5f66c1176490e3fb42e674be7dc115de872a140b1abd64a4ebc812a04cad67a3e9c7aaa4fa2372c9675 3.9 KB

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