diff --git a/src/index.js b/src/index.js index 23bc6ad..afae972 100644 --- a/src/index.js +++ b/src/index.js @@ -42,7 +42,6 @@ module.exports.pitch = function pitch(request) { throw new Error("Only usable with webpack"); } - this.cacheable(false); const callback = this.async(); const options = loaderUtils.getOptions(this) || {}; const compilerOptions = this._compiler.options || {}; @@ -89,22 +88,38 @@ module.exports.pitch = function pitch(request) { } new SingleEntryPlugin(this.context, `!!${request}`, "main").apply(workerCompiler); - - const subCache = `subcache ${__dirname} ${request}`; const plugin = { name: "WorkerLoader" }; - workerCompiler.hooks.compilation.tap(plugin, compilation => { - if (compilation.cache) { - compilation.cache = compilation.cache[subCache] || {}; - } + workerCompiler.hooks.thisCompilation.tap(plugin, compilation => { + /** + * A dirty hack to disable HMR plugin in childCompilation: + * https://github.com/webpack/webpack/blob/4056506488c1e071dfc9a0127daa61bf531170bf/lib/HotModuleReplacementPlugin.js#L154 + * + * Once we update to webpack@4.40.3 and above this can be removed: + * https://github.com/webpack/webpack/commit/1c4138d6ac04b7b47daa5ec4475c0ae1b4f596a2 + */ + compilation.hotUpdateChunkTemplate = null; }); - workerCompiler.runAsChild((err, entries) => { + workerCompiler.runAsChild((err, entries, childCompilation) => { if (err) { return callback(err); } if (entries[0]) { + const fileDeps = Array.from(childCompilation.fileDependencies); + this.clearDependencies(); + fileDeps.map(fileName => { + this.addDependency(fileName); + }); + /** + * Clears the hash of the child compilation as it affects the hash of the parent compilation: + * https://github.com/webpack/webpack/blob/4056506488c1e071dfc9a0127daa61bf531170bf/lib/Compilation.js#L2281 + * + * If we don't clear the hash an emit of runtime.js and an empty [somehash].hot-update.json will happen on save without changes. + * This will restart the NS application. + */ + childCompilation.hash = ""; const workerFile = entries[0].files[0]; this._compilation.workerChunks.push(workerFile); const workerFactory = getWorker(workerFile); diff --git a/src/package.json b/src/package.json index a85a68f..98dbab7 100644 --- a/src/package.json +++ b/src/package.json @@ -1,6 +1,6 @@ { "name": "nativescript-worker-loader", - "version": "0.9.5", + "version": "0.10.0", "author": "NativeScript team", "description": "nativescript worker loader module for webpack", "scripts": {