Skip to content
This repository was archived by the owner on Sep 9, 2021. It is now read-only.

Commit ee519b1

Browse files
fix: compatibility with webpack@5 cache (#279)
1 parent 9ed9e42 commit ee519b1

File tree

2 files changed

+53
-56
lines changed

2 files changed

+53
-56
lines changed

src/supportWebpack4.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export default function runAsChild(
4242
return callback(null, workerCode);
4343
}
4444

45-
return callback(null, null);
45+
return callback(
46+
new Error(
47+
`Failed to compile web worker "${workerContext.request}" request`
48+
)
49+
);
4650
});
4751
}

src/supportWebpack5.js

Lines changed: 48 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { stringifyRequest } from 'loader-utils';
2-
31
import { workerGenerator, sourceMappingURLRegex } from './utils';
42

53
export default function runAsChild(
@@ -8,78 +6,73 @@ export default function runAsChild(
86
options,
97
callback
108
) {
11-
// eslint-disable-next-line import/no-unresolved, global-require
12-
const getLazyHashedEtag = require('webpack/lib/cache/getLazyHashedEtag');
13-
149
workerContext.compiler.runAsChild((error, entries, compilation) => {
1510
if (error) {
1611
return callback(error);
1712
}
1813

1914
if (entries[0]) {
2015
const [workerFilename] = [...entries[0].files];
21-
const requestIdent = stringifyRequest(
22-
{ context: loaderContext.rootContext },
23-
workerContext.request
16+
const cache = workerContext.compiler.getCache('worker-loader');
17+
const cacheIdent = workerFilename;
18+
const cacheETag = cache.getLazyHashedEtag(
19+
compilation.assets[workerFilename]
2420
);
25-
const cacheIdent = `${workerContext.compiler.compilerPath}/worker-loader|${requestIdent}`;
26-
const cacheETag = getLazyHashedEtag(compilation.assets[workerFilename]);
2721

28-
// TODO not working, need fix on webpack@5 side
29-
return workerContext.compiler.cache.get(
30-
cacheIdent,
31-
cacheETag,
32-
(getCacheError, content) => {
33-
if (getCacheError) {
34-
return callback(getCacheError);
35-
}
36-
37-
if (options.inline === 'no-fallback') {
38-
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
39-
delete loaderContext._compilation.assets[workerFilename];
22+
return cache.get(cacheIdent, cacheETag, (getCacheError, content) => {
23+
if (getCacheError) {
24+
return callback(getCacheError);
25+
}
4026

41-
// TODO improve this, we should store generated source maps files for file in `assetInfo`
42-
// eslint-disable-next-line no-underscore-dangle
43-
if (loaderContext._compilation.assets[`${workerFilename}.map`]) {
44-
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
45-
delete loaderContext._compilation.assets[`${workerFilename}.map`];
46-
}
47-
}
27+
if (options.inline === 'no-fallback') {
28+
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
29+
delete loaderContext._compilation.assets[workerFilename];
4830

49-
if (content) {
50-
return callback(null, content);
31+
// TODO improve this, we should store generated source maps files for file in `assetInfo`
32+
// eslint-disable-next-line no-underscore-dangle
33+
if (loaderContext._compilation.assets[`${workerFilename}.map`]) {
34+
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
35+
delete loaderContext._compilation.assets[`${workerFilename}.map`];
5136
}
37+
}
5238

53-
let workerSource = compilation.assets[workerFilename].source();
39+
if (content) {
40+
return callback(null, content);
41+
}
5442

55-
if (options.inline === 'no-fallback') {
56-
// Remove `/* sourceMappingURL=url */` comment
57-
workerSource = workerSource.replace(sourceMappingURLRegex, '');
58-
}
43+
let workerSource = compilation.assets[workerFilename].source();
5944

60-
const workerCode = workerGenerator(
61-
loaderContext,
62-
workerFilename,
63-
workerSource,
64-
options
65-
);
45+
if (options.inline === 'no-fallback') {
46+
// Remove `/* sourceMappingURL=url */` comment
47+
workerSource = workerSource.replace(sourceMappingURLRegex, '');
48+
}
6649

67-
return workerContext.compiler.cache.store(
68-
cacheIdent,
69-
cacheETag,
70-
workerCode,
71-
(storeCacheError) => {
72-
if (storeCacheError) {
73-
return callback(storeCacheError);
74-
}
50+
const workerCode = workerGenerator(
51+
loaderContext,
52+
workerFilename,
53+
workerSource,
54+
options
55+
);
7556

76-
return callback(null, workerCode);
57+
return cache.store(
58+
cacheIdent,
59+
cacheETag,
60+
workerCode,
61+
(storeCacheError) => {
62+
if (storeCacheError) {
63+
return callback(storeCacheError);
7764
}
78-
);
79-
}
80-
);
65+
66+
return callback(null, workerCode);
67+
}
68+
);
69+
});
8170
}
8271

83-
return callback(null, null);
72+
return callback(
73+
new Error(
74+
`Failed to compile web worker "${workerContext.request}" request`
75+
)
76+
);
8477
});
8578
}

0 commit comments

Comments
 (0)