UNPKG

7.95 kBMarkdownView Raw
1[![npm][npm]][npm-url]
2[![node][node]][node-url]
3[![deps][deps]][deps-url]
4[![test][test]][test-url]
5[![coverage][cover]][cover-url]
6[![chat][chat]][chat-url]
7
8<div align="center">
9 <a href="https://github.com/webpack/webpack">
10 <img width="200" height="200" src="https://cdn.rawgit.com/webpack/media/e7485eb2/logo/icon.svg">
11 </a>
12 <h1>Worker Loader</h1>
13 <p>This loader registers the script as <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API">Web Worker</a><p>
14</div>
15
16
17<h2 align="center">Install</h2>
18
19```bash
20npm i -D worker-loader
21```
22
23<h2 align="center"><a href="https://webpack.js.org/concepts/loaders">Usage</a></h2>
24
25### `Inlined`
26
27**App.js**
28```js
29import Worker from 'worker-loader!./Worker.js';
30```
31
32### `Config`
33
34**webpack.config.js**
35```js
36{
37 module: {
38 rules: [
39 {
40 test: /\.worker\.js$/,
41 use: { loader: 'worker-loader' }
42 }
43 ]
44 }
45}
46```
47
48**App.js**
49```js
50import Worker from './file.worker.js';
51
52const worker = new Worker();
53
54worker.postMessage({ a: 1 });
55worker.onmessage = function (event) {};
56
57worker.addEventListener("message", function (event) {});
58```
59
60<h2 align="center">Options</h2>
61
62|Name|Type|Default|Description|
63|:--:|:--:|:-----:|:----------|
64|[**`name`**](#name)|`{String}`|`[hash].worker.js`|Set a custom name for the output script|
65|[**`inline`**](#inline)|`{Boolean}`|`false`|Inline the worker as a BLOB|
66|[**`fallback`**](#fallback)|`{Boolean}`|`false`|Require a fallback for non-worker supporting environments|
67|[**`publicPath`**](#publicPath)|`{String}`|`null`|Override the path from which worker scripts are downloaded|
68
69### `name`
70
71To set a custom name for the output script, use the `name` parameter. The name may contain the string `[hash]`, which will be replaced with a content dependent hash for caching purposes. When using `name` alone `[hash]` is omitted.
72
73*webpack.config.js**
74```js
75{
76 loader: 'worker-loader',
77 options: { name: 'WorkerName.[hash].js' }
78}
79```
80
81### `inline`
82
83You can also inline the worker as a BLOB with the `inline` parameter
84
85**webpack.config.js**
86```js
87{
88 loader: 'worker-loader',
89 options: { inline: true }
90}
91```
92
93> ℹ️ Inline mode will also create chunks for browsers without support for inline workers, to disable this behavior just set `fallback` parameter as `false`
94
95**webpack.config.js**
96```js
97{
98 loader: 'worker-loader'
99 options: { inline: true, fallback: false }
100}
101```
102
103### `fallback`
104
105Require a fallback for non-worker supporting environments
106
107**webpack.config.js**
108```js
109{
110 loader: 'worker-loader'
111 options: { fallback: false }
112}
113```
114
115### `publicPath`
116
117Overrides the path from which worker scripts are downloaded. If not specified, the same public path used for other
118webpack assets is used
119
120**webpack.config.js**
121```js
122{
123 loader: 'worker-loader'
124 options: { publicPath: '/scripts/workers/' }
125}
126```
127
128<h2 align="center">Examples</h2>
129
130The worker file can import dependencies just like any other file
131
132**Worker.js**
133```js
134const _ = require('lodash')
135
136const obj = { foo: 'foo' }
137
138_.has(obj, 'foo')
139
140// Post data to parent thread
141self.postMessage({ foo: 'foo' })
142
143// Respond to message from parent thread
144self.addEventListener('message', (event) => console.log(event))
145```
146
147### `Integrating with ES2015 Modules`
148
149> ℹ️ You can even use ES2015 Modules if you have the [`babel-loader`](https://github.com/babel/babel-loader) configured.
150
151**Worker.js**
152```js
153import _ from 'lodash'
154
155const obj = { foo: 'foo' }
156
157_.has(obj, 'foo')
158
159// Post data to parent thread
160self.postMessage({ foo: 'foo' })
161
162// Respond to message from parent thread
163self.addEventListener('message', (event) => console.log(event))
164```
165
166### `Integrating with TypeScript`
167
168To integrate with TypeScript, you will need to define a custom module for the exports of your worker
169
170**typings/custom.d.ts**
171```typescript
172declare module "worker-loader!*" {
173 class WebpackWorker extends Worker {
174 constructor();
175 }
176
177 export default WebpackWorker;
178}
179```
180
181**Worker.ts**
182```typescript
183const ctx: Worker = self as any;
184
185// Post data to parent thread
186ctx.postMessage({ foo: "foo" });
187
188// Respond to message from parent thread
189ctx.addEventListener("message", (event) => console.log(event));
190```
191
192**App.ts**
193```typescript
194import Worker from "worker-loader!./Worker";
195
196const worker = new Worker();
197
198worker.postMessage({ a: 1 });
199worker.onmessage = (event) => {};
200
201worker.addEventListener("message", (event) => {});
202```
203
204### `Cross-Origin Policy`
205
206[`WebWorkers`](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) are restricted by a [same-origin policy](https://en.wikipedia.org/wiki/Same-origin_policy), so if your `webpack` assets are not being served from the same origin as your application, their download may be blocked by your browser. This scenario can commonly occur if you are hosting your assets under a CDN domain. Even downloads from the `webpack-dev-server` could be blocked. There are two workarounds
207
208Firstly, you can inline the worker as a blob instead of downloading it as an external script via the [`inline`](#inline) parameter
209
210**App.js**
211```js
212import Worker from './file.worker.js';
213```
214
215**webpack.config.js**
216```js
217{
218 loader: 'worker-loader'
219 options: { inline: true }
220}
221```
222
223Secondly, you may override the base download URL for your worker script via the [`publicPath`](#publicpath) option
224
225**App.js**
226```js
227// This will cause the worker to be downloaded from `/workers/file.worker.js`
228import Worker from './file.worker.js';
229```
230
231**webpack.config.js**
232```js
233{
234 loader: 'worker-loader'
235 options: { publicPath: '/workers/' }
236}
237```
238
239<h2 align="center">Maintainers</h2>
240
241<table>
242 <tbody>
243 <tr>
244 <td align="center">
245 <a href="https://github.com/TrySound">
246 <img width="150" height="150" src="https://avatars3.githubusercontent.com/u/5635476?v=3&s=150">
247 </a>
248 <br />
249 <a href="https://github.com/TrySound">Bogdan Chadkin</a>
250 </td>
251 <td align="center">
252 <a href="https://github.com/bebraw">
253 <img width="150" height="150" src="https://github.com/bebraw.png?v=3&s=150">
254 </br>
255 Juho Vepsäläinen
256 </a>
257 </td>
258 <td align="center">
259 <a href="https://github.com/d3viant0ne">
260 <img width="150" height="150" src="https://github.com/d3viant0ne.png?v=3&s=150">
261 </br>
262 Joshua Wiens
263 </a>
264 </td>
265 <td align="center">
266 <a href="https://github.com/michael-ciniawsky">
267 <img width="150" height="150" src="https://github.com/michael-ciniawsky.png?v=3&s=150">
268 </br>
269 Michael Ciniawsky
270 </a>
271 </td>
272 <td align="center">
273 <a href="https://github.com/evilebottnawi">
274 <img width="150" height="150" src="https://github.com/evilebottnawi.png?v=3&s=150">
275 </br>
276 Alexander Krasnoyarov
277 </a>
278 </td>
279 </tr>
280 <tbody>
281</table>
282
283
284[npm]: https://img.shields.io/npm/v/worker-loader.svg
285[npm-url]: https://npmjs.com/package/worker-loader
286
287[node]: https://img.shields.io/node/v/cache-loader.svg
288[node-url]: https://nodejs.org
289
290[deps]: https://david-dm.org/webpack-contrib/worker-loader.svg
291[deps-url]: https://david-dm.org/webpack-contrib/worker-loader
292
293[test]: http://img.shields.io/travis/webpack-contrib/worker-loader.svg
294[test-url]: https://travis-ci.org/webpack-contrib/worker-loader
295
296[cover]: https://codecov.io/gh/webpack-contrib/cache-loader/branch/master/graph/badge.svg
297[cover-url]: https://codecov.io/gh/webpack-contrib/cache-loader
298
299[chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
300[chat-url]: https://gitter.im/webpack/webpack