From ee519b1d283dbb599385fe2932c99c929b09db36 Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Wed, 5 Aug 2020 19:12:36 +0300 Subject: [PATCH] fix: compatibility with webpack@5 cache (#279) --- src/supportWebpack4.js | 6 ++- src/supportWebpack5.js | 103 +++++++++++++++++++---------------------- 2 files changed, 53 insertions(+), 56 deletions(-) diff --git a/src/supportWebpack4.js b/src/supportWebpack4.js index cbdd762..fec90d4 100644 --- a/src/supportWebpack4.js +++ b/src/supportWebpack4.js @@ -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` + ) + ); }); } diff --git a/src/supportWebpack5.js b/src/supportWebpack5.js index 9d9164a..6c6f910 100644 --- a/src/supportWebpack5.js +++ b/src/supportWebpack5.js @@ -1,5 +1,3 @@ -import { stringifyRequest } from 'loader-utils'; - import { workerGenerator, sourceMappingURLRegex } from './utils'; export default function runAsChild( @@ -8,9 +6,6 @@ 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); @@ -18,68 +13,66 @@ export default function runAsChild( 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` + ) + ); }); }