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

Commit

Permalink
fix: compatibility with webpack@5 cache (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Aug 5, 2020
1 parent 9ed9e42 commit ee519b1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 56 deletions.
6 changes: 5 additions & 1 deletion src/supportWebpack4.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export default function runAsChild(
return callback(null, workerCode);
}

return callback(null, null);
return callback(
new Error(
`Failed to compile web worker "${workerContext.request}" request`
)
);
});
}
103 changes: 48 additions & 55 deletions src/supportWebpack5.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { stringifyRequest } from 'loader-utils';

import { workerGenerator, sourceMappingURLRegex } from './utils';

export default function runAsChild(
Expand All @@ -8,78 +6,73 @@ export default function runAsChild(
options,
callback
) {
// eslint-disable-next-line import/no-unresolved, global-require
const getLazyHashedEtag = require('webpack/lib/cache/getLazyHashedEtag');

workerContext.compiler.runAsChild((error, entries, compilation) => {
if (error) {
return callback(error);
}

if (entries[0]) {
const [workerFilename] = [...entries[0].files];
const requestIdent = stringifyRequest(
{ context: loaderContext.rootContext },
workerContext.request
const cache = workerContext.compiler.getCache('worker-loader');
const cacheIdent = workerFilename;
const cacheETag = cache.getLazyHashedEtag(
compilation.assets[workerFilename]
);
const cacheIdent = `${workerContext.compiler.compilerPath}/worker-loader|${requestIdent}`;
const cacheETag = getLazyHashedEtag(compilation.assets[workerFilename]);

// TODO not working, need fix on webpack@5 side
return workerContext.compiler.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];
return cache.get(cacheIdent, cacheETag, (getCacheError, content) => {
if (getCacheError) {
return callback(getCacheError);
}

// 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 (options.inline === 'no-fallback') {
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
delete loaderContext._compilation.assets[workerFilename];

if (content) {
return callback(null, content);
// 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`];
}
}

let workerSource = compilation.assets[workerFilename].source();
if (content) {
return callback(null, content);
}

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

const workerCode = workerGenerator(
loaderContext,
workerFilename,
workerSource,
options
);
if (options.inline === 'no-fallback') {
// Remove `/* sourceMappingURL=url */` comment
workerSource = workerSource.replace(sourceMappingURLRegex, '');
}

return workerContext.compiler.cache.store(
cacheIdent,
cacheETag,
workerCode,
(storeCacheError) => {
if (storeCacheError) {
return callback(storeCacheError);
}
const workerCode = workerGenerator(
loaderContext,
workerFilename,
workerSource,
options
);

return callback(null, workerCode);
return cache.store(
cacheIdent,
cacheETag,
workerCode,
(storeCacheError) => {
if (storeCacheError) {
return callback(storeCacheError);
}
);
}
);

return callback(null, workerCode);
}
);
});
}

return callback(null, null);
return callback(
new Error(
`Failed to compile web worker "${workerContext.request}" request`
)
);
});
}

0 comments on commit ee519b1

Please sign in to comment.