codingclip-worker-loader 3.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/README.md +650 -0
- package/dist/cjs.js +6 -0
- package/dist/index.js +99 -0
- package/dist/options.json +72 -0
- package/dist/runtime/inline.js +39 -0
- package/dist/supportWebpack4.js +43 -0
- package/dist/supportWebpack5.js +64 -0
- package/dist/utils.js +93 -0
- package/package.json +80 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright JS Foundation and other contributors
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
'Software'), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
17
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
18
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
19
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
20
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,650 @@
|
|
|
1
|
+
# A fork of worker-loader to fix issues in webpack5
|
|
2
|
+
|
|
3
|
+
<div align="center">
|
|
4
|
+
<a href="https://github.com/webpack/webpack">
|
|
5
|
+
<img width="200" height="200" src="https://webpack.js.org/assets/icon-square-big.svg">
|
|
6
|
+
</a>
|
|
7
|
+
</div>
|
|
8
|
+
|
|
9
|
+
[![npm][npm]][npm-url]
|
|
10
|
+
[![node][node]][node-url]
|
|
11
|
+
[![deps][deps]][deps-url]
|
|
12
|
+
[![tests][tests]][tests-url]
|
|
13
|
+
[![coverage][cover]][cover-url]
|
|
14
|
+
[![chat][chat]][chat-url]
|
|
15
|
+
[![size][size]][size-url]
|
|
16
|
+
|
|
17
|
+
# worker-loader
|
|
18
|
+
|
|
19
|
+
**DEPRECATED for v5**: https://webpack.js.org/guides/web-workers/
|
|
20
|
+
|
|
21
|
+
Web Worker loader for webpack 4.
|
|
22
|
+
|
|
23
|
+
Note that this is specific to webpack 4. To use Web Workers in webpack 5, see https://webpack.js.org/guides/web-workers/.
|
|
24
|
+
|
|
25
|
+
## Getting Started
|
|
26
|
+
|
|
27
|
+
To begin, you'll need to install `worker-loader`:
|
|
28
|
+
|
|
29
|
+
```console
|
|
30
|
+
$ npm install worker-loader --save-dev
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Inlined
|
|
34
|
+
|
|
35
|
+
**App.js**
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
import Worker from "worker-loader!./Worker.js";
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Config
|
|
42
|
+
|
|
43
|
+
**webpack.config.js**
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
module.exports = {
|
|
47
|
+
module: {
|
|
48
|
+
rules: [
|
|
49
|
+
{
|
|
50
|
+
test: /\.worker\.js$/,
|
|
51
|
+
use: { loader: "worker-loader" },
|
|
52
|
+
},
|
|
53
|
+
],
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
**App.js**
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
import Worker from "./file.worker.js";
|
|
62
|
+
|
|
63
|
+
const worker = new Worker();
|
|
64
|
+
|
|
65
|
+
worker.postMessage({ a: 1 });
|
|
66
|
+
worker.onmessage = function (event) {};
|
|
67
|
+
|
|
68
|
+
worker.addEventListener("message", function (event) {});
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
And run `webpack` via your preferred method.
|
|
72
|
+
|
|
73
|
+
## Options
|
|
74
|
+
|
|
75
|
+
| Name | Type | Default | Description |
|
|
76
|
+
| :-----------------------------------: | :-------------------------: | :-----------------------------: | :-------------------------------------------------------------------------------- |
|
|
77
|
+
| **[`worker`](#worker)** | `{String\|Object}` | `Worker` | Allows to set web worker constructor name and options |
|
|
78
|
+
| **[`publicPath`](#publicpath)** | `{String\|Function}` | based on `output.publicPath` | specifies the public URL address of the output files when referenced in a browser |
|
|
79
|
+
| **[`filename`](#filename)** | `{String\|Function}` | based on `output.filename` | The filename of entry chunks for web workers |
|
|
80
|
+
| **[`chunkFilename`](#chunkfilename)** | `{String}` | based on `output.chunkFilename` | The filename of non-entry chunks for web workers |
|
|
81
|
+
| **[`inline`](#inline)** | `'no-fallback'\|'fallback'` | `undefined` | Allow to inline the worker as a `BLOB` |
|
|
82
|
+
| **[`esModule`](#esmodule)** | `{Boolean}` | `true` | Use ES modules syntax |
|
|
83
|
+
|
|
84
|
+
### `worker`
|
|
85
|
+
|
|
86
|
+
Type: `String|Object`
|
|
87
|
+
Default: `Worker`
|
|
88
|
+
|
|
89
|
+
Set the worker type.
|
|
90
|
+
|
|
91
|
+
#### `String`
|
|
92
|
+
|
|
93
|
+
Allows to set web worker constructor name.
|
|
94
|
+
|
|
95
|
+
**webpack.config.js**
|
|
96
|
+
|
|
97
|
+
```js
|
|
98
|
+
module.exports = {
|
|
99
|
+
module: {
|
|
100
|
+
rules: [
|
|
101
|
+
{
|
|
102
|
+
test: /\.worker\.(c|m)?js$/i,
|
|
103
|
+
loader: "worker-loader",
|
|
104
|
+
options: {
|
|
105
|
+
worker: "SharedWorker",
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
#### `Object`
|
|
114
|
+
|
|
115
|
+
Allow to set web worker constructor name and options.
|
|
116
|
+
|
|
117
|
+
**webpack.config.js**
|
|
118
|
+
|
|
119
|
+
```js
|
|
120
|
+
module.exports = {
|
|
121
|
+
module: {
|
|
122
|
+
rules: [
|
|
123
|
+
{
|
|
124
|
+
test: /\.worker\.(c|m)?js$/i,
|
|
125
|
+
loader: "worker-loader",
|
|
126
|
+
options: {
|
|
127
|
+
worker: {
|
|
128
|
+
type: "SharedWorker",
|
|
129
|
+
options: {
|
|
130
|
+
type: "classic",
|
|
131
|
+
credentials: "omit",
|
|
132
|
+
name: "my-custom-worker-name",
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
],
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### `publicPath`
|
|
143
|
+
|
|
144
|
+
Type: `String|Function`
|
|
145
|
+
Default: based on `output.publicPath`
|
|
146
|
+
|
|
147
|
+
The `publicPath` specifies the public URL address of the output files when referenced in a browser.
|
|
148
|
+
If not specified, the same public path used for other webpack assets is used.
|
|
149
|
+
|
|
150
|
+
#### `String`
|
|
151
|
+
|
|
152
|
+
**webpack.config.js**
|
|
153
|
+
|
|
154
|
+
```js
|
|
155
|
+
module.exports = {
|
|
156
|
+
module: {
|
|
157
|
+
rules: [
|
|
158
|
+
{
|
|
159
|
+
test: /\.worker\.(c|m)?js$/i,
|
|
160
|
+
loader: "worker-loader",
|
|
161
|
+
options: {
|
|
162
|
+
publicPath: "/scripts/workers/",
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
],
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
#### `Function`
|
|
171
|
+
|
|
172
|
+
**webpack.config.js**
|
|
173
|
+
|
|
174
|
+
```js
|
|
175
|
+
module.exports = {
|
|
176
|
+
module: {
|
|
177
|
+
rules: [
|
|
178
|
+
{
|
|
179
|
+
test: /\.worker\.(c|m)?js$/i,
|
|
180
|
+
loader: "worker-loader",
|
|
181
|
+
options: {
|
|
182
|
+
publicPath: (pathData, assetInfo) => {
|
|
183
|
+
return `/scripts/${pathData.hash}/workers/`;
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
],
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### `filename`
|
|
193
|
+
|
|
194
|
+
Type: `String|Function`
|
|
195
|
+
Default: based on `output.filename`, adding `worker` suffix, for example - `output.filename: '[name].js'` value of this option will be `[name].worker.js`
|
|
196
|
+
|
|
197
|
+
The filename of entry chunks for web workers.
|
|
198
|
+
|
|
199
|
+
#### `String`
|
|
200
|
+
|
|
201
|
+
**webpack.config.js**
|
|
202
|
+
|
|
203
|
+
```js
|
|
204
|
+
module.exports = {
|
|
205
|
+
module: {
|
|
206
|
+
rules: [
|
|
207
|
+
{
|
|
208
|
+
test: /\.worker\.(c|m)?js$/i,
|
|
209
|
+
loader: "worker-loader",
|
|
210
|
+
options: {
|
|
211
|
+
filename: "[name].[contenthash].worker.js",
|
|
212
|
+
},
|
|
213
|
+
},
|
|
214
|
+
],
|
|
215
|
+
},
|
|
216
|
+
};
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
#### `Function`
|
|
220
|
+
|
|
221
|
+
**webpack.config.js**
|
|
222
|
+
|
|
223
|
+
```js
|
|
224
|
+
module.exports = {
|
|
225
|
+
module: {
|
|
226
|
+
rules: [
|
|
227
|
+
{
|
|
228
|
+
test: /\.worker\.(c|m)?js$/i,
|
|
229
|
+
loader: "worker-loader",
|
|
230
|
+
options: {
|
|
231
|
+
filename: (pathData) => {
|
|
232
|
+
if (
|
|
233
|
+
/\.worker\.(c|m)?js$/i.test(pathData.chunk.entryModule.resource)
|
|
234
|
+
) {
|
|
235
|
+
return "[name].custom.worker.js";
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return "[name].js";
|
|
239
|
+
},
|
|
240
|
+
},
|
|
241
|
+
},
|
|
242
|
+
],
|
|
243
|
+
},
|
|
244
|
+
};
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### `chunkFilename`
|
|
248
|
+
|
|
249
|
+
Type: `String`
|
|
250
|
+
Default: based on `output.chunkFilename`, adding `worker` suffix, for example - `output.chunkFilename: '[id].js'` value of this option will be `[id].worker.js`
|
|
251
|
+
|
|
252
|
+
The filename of non-entry chunks for web workers.
|
|
253
|
+
|
|
254
|
+
**webpack.config.js**
|
|
255
|
+
|
|
256
|
+
```js
|
|
257
|
+
module.exports = {
|
|
258
|
+
module: {
|
|
259
|
+
rules: [
|
|
260
|
+
{
|
|
261
|
+
test: /\.worker\.(c|m)?js$/i,
|
|
262
|
+
loader: "worker-loader",
|
|
263
|
+
options: {
|
|
264
|
+
chunkFilename: "[id].[contenthash].worker.js",
|
|
265
|
+
},
|
|
266
|
+
},
|
|
267
|
+
],
|
|
268
|
+
},
|
|
269
|
+
};
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
### `inline`
|
|
273
|
+
|
|
274
|
+
Type: `'fallback' | 'no-fallback'`
|
|
275
|
+
Default: `undefined`
|
|
276
|
+
|
|
277
|
+
Allow to inline the worker as a `BLOB`.
|
|
278
|
+
|
|
279
|
+
Inline mode with the `fallback` value will create file for browsers without support web workers, to disable this behavior just use `no-fallback` value.
|
|
280
|
+
|
|
281
|
+
**webpack.config.js**
|
|
282
|
+
|
|
283
|
+
```js
|
|
284
|
+
module.exports = {
|
|
285
|
+
module: {
|
|
286
|
+
rules: [
|
|
287
|
+
{
|
|
288
|
+
test: /\.worker\.(c|m)?js$/i,
|
|
289
|
+
loader: "worker-loader",
|
|
290
|
+
options: {
|
|
291
|
+
inline: "fallback",
|
|
292
|
+
},
|
|
293
|
+
},
|
|
294
|
+
],
|
|
295
|
+
},
|
|
296
|
+
};
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
### `esModule`
|
|
300
|
+
|
|
301
|
+
Type: `Boolean`
|
|
302
|
+
Default: `true`
|
|
303
|
+
|
|
304
|
+
By default, `worker-loader` generates JS modules that use the ES modules syntax.
|
|
305
|
+
|
|
306
|
+
You can enable a CommonJS modules syntax using:
|
|
307
|
+
|
|
308
|
+
**webpack.config.js**
|
|
309
|
+
|
|
310
|
+
```js
|
|
311
|
+
module.exports = {
|
|
312
|
+
module: {
|
|
313
|
+
rules: [
|
|
314
|
+
{
|
|
315
|
+
test: /\.worker\.(c|m)?js$/i,
|
|
316
|
+
loader: "worker-loader",
|
|
317
|
+
options: {
|
|
318
|
+
esModule: false,
|
|
319
|
+
},
|
|
320
|
+
},
|
|
321
|
+
],
|
|
322
|
+
},
|
|
323
|
+
};
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
## Examples
|
|
327
|
+
|
|
328
|
+
### Basic
|
|
329
|
+
|
|
330
|
+
The worker file can import dependencies just like any other file:
|
|
331
|
+
|
|
332
|
+
**index.js**
|
|
333
|
+
|
|
334
|
+
```js
|
|
335
|
+
import Worker from "./my.worker.js";
|
|
336
|
+
|
|
337
|
+
var worker = new Worker();
|
|
338
|
+
|
|
339
|
+
var result;
|
|
340
|
+
|
|
341
|
+
worker.onmessage = function (event) {
|
|
342
|
+
if (!result) {
|
|
343
|
+
result = document.createElement("div");
|
|
344
|
+
result.setAttribute("id", "result");
|
|
345
|
+
|
|
346
|
+
document.body.append(result);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
result.innerText = JSON.stringify(event.data);
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
const button = document.getElementById("button");
|
|
353
|
+
|
|
354
|
+
button.addEventListener("click", function () {
|
|
355
|
+
worker.postMessage({ postMessage: true });
|
|
356
|
+
});
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
**my.worker.js**
|
|
360
|
+
|
|
361
|
+
```js
|
|
362
|
+
onmessage = function (event) {
|
|
363
|
+
var workerResult = event.data;
|
|
364
|
+
|
|
365
|
+
workerResult.onmessage = true;
|
|
366
|
+
|
|
367
|
+
postMessage(workerResult);
|
|
368
|
+
};
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
**webpack.config.js**
|
|
372
|
+
|
|
373
|
+
```js
|
|
374
|
+
module.exports = {
|
|
375
|
+
module: {
|
|
376
|
+
rules: [
|
|
377
|
+
{
|
|
378
|
+
test: /\.worker\.(c|m)?js$/i,
|
|
379
|
+
loader: "worker-loader",
|
|
380
|
+
options: {
|
|
381
|
+
esModule: false,
|
|
382
|
+
},
|
|
383
|
+
},
|
|
384
|
+
],
|
|
385
|
+
},
|
|
386
|
+
};
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
### Integrating with ES6+ features
|
|
390
|
+
|
|
391
|
+
You can even use ES6+ features if you have the [`babel-loader`](https://github.com/babel/babel-loader) configured.
|
|
392
|
+
|
|
393
|
+
**index.js**
|
|
394
|
+
|
|
395
|
+
```js
|
|
396
|
+
import Worker from "./my.worker.js";
|
|
397
|
+
|
|
398
|
+
const worker = new Worker();
|
|
399
|
+
|
|
400
|
+
let result;
|
|
401
|
+
|
|
402
|
+
worker.onmessage = (event) => {
|
|
403
|
+
if (!result) {
|
|
404
|
+
result = document.createElement("div");
|
|
405
|
+
result.setAttribute("id", "result");
|
|
406
|
+
|
|
407
|
+
document.body.append(result);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
result.innerText = JSON.stringify(event.data);
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
const button = document.getElementById("button");
|
|
414
|
+
|
|
415
|
+
button.addEventListener("click", () => {
|
|
416
|
+
worker.postMessage({ postMessage: true });
|
|
417
|
+
});
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
**my.worker.js**
|
|
421
|
+
|
|
422
|
+
```js
|
|
423
|
+
onmessage = function (event) {
|
|
424
|
+
const workerResult = event.data;
|
|
425
|
+
|
|
426
|
+
workerResult.onmessage = true;
|
|
427
|
+
|
|
428
|
+
postMessage(workerResult);
|
|
429
|
+
};
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
**webpack.config.js**
|
|
433
|
+
|
|
434
|
+
```js
|
|
435
|
+
module.exports = {
|
|
436
|
+
module: {
|
|
437
|
+
rules: [
|
|
438
|
+
{
|
|
439
|
+
test: /\.worker\.(c|m)?js$/i,
|
|
440
|
+
use: [
|
|
441
|
+
{
|
|
442
|
+
loader: "worker-loader",
|
|
443
|
+
},
|
|
444
|
+
{
|
|
445
|
+
loader: "babel-loader",
|
|
446
|
+
options: {
|
|
447
|
+
presets: ["@babel/preset-env"],
|
|
448
|
+
},
|
|
449
|
+
},
|
|
450
|
+
],
|
|
451
|
+
},
|
|
452
|
+
],
|
|
453
|
+
},
|
|
454
|
+
};
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
### Integrating with TypeScript
|
|
458
|
+
|
|
459
|
+
To integrate with TypeScript, you will need to define a custom module for the exports of your worker.
|
|
460
|
+
|
|
461
|
+
#### Loading with `worker-loader!`
|
|
462
|
+
|
|
463
|
+
**typings/worker-loader.d.ts**
|
|
464
|
+
|
|
465
|
+
```typescript
|
|
466
|
+
declare module "worker-loader!*" {
|
|
467
|
+
// You need to change `Worker`, if you specified a different value for the `workerType` option
|
|
468
|
+
class WebpackWorker extends Worker {
|
|
469
|
+
constructor();
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
// Uncomment this if you set the `esModule` option to `false`
|
|
473
|
+
// export = WebpackWorker;
|
|
474
|
+
export default WebpackWorker;
|
|
475
|
+
}
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
**my.worker.ts**
|
|
479
|
+
|
|
480
|
+
```typescript
|
|
481
|
+
const ctx: Worker = self as any;
|
|
482
|
+
|
|
483
|
+
// Post data to parent thread
|
|
484
|
+
ctx.postMessage({ foo: "foo" });
|
|
485
|
+
|
|
486
|
+
// Respond to message from parent thread
|
|
487
|
+
ctx.addEventListener("message", (event) => console.log(event));
|
|
488
|
+
```
|
|
489
|
+
|
|
490
|
+
**index.ts**
|
|
491
|
+
|
|
492
|
+
```typescript
|
|
493
|
+
import Worker from "worker-loader!./Worker";
|
|
494
|
+
|
|
495
|
+
const worker = new Worker();
|
|
496
|
+
|
|
497
|
+
worker.postMessage({ a: 1 });
|
|
498
|
+
worker.onmessage = (event) => {};
|
|
499
|
+
|
|
500
|
+
worker.addEventListener("message", (event) => {});
|
|
501
|
+
```
|
|
502
|
+
|
|
503
|
+
#### Loading without `worker-loader!`
|
|
504
|
+
|
|
505
|
+
Alternatively, you can omit the `worker-loader!` prefix passed to `import` statement by using the following notation.
|
|
506
|
+
This is useful for executing the code using a non-WebPack runtime environment
|
|
507
|
+
(such as Jest with [`workerloader-jest-transformer`](https://github.com/astagi/workerloader-jest-transformer)).
|
|
508
|
+
|
|
509
|
+
**typings/worker-loader.d.ts**
|
|
510
|
+
|
|
511
|
+
```typescript
|
|
512
|
+
declare module "*.worker.ts" {
|
|
513
|
+
// You need to change `Worker`, if you specified a different value for the `workerType` option
|
|
514
|
+
class WebpackWorker extends Worker {
|
|
515
|
+
constructor();
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// Uncomment this if you set the `esModule` option to `false`
|
|
519
|
+
// export = WebpackWorker;
|
|
520
|
+
export default WebpackWorker;
|
|
521
|
+
}
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
**my.worker.ts**
|
|
525
|
+
|
|
526
|
+
```typescript
|
|
527
|
+
const ctx: Worker = self as any;
|
|
528
|
+
|
|
529
|
+
// Post data to parent thread
|
|
530
|
+
ctx.postMessage({ foo: "foo" });
|
|
531
|
+
|
|
532
|
+
// Respond to message from parent thread
|
|
533
|
+
ctx.addEventListener("message", (event) => console.log(event));
|
|
534
|
+
```
|
|
535
|
+
|
|
536
|
+
**index.ts**
|
|
537
|
+
|
|
538
|
+
```typescript
|
|
539
|
+
import MyWorker from "./my.worker.ts";
|
|
540
|
+
|
|
541
|
+
const worker = new MyWorker();
|
|
542
|
+
|
|
543
|
+
worker.postMessage({ a: 1 });
|
|
544
|
+
worker.onmessage = (event) => {};
|
|
545
|
+
|
|
546
|
+
worker.addEventListener("message", (event) => {});
|
|
547
|
+
```
|
|
548
|
+
|
|
549
|
+
**webpack.config.js**
|
|
550
|
+
|
|
551
|
+
```js
|
|
552
|
+
module.exports = {
|
|
553
|
+
module: {
|
|
554
|
+
rules: [
|
|
555
|
+
// Place this *before* the `ts-loader`.
|
|
556
|
+
{
|
|
557
|
+
test: /\.worker\.ts$/,
|
|
558
|
+
loader: "worker-loader",
|
|
559
|
+
},
|
|
560
|
+
{
|
|
561
|
+
test: /\.ts$/,
|
|
562
|
+
loader: "ts-loader",
|
|
563
|
+
},
|
|
564
|
+
],
|
|
565
|
+
},
|
|
566
|
+
resolve: {
|
|
567
|
+
extensions: [".ts", ".js"],
|
|
568
|
+
},
|
|
569
|
+
};
|
|
570
|
+
```
|
|
571
|
+
|
|
572
|
+
### Cross-Origin Policy
|
|
573
|
+
|
|
574
|
+
[`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.
|
|
575
|
+
This scenario can commonly occur if you are hosting your assets under a CDN domain.
|
|
576
|
+
Even downloads from the `webpack-dev-server` could be blocked.
|
|
577
|
+
|
|
578
|
+
There are two workarounds:
|
|
579
|
+
|
|
580
|
+
Firstly, you can inline the worker as a blob instead of downloading it as an external script via the [`inline`](#inline) parameter
|
|
581
|
+
|
|
582
|
+
**App.js**
|
|
583
|
+
|
|
584
|
+
```js
|
|
585
|
+
import Worker from "./file.worker.js";
|
|
586
|
+
```
|
|
587
|
+
|
|
588
|
+
**webpack.config.js**
|
|
589
|
+
|
|
590
|
+
```js
|
|
591
|
+
module.exports = {
|
|
592
|
+
module: {
|
|
593
|
+
rules: [
|
|
594
|
+
{
|
|
595
|
+
loader: "worker-loader",
|
|
596
|
+
options: { inline: "fallback" },
|
|
597
|
+
},
|
|
598
|
+
],
|
|
599
|
+
},
|
|
600
|
+
};
|
|
601
|
+
```
|
|
602
|
+
|
|
603
|
+
Secondly, you may override the base download URL for your worker script via the [`publicPath`](#publicpath) option
|
|
604
|
+
|
|
605
|
+
**App.js**
|
|
606
|
+
|
|
607
|
+
```js
|
|
608
|
+
// This will cause the worker to be downloaded from `/workers/file.worker.js`
|
|
609
|
+
import Worker from "./file.worker.js";
|
|
610
|
+
```
|
|
611
|
+
|
|
612
|
+
**webpack.config.js**
|
|
613
|
+
|
|
614
|
+
```js
|
|
615
|
+
module.exports = {
|
|
616
|
+
module: {
|
|
617
|
+
rules: [
|
|
618
|
+
{
|
|
619
|
+
loader: "worker-loader",
|
|
620
|
+
options: { publicPath: "/workers/" },
|
|
621
|
+
},
|
|
622
|
+
],
|
|
623
|
+
},
|
|
624
|
+
};
|
|
625
|
+
```
|
|
626
|
+
|
|
627
|
+
## Contributing
|
|
628
|
+
|
|
629
|
+
Please take a moment to read our contributing guidelines if you haven't yet done so.
|
|
630
|
+
|
|
631
|
+
[CONTRIBUTING](./.github/CONTRIBUTING.md)
|
|
632
|
+
|
|
633
|
+
## License
|
|
634
|
+
|
|
635
|
+
[MIT](./LICENSE)
|
|
636
|
+
|
|
637
|
+
[npm]: https://img.shields.io/npm/v/worker-loader.svg
|
|
638
|
+
[npm-url]: https://npmjs.com/package/worker-loader
|
|
639
|
+
[node]: https://img.shields.io/node/v/worker-loader.svg
|
|
640
|
+
[node-url]: https://nodejs.org
|
|
641
|
+
[deps]: https://david-dm.org/webpack-contrib/worker-loader.svg
|
|
642
|
+
[deps-url]: https://david-dm.org/webpack-contrib/worker-loader
|
|
643
|
+
[tests]: https://github.com/webpack-contrib/worker-loader/workflows/worker-loader/badge.svg
|
|
644
|
+
[tests-url]: https://github.com/webpack-contrib/worker-loader/actions
|
|
645
|
+
[cover]: https://codecov.io/gh/webpack-contrib/worker-loader/branch/master/graph/badge.svg
|
|
646
|
+
[cover-url]: https://codecov.io/gh/webpack-contrib/worker-loader
|
|
647
|
+
[chat]: https://badges.gitter.im/webpack/webpack.svg
|
|
648
|
+
[chat-url]: https://gitter.im/webpack/webpack
|
|
649
|
+
[size]: https://packagephobia.now.sh/badge?p=worker-loader
|
|
650
|
+
[size-url]: https://packagephobia.now.sh/result?p=worker-loader
|
package/dist/cjs.js
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = loader;
|
|
7
|
+
exports.pitch = pitch;
|
|
8
|
+
|
|
9
|
+
var _path = _interopRequireDefault(require("path"));
|
|
10
|
+
|
|
11
|
+
var _loaderUtils = require("loader-utils");
|
|
12
|
+
|
|
13
|
+
var _schemaUtils = require("schema-utils");
|
|
14
|
+
|
|
15
|
+
var _NodeTargetPlugin = _interopRequireDefault(require("webpack/lib/node/NodeTargetPlugin"));
|
|
16
|
+
|
|
17
|
+
var _SingleEntryPlugin = _interopRequireDefault(require("webpack/lib/SingleEntryPlugin"));
|
|
18
|
+
|
|
19
|
+
var _WebWorkerTemplatePlugin = _interopRequireDefault(require("webpack/lib/webworker/WebWorkerTemplatePlugin"));
|
|
20
|
+
|
|
21
|
+
var _ExternalsPlugin = _interopRequireDefault(require("webpack/lib/ExternalsPlugin"));
|
|
22
|
+
|
|
23
|
+
var _options = _interopRequireDefault(require("./options.json"));
|
|
24
|
+
|
|
25
|
+
var _supportWebpack = _interopRequireDefault(require("./supportWebpack5"));
|
|
26
|
+
|
|
27
|
+
var _supportWebpack2 = _interopRequireDefault(require("./supportWebpack4"));
|
|
28
|
+
|
|
29
|
+
var _utils = require("./utils");
|
|
30
|
+
|
|
31
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
32
|
+
|
|
33
|
+
let FetchCompileWasmPlugin;
|
|
34
|
+
let FetchCompileAsyncWasmPlugin; // determine the version of webpack peer dependency
|
|
35
|
+
// eslint-disable-next-line global-require, import/no-unresolved
|
|
36
|
+
|
|
37
|
+
const useWebpack5 = require("webpack/package.json").version.startsWith("5.");
|
|
38
|
+
|
|
39
|
+
if (useWebpack5) {
|
|
40
|
+
// eslint-disable-next-line global-require, import/no-unresolved
|
|
41
|
+
FetchCompileWasmPlugin = require("webpack/lib/web/FetchCompileWasmPlugin"); // eslint-disable-next-line global-require, import/no-unresolved
|
|
42
|
+
|
|
43
|
+
FetchCompileAsyncWasmPlugin = require("webpack/lib/web/FetchCompileAsyncWasmPlugin");
|
|
44
|
+
} else {
|
|
45
|
+
// eslint-disable-next-line global-require, import/no-unresolved, import/extensions
|
|
46
|
+
FetchCompileWasmPlugin = require("webpack/lib/web/FetchCompileWasmTemplatePlugin");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function loader() {}
|
|
50
|
+
|
|
51
|
+
function pitch(request) {
|
|
52
|
+
this.cacheable(false);
|
|
53
|
+
const options = (0, _loaderUtils.getOptions)(this);
|
|
54
|
+
(0, _schemaUtils.validate)(_options.default, options, {
|
|
55
|
+
name: "Worker Loader",
|
|
56
|
+
baseDataPath: "options"
|
|
57
|
+
});
|
|
58
|
+
const workerContext = {};
|
|
59
|
+
const compilerOptions = this._compiler.options || {};
|
|
60
|
+
const filename = options.filename ? options.filename : (0, _utils.getDefaultFilename)(compilerOptions.output.filename);
|
|
61
|
+
const chunkFilename = options.chunkFilename ? options.chunkFilename : (0, _utils.getDefaultChunkFilename)(compilerOptions.output.chunkFilename);
|
|
62
|
+
const publicPath = options.publicPath ? options.publicPath : compilerOptions.output.publicPath;
|
|
63
|
+
workerContext.options = {
|
|
64
|
+
filename,
|
|
65
|
+
chunkFilename,
|
|
66
|
+
publicPath,
|
|
67
|
+
globalObject: "self"
|
|
68
|
+
};
|
|
69
|
+
workerContext.compiler = this._compilation.createChildCompiler(`worker-loader ${request}`, workerContext.options);
|
|
70
|
+
new _WebWorkerTemplatePlugin.default().apply(workerContext.compiler);
|
|
71
|
+
|
|
72
|
+
if (this.target !== "webworker" && this.target !== "web") {
|
|
73
|
+
new _NodeTargetPlugin.default().apply(workerContext.compiler);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (FetchCompileWasmPlugin) {
|
|
77
|
+
new FetchCompileWasmPlugin({
|
|
78
|
+
mangleImports: compilerOptions.optimization.mangleWasmImports
|
|
79
|
+
}).apply(workerContext.compiler);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (FetchCompileAsyncWasmPlugin) {
|
|
83
|
+
new FetchCompileAsyncWasmPlugin().apply(workerContext.compiler);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (compilerOptions.externals) {
|
|
87
|
+
new _ExternalsPlugin.default((0, _utils.getExternalsType)(compilerOptions), compilerOptions.externals).apply(workerContext.compiler);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
new _SingleEntryPlugin.default(this.context, `!!${request}`, _path.default.parse(this.resourcePath).name).apply(workerContext.compiler);
|
|
91
|
+
workerContext.request = request;
|
|
92
|
+
const cb = this.async();
|
|
93
|
+
|
|
94
|
+
if (workerContext.compiler.cache && typeof workerContext.compiler.cache.get === "function") {
|
|
95
|
+
(0, _supportWebpack.default)(this, workerContext, options, cb);
|
|
96
|
+
} else {
|
|
97
|
+
(0, _supportWebpack2.default)(this, workerContext, options, cb);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
{
|
|
2
|
+
"type": "object",
|
|
3
|
+
"properties": {
|
|
4
|
+
"worker": {
|
|
5
|
+
"anyOf": [
|
|
6
|
+
{
|
|
7
|
+
"type": "string",
|
|
8
|
+
"minLength": 1
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
"type": "object",
|
|
12
|
+
"additionalProperties": false,
|
|
13
|
+
"properties": {
|
|
14
|
+
"type": {
|
|
15
|
+
"type": "string",
|
|
16
|
+
"minLength": 1
|
|
17
|
+
},
|
|
18
|
+
"options": {
|
|
19
|
+
"additionalProperties": true,
|
|
20
|
+
"type": "object"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"required": ["type"]
|
|
24
|
+
}
|
|
25
|
+
],
|
|
26
|
+
"description": "Set the worker type.",
|
|
27
|
+
"link": "https://github.com/webpack-contrib/worker-loader#worker"
|
|
28
|
+
},
|
|
29
|
+
"publicPath": {
|
|
30
|
+
"anyOf": [
|
|
31
|
+
{
|
|
32
|
+
"type": "string"
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
"instanceof": "Function"
|
|
36
|
+
}
|
|
37
|
+
],
|
|
38
|
+
"description": "Specifies the public URL address of the output files when referenced in a browser.",
|
|
39
|
+
"link": "https://github.com/webpack-contrib/worker-loader#publicpath"
|
|
40
|
+
},
|
|
41
|
+
"filename": {
|
|
42
|
+
"anyOf": [
|
|
43
|
+
{
|
|
44
|
+
"type": "string",
|
|
45
|
+
"minLength": 1
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
"instanceof": "Function"
|
|
49
|
+
}
|
|
50
|
+
],
|
|
51
|
+
"description": "The filename of entry chunks for web workers.",
|
|
52
|
+
"link": "https://github.com/webpack-contrib/worker-loader#filename"
|
|
53
|
+
},
|
|
54
|
+
"chunkFilename": {
|
|
55
|
+
"type": "string",
|
|
56
|
+
"description": "The filename of non-entry chunks for web workers.",
|
|
57
|
+
"link": "https://github.com/webpack-contrib/worker-loader#chunkfilename",
|
|
58
|
+
"minLength": 1
|
|
59
|
+
},
|
|
60
|
+
"inline": {
|
|
61
|
+
"enum": ["no-fallback", "fallback"],
|
|
62
|
+
"description": "Allow to inline the worker as a BLOB.",
|
|
63
|
+
"link": "https://github.com/webpack-contrib/worker-loader#inline"
|
|
64
|
+
},
|
|
65
|
+
"esModule": {
|
|
66
|
+
"type": "boolean",
|
|
67
|
+
"description": "Enable or disable ES module syntax.",
|
|
68
|
+
"link": "https://github.com/webpack-contrib/worker-loader#esmodule"
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
"additionalProperties": false
|
|
72
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/* eslint-env browser */
|
|
4
|
+
|
|
5
|
+
/* eslint-disable no-undef, no-use-before-define, new-cap */
|
|
6
|
+
module.exports = function (content, workerConstructor, workerOptions, url) {
|
|
7
|
+
var globalScope = self || window;
|
|
8
|
+
|
|
9
|
+
try {
|
|
10
|
+
try {
|
|
11
|
+
var blob;
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
// New API
|
|
15
|
+
blob = new globalScope.Blob([content]);
|
|
16
|
+
} catch (e) {
|
|
17
|
+
// BlobBuilder = Deprecated, but widely implemented
|
|
18
|
+
var BlobBuilder = globalScope.BlobBuilder || globalScope.WebKitBlobBuilder || globalScope.MozBlobBuilder || globalScope.MSBlobBuilder;
|
|
19
|
+
blob = new BlobBuilder();
|
|
20
|
+
blob.append(content);
|
|
21
|
+
blob = blob.getBlob();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
var URL = globalScope.URL || globalScope.webkitURL;
|
|
25
|
+
var objectURL = URL.createObjectURL(blob);
|
|
26
|
+
var worker = new globalScope[workerConstructor](objectURL, workerOptions);
|
|
27
|
+
URL.revokeObjectURL(objectURL);
|
|
28
|
+
return worker;
|
|
29
|
+
} catch (e) {
|
|
30
|
+
return new globalScope[workerConstructor]("data:application/javascript,".concat(encodeURIComponent(content)), workerOptions);
|
|
31
|
+
}
|
|
32
|
+
} catch (e) {
|
|
33
|
+
if (!url) {
|
|
34
|
+
throw Error("Inline worker is not supported");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return new globalScope[workerConstructor](url, workerOptions);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = runAsChild;
|
|
7
|
+
|
|
8
|
+
var _utils = require("./utils");
|
|
9
|
+
|
|
10
|
+
function runAsChild(loaderContext, workerContext, options, callback) {
|
|
11
|
+
workerContext.compiler.runAsChild((error, entries, compilation) => {
|
|
12
|
+
if (error) {
|
|
13
|
+
return callback(error);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (entries[0]) {
|
|
17
|
+
// eslint-disable-next-line no-param-reassign, prefer-destructuring
|
|
18
|
+
const workerFilename = entries[0].files[0];
|
|
19
|
+
let workerSource = compilation.assets[workerFilename].source();
|
|
20
|
+
|
|
21
|
+
if (options.inline === "no-fallback") {
|
|
22
|
+
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
|
|
23
|
+
delete loaderContext._compilation.assets[workerFilename]; // TODO improve it, we should store generated source maps files for file in `assetInfo`
|
|
24
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
25
|
+
|
|
26
|
+
if (loaderContext._compilation.assets[`${workerFilename}.map`]) {
|
|
27
|
+
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
|
|
28
|
+
delete loaderContext._compilation.assets[`${workerFilename}.map`];
|
|
29
|
+
} // Remove `/* sourceMappingURL=url */` comment
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
workerSource = workerSource.replace(_utils.sourceMappingURLRegex, ""); // Remove `//# sourceURL=webpack-internal` comment
|
|
33
|
+
|
|
34
|
+
workerSource = workerSource.replace(_utils.sourceURLWebpackRegex, "");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const workerCode = (0, _utils.workerGenerator)(loaderContext, workerFilename, workerSource, options);
|
|
38
|
+
return callback(null, workerCode);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return callback(new Error(`Failed to compile web worker "${workerContext.request}" request`));
|
|
42
|
+
});
|
|
43
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = runAsChild;
|
|
7
|
+
|
|
8
|
+
var _utils = require("./utils");
|
|
9
|
+
|
|
10
|
+
function runAsChild(loaderContext, workerContext, options, callback) {
|
|
11
|
+
workerContext.compiler.runAsChild((error, entries, compilation) => {
|
|
12
|
+
if (error) {
|
|
13
|
+
return callback(error);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (entries[0]) {
|
|
17
|
+
const [workerFilename] = [...entries[0].files];
|
|
18
|
+
const cache = workerContext.compiler.getCache("worker-loader");
|
|
19
|
+
const cacheIdent = workerFilename;
|
|
20
|
+
const cacheETag = cache.getLazyHashedEtag(compilation.assets[workerFilename]);
|
|
21
|
+
return cache.get(cacheIdent, cacheETag, (getCacheError, content) => {
|
|
22
|
+
if (getCacheError) {
|
|
23
|
+
return callback(getCacheError);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (options.inline === "no-fallback") {
|
|
27
|
+
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
|
|
28
|
+
delete loaderContext._compilation.assets[workerFilename]; // TODO improve this, we should store generated source maps files for file in `assetInfo`
|
|
29
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
30
|
+
|
|
31
|
+
if (loaderContext._compilation.assets[`${workerFilename}.map`]) {
|
|
32
|
+
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
|
|
33
|
+
delete loaderContext._compilation.assets[`${workerFilename}.map`];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (content) {
|
|
38
|
+
return callback(null, content);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
let workerSource = compilation.assets[workerFilename].source();
|
|
42
|
+
|
|
43
|
+
if (options.inline === "no-fallback") {
|
|
44
|
+
// Remove `/* sourceMappingURL=url */` comment
|
|
45
|
+
workerSource = workerSource.replace(_utils.sourceMappingURLRegex, ""); // Remove `//# sourceURL=webpack-internal` comment
|
|
46
|
+
|
|
47
|
+
workerSource = workerSource.replace(_utils.sourceURLWebpackRegex, "");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const workerCode = (0, _utils.workerGenerator)(loaderContext, workerFilename, workerSource, options);
|
|
51
|
+
const workerCodeBuffer = Buffer.from(workerCode);
|
|
52
|
+
return cache.store(cacheIdent, cacheETag, workerCodeBuffer, storeCacheError => {
|
|
53
|
+
if (storeCacheError) {
|
|
54
|
+
return callback(storeCacheError);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return callback(null, workerCodeBuffer);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return callback(new Error(`Failed to compile web worker "${workerContext.request}" request`));
|
|
63
|
+
});
|
|
64
|
+
}
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getDefaultFilename = getDefaultFilename;
|
|
7
|
+
exports.getDefaultChunkFilename = getDefaultChunkFilename;
|
|
8
|
+
exports.getExternalsType = getExternalsType;
|
|
9
|
+
exports.workerGenerator = workerGenerator;
|
|
10
|
+
exports.sourceURLWebpackRegex = exports.sourceMappingURLRegex = void 0;
|
|
11
|
+
|
|
12
|
+
var _loaderUtils = require("loader-utils");
|
|
13
|
+
|
|
14
|
+
function getDefaultFilename(filename) {
|
|
15
|
+
if (typeof filename === "function") {
|
|
16
|
+
return filename;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return filename.replace(/\.([a-z]+)(\?.+)?$/i, ".worker.$1$2");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function getDefaultChunkFilename(chunkFilename) {
|
|
23
|
+
return chunkFilename.replace(/\.([a-z]+)(\?.+)?$/i, ".worker.$1$2");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function getExternalsType(compilerOptions) {
|
|
27
|
+
// For webpack@4
|
|
28
|
+
if (compilerOptions.output.libraryTarget) {
|
|
29
|
+
return compilerOptions.output.libraryTarget;
|
|
30
|
+
} // For webpack@5
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
if (compilerOptions.externalsType) {
|
|
34
|
+
return compilerOptions.externalsType;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (compilerOptions.output.library) {
|
|
38
|
+
return compilerOptions.output.library.type;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (compilerOptions.output.module) {
|
|
42
|
+
return "module";
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return "var";
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function workerGenerator(loaderContext, workerFilename, workerSource, options) {
|
|
49
|
+
let workerConstructor;
|
|
50
|
+
let workerOptions;
|
|
51
|
+
|
|
52
|
+
if (typeof options.worker === "undefined") {
|
|
53
|
+
workerConstructor = "Worker";
|
|
54
|
+
} else if (typeof options.worker === "string") {
|
|
55
|
+
workerConstructor = options.worker;
|
|
56
|
+
} else {
|
|
57
|
+
({
|
|
58
|
+
type: workerConstructor,
|
|
59
|
+
options: workerOptions
|
|
60
|
+
} = options.worker);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const esModule = typeof options.esModule !== "undefined" ? options.esModule : true;
|
|
64
|
+
const fnName = `${workerConstructor}_fn`;
|
|
65
|
+
const publicPath = options.publicPath ? `"${options.publicPath}"` : "__webpack_public_path__";
|
|
66
|
+
|
|
67
|
+
if (options.inline) {
|
|
68
|
+
const InlineWorkerPath = (0, _loaderUtils.stringifyRequest)(loaderContext, `!!${require.resolve("./runtime/inline.js")}`);
|
|
69
|
+
let fallbackWorkerPath;
|
|
70
|
+
|
|
71
|
+
if (options.inline === "fallback") {
|
|
72
|
+
fallbackWorkerPath = `${publicPath} + ${JSON.stringify(workerFilename)}`;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return `
|
|
76
|
+
${esModule ? `import worker from ${InlineWorkerPath};` : `var worker = require(${InlineWorkerPath});`}
|
|
77
|
+
|
|
78
|
+
${esModule ? "export default" : "module.exports ="} function ${fnName}() {\n return worker(${JSON.stringify(workerSource)}, ${JSON.stringify(workerConstructor)}, ${JSON.stringify(workerOptions)}, ${fallbackWorkerPath});\n}\n`;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return `${esModule ? "export default" : "module.exports ="} function ${fnName}() {\n return new ${workerConstructor}(${publicPath} + ${JSON.stringify(workerFilename)}${workerOptions ? `, ${JSON.stringify(workerOptions)}` : ""});\n}\n`;
|
|
82
|
+
} // Matches only the last occurrence of sourceMappingURL
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
const innerRegex = /\s*[#@]\s*sourceMappingURL\s*=\s*(.*?(?=[\s'"]|\\n|\*\/|$)(?:\\n)?)\s*/;
|
|
86
|
+
/* eslint-disable prefer-template */
|
|
87
|
+
|
|
88
|
+
const sourceMappingURLRegex = RegExp("(?:" + "/\\*" + "(?:\\s*\r?\n(?://)?)?" + "(?:" + innerRegex.source + ")" + "\\s*" + "\\*/" + "|" + "//(?:" + innerRegex.source + ")" + ")" + "\\s*");
|
|
89
|
+
exports.sourceMappingURLRegex = sourceMappingURLRegex;
|
|
90
|
+
const sourceURLWebpackRegex = RegExp("\\/\\/#\\ssourceURL=webpack-internal:\\/\\/\\/(.*?)\\\\n");
|
|
91
|
+
/* eslint-enable prefer-template */
|
|
92
|
+
|
|
93
|
+
exports.sourceURLWebpackRegex = sourceURLWebpackRegex;
|
package/package.json
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "codingclip-worker-loader",
|
|
3
|
+
"version": "3.0.8",
|
|
4
|
+
"description": "worker loader module for webpack",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": "webpack-contrib/worker-loader",
|
|
7
|
+
"author": "Tobias Koppers @sokra",
|
|
8
|
+
"homepage": "https://github.com/webpack-contrib/worker-loader",
|
|
9
|
+
"bugs": "https://github.com/webpack-contrib/worker-loader/issues",
|
|
10
|
+
"funding": {
|
|
11
|
+
"type": "opencollective",
|
|
12
|
+
"url": "https://opencollective.com/webpack"
|
|
13
|
+
},
|
|
14
|
+
"main": "dist/cjs.js",
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">= 10.13.0"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"start": "npm run build -- -w",
|
|
20
|
+
"clean": "del-cli dist",
|
|
21
|
+
"prebuild": "npm run clean",
|
|
22
|
+
"build": "cross-env NODE_ENV=production babel src -d dist --copy-files",
|
|
23
|
+
"commitlint": "commitlint --from=master",
|
|
24
|
+
"security": "npm audit",
|
|
25
|
+
"lint:prettier": "prettier --list-different .",
|
|
26
|
+
"lint:js": "eslint --cache .",
|
|
27
|
+
"lint": "npm-run-all -l -p \"lint:**\"",
|
|
28
|
+
"test:only": "cross-env NODE_ENV=test jest",
|
|
29
|
+
"test:watch": "npm run test:only -- --watch",
|
|
30
|
+
"test:coverage": "npm run test:only -- --collectCoverageFrom=\"src/**/*.js\" --coverage",
|
|
31
|
+
"pretest": "npm run lint",
|
|
32
|
+
"test": "npm run test:coverage",
|
|
33
|
+
"prepare": "npm run build",
|
|
34
|
+
"release": "standard-version"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist"
|
|
38
|
+
],
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"webpack": "^4.0.0 || ^5.0.0"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"loader-utils": "^2.0.0",
|
|
44
|
+
"schema-utils": "^3.1.0"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@babel/cli": "^7.14.5",
|
|
48
|
+
"@babel/core": "^7.14.6",
|
|
49
|
+
"@babel/preset-env": "^7.14.7",
|
|
50
|
+
"@commitlint/cli": "^12.1.4",
|
|
51
|
+
"@commitlint/config-conventional": "^12.1.4",
|
|
52
|
+
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
|
|
53
|
+
"babel-jest": "^26.6.3",
|
|
54
|
+
"cross-env": "^7.0.3",
|
|
55
|
+
"del": "^6.0.0",
|
|
56
|
+
"del-cli": "^3.0.1",
|
|
57
|
+
"eslint": "^7.31.0",
|
|
58
|
+
"eslint-config-prettier": "^8.3.0",
|
|
59
|
+
"eslint-plugin-import": "^2.23.4",
|
|
60
|
+
"express": "^4.17.1",
|
|
61
|
+
"get-port": "^5.1.1",
|
|
62
|
+
"html-webpack-plugin": "^4.5.0",
|
|
63
|
+
"husky": "^4.3.0",
|
|
64
|
+
"jest": "^26.6.3",
|
|
65
|
+
"lint-staged": "^10.5.4",
|
|
66
|
+
"memfs": "^3.2.0",
|
|
67
|
+
"nanoid": "^3.1.20",
|
|
68
|
+
"npm-run-all": "^4.1.5",
|
|
69
|
+
"prettier": "^2.3.2",
|
|
70
|
+
"puppeteer": "^7.0.4",
|
|
71
|
+
"standard-version": "^9.3.1",
|
|
72
|
+
"webpack": "^5.45.1"
|
|
73
|
+
},
|
|
74
|
+
"keywords": [
|
|
75
|
+
"webpack"
|
|
76
|
+
],
|
|
77
|
+
"jest": {
|
|
78
|
+
"testEnvironment": "node"
|
|
79
|
+
}
|
|
80
|
+
}
|