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 |
|
28 | delete loaderContext._compilation.assets[workerFilename];
|
29 |
|
30 |
|
31 | if (loaderContext._compilation.assets[`${workerFilename}.map`]) {
|
32 |
|
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 |
|
45 | workerSource = workerSource.replace(_utils.sourceMappingURLRegex, "");
|
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 | } |
\ | No newline at end of file |