UNPKG

2.39 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = runAsChild;
7
8var _utils = require("./utils");
9
10function 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}
\No newline at end of file