Skip to content

Kddimitrov/fix worker loader cachable #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {};
Expand Down Expand Up @@ -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) {
Copy link
Contributor Author

@KristianDD KristianDD Nov 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed based on this issue - jantimon/html-webpack-plugin#1089

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);
Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down