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