fix: serialize `HookWebpackError` (#20169)

This commit is contained in:
Alexander Akait 2025-11-26 17:49:00 +03:00 committed by GitHub
parent 1b5084e3c4
commit 9abcf3fa8f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 76 additions and 4 deletions

View File

@ -6,6 +6,10 @@
"use strict";
const WebpackError = require("./WebpackError");
const makeSerializable = require("./util/makeSerializable");
/** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
/** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
/**
* @template T
@ -22,18 +26,43 @@ class HookWebpackError extends WebpackError {
* @param {string} hook name of hook
*/
constructor(error, hook) {
super(error.message);
super(error ? error.message : undefined, error ? { cause: error } : {});
this.name = "HookWebpackError";
this.hook = hook;
this.error = error;
this.name = "HookWebpackError";
this.hideStack = true;
this.details = `caused by plugins in ${hook}\n${error.stack}`;
this.stack += `\n-- inner error --\n${error ? error.stack : ""}`;
this.details = `caused by plugins in ${hook}\n${error ? error.stack : ""}`;
}
this.stack += `\n-- inner error --\n${error.stack}`;
/**
* @param {ObjectSerializerContext} context context
*/
serialize(context) {
const { write } = context;
write(this.error);
write(this.hook);
super.serialize(context);
}
/**
* @param {ObjectDeserializerContext} context context
*/
deserialize(context) {
const { read } = context;
this.error = read();
this.hook = read();
super.deserialize(context);
}
}
makeSerializable(HookWebpackError, "webpack/lib/HookWebpackError");
module.exports = HookWebpackError;
/**

View File

@ -0,0 +1,3 @@
"use strict";
module.exports = [[/Module build failed/]];

View File

@ -0,0 +1,5 @@
import value from "./my-errored-module.js"
it("should work and cache", function(done) {
done();
});

View File

@ -0,0 +1,9 @@
"use strict";
module.exports = (options) => {
if (options.cache && options.cache.type === "filesystem") {
return [/Pack got invalid because of write to/];
}
return [];
};

View File

@ -0,0 +1,9 @@
const HookWebpackError = require('../../../../lib/HookWebpackError.js');
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function loader() {
const callback = this.async();
callback(new HookWebpackError(new Error("Error: test"), "hookName"));
};

View File

@ -0,0 +1,17 @@
"use strict";
/** @type {import("../../../../").Configuration} */
module.exports = {
module: {
rules: [
{
test: /my-errored-module\.js$/i,
use: [
{
loader: require.resolve("./loader.js")
}
]
}
]
}
};