From d4b39314edfe8e1870b95a4ba8904c6a475c93b2 Mon Sep 17 00:00:00 2001 From: Kristian Dimitrov Date: Fri, 15 Nov 2019 13:46:22 +0200 Subject: [PATCH 1/4] fix: worker loader not cacheable --- src/index.js | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/index.js b/src/index.js index 23bc6ad..e749dd8 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 || {}; @@ -90,21 +89,17 @@ 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.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); + }); const workerFile = entries[0].files[0]; this._compilation.workerChunks.push(workerFile); const workerFactory = getWorker(workerFile); From 6f8ad0ec7a1b4eb7bcf6d10244b9bf233da4e12f Mon Sep 17 00:00:00 2001 From: Kristian Dimitrov Date: Fri, 15 Nov 2019 18:13:32 +0200 Subject: [PATCH 2/4] fix: disable HMR in child compilation and clear compilation hash --- src/index.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/index.js b/src/index.js index e749dd8..37dd4a5 100644 --- a/src/index.js +++ b/src/index.js @@ -88,6 +88,15 @@ module.exports.pitch = function pitch(request) { } new SingleEntryPlugin(this.context, `!!${request}`, "main").apply(workerCompiler); + const plugin = { name: "WorkerLoader" }; + + 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, childCompilation) => { if (err) { @@ -100,6 +109,11 @@ module.exports.pitch = function pitch(request) { 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, which will restart the NS application. + */ + childCompilation.hash = ""; const workerFile = entries[0].files[0]; this._compilation.workerChunks.push(workerFile); const workerFactory = getWorker(workerFile); From d462cb93a1fbe1a261c10c272ed030b1d3fff3b3 Mon Sep 17 00:00:00 2001 From: Kristian Dimitrov Date: Fri, 15 Nov 2019 18:22:04 +0200 Subject: [PATCH 3/4] chore: format code comments --- src/index.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 37dd4a5..afae972 100644 --- a/src/index.js +++ b/src/index.js @@ -92,8 +92,11 @@ module.exports.pitch = function pitch(request) { 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 + * 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; }); @@ -110,8 +113,11 @@ module.exports.pitch = function pitch(request) { 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, which will restart the NS application. + * 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]; From 112385a1124b1a07b0272baf56041057b0db674a Mon Sep 17 00:00:00 2001 From: Kristian Dimitrov Date: Mon, 18 Nov 2019 16:05:18 +0200 Subject: [PATCH 4/4] chore: bump version to 0.10.0 --- src/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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": {