This repository was archived by the owner on Sep 9, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 276
/
Copy pathsupportWebpack5.js
86 lines (73 loc) · 2.46 KB
/
supportWebpack5.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import {
workerGenerator,
sourceMappingURLRegex,
sourceURLWebpackRegex,
} from "./utils";
export default function runAsChild(
loaderContext,
workerContext,
options,
callback
) {
workerContext.compiler.runAsChild((error, entries, compilation) => {
if (error) {
return callback(error);
}
if (entries[0]) {
const [workerFilename] = [...entries[0].files];
const cache = workerContext.compiler.getCache("worker-loader");
const cacheIdent = workerFilename;
const cacheETag = cache.getLazyHashedEtag(
compilation.assets[workerFilename]
);
return cache.get(cacheIdent, cacheETag, (getCacheError, content) => {
if (getCacheError) {
return callback(getCacheError);
}
if (options.inline === "no-fallback") {
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
delete loaderContext._compilation.assets[workerFilename];
// TODO improve this, we should store generated source maps files for file in `assetInfo`
// eslint-disable-next-line no-underscore-dangle
if (loaderContext._compilation.assets[`${workerFilename}.map`]) {
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
delete loaderContext._compilation.assets[`${workerFilename}.map`];
}
}
if (content) {
return callback(null, content);
}
let workerSource = compilation.assets[workerFilename].source();
if (options.inline === "no-fallback") {
// Remove `/* sourceMappingURL=url */` comment
workerSource = workerSource.replace(sourceMappingURLRegex, "");
// Remove `//# sourceURL=webpack-internal` comment
workerSource = workerSource.replace(sourceURLWebpackRegex, "");
}
const workerCode = workerGenerator(
loaderContext,
workerFilename,
workerSource,
options
);
const workerCodeBuffer = Buffer.from(workerCode);
return cache.store(
cacheIdent,
cacheETag,
workerCodeBuffer,
(storeCacheError) => {
if (storeCacheError) {
return callback(storeCacheError);
}
return callback(null, workerCodeBuffer);
}
);
});
}
return callback(
new Error(
`Failed to compile web worker "${workerContext.request}" request`
)
);
});
}