Skip to content

Commit b7f5db9

Browse files
authored
Merge pull request #48 from NativeScript/kddimitrov/fix-worker-loader-cachable
Kddimitrov/fix worker loader cachable
2 parents 7382676 + 112385a commit b7f5db9

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/index.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ module.exports.pitch = function pitch(request) {
4242
throw new Error("Only usable with webpack");
4343
}
4444

45-
this.cacheable(false);
4645
const callback = this.async();
4746
const options = loaderUtils.getOptions(this) || {};
4847
const compilerOptions = this._compiler.options || {};
@@ -89,22 +88,38 @@ module.exports.pitch = function pitch(request) {
8988
}
9089

9190
new SingleEntryPlugin(this.context, `!!${request}`, "main").apply(workerCompiler);
92-
93-
const subCache = `subcache ${__dirname} ${request}`;
9491
const plugin = { name: "WorkerLoader" };
9592

96-
workerCompiler.hooks.compilation.tap(plugin, compilation => {
97-
if (compilation.cache) {
98-
compilation.cache = compilation.cache[subCache] || {};
99-
}
93+
workerCompiler.hooks.thisCompilation.tap(plugin, compilation => {
94+
/**
95+
* A dirty hack to disable HMR plugin in childCompilation:
96+
* https://github.com/webpack/webpack/blob/4056506488c1e071dfc9a0127daa61bf531170bf/lib/HotModuleReplacementPlugin.js#L154
97+
*
98+
* Once we update to webpack@4.40.3 and above this can be removed:
99+
* https://github.com/webpack/webpack/commit/1c4138d6ac04b7b47daa5ec4475c0ae1b4f596a2
100+
*/
101+
compilation.hotUpdateChunkTemplate = null;
100102
});
101103

102-
workerCompiler.runAsChild((err, entries) => {
104+
workerCompiler.runAsChild((err, entries, childCompilation) => {
103105
if (err) {
104106
return callback(err);
105107
}
106108

107109
if (entries[0]) {
110+
const fileDeps = Array.from(childCompilation.fileDependencies);
111+
this.clearDependencies();
112+
fileDeps.map(fileName => {
113+
this.addDependency(fileName);
114+
});
115+
/**
116+
* Clears the hash of the child compilation as it affects the hash of the parent compilation:
117+
* https://github.com/webpack/webpack/blob/4056506488c1e071dfc9a0127daa61bf531170bf/lib/Compilation.js#L2281
118+
*
119+
* 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.
120+
* This will restart the NS application.
121+
*/
122+
childCompilation.hash = "";
108123
const workerFile = entries[0].files[0];
109124
this._compilation.workerChunks.push(workerFile);
110125
const workerFactory = getWorker(workerFile);

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-worker-loader",
3-
"version": "0.9.5",
3+
"version": "0.10.0",
44
"author": "NativeScript team",
55
"description": "nativescript worker loader module for webpack",
66
"scripts": {

0 commit comments

Comments
 (0)