diff --git a/lib/HookWebpackError.js b/lib/HookWebpackError.js index 09d45a67f..2dafbabfc 100644 --- a/lib/HookWebpackError.js +++ b/lib/HookWebpackError.js @@ -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; /** diff --git a/test/configCases/errors/hook-webpack-error/errors.js b/test/configCases/errors/hook-webpack-error/errors.js new file mode 100644 index 000000000..d78130172 --- /dev/null +++ b/test/configCases/errors/hook-webpack-error/errors.js @@ -0,0 +1,3 @@ +"use strict"; + +module.exports = [[/Module build failed/]]; diff --git a/test/configCases/errors/hook-webpack-error/index.js b/test/configCases/errors/hook-webpack-error/index.js new file mode 100644 index 000000000..50784061e --- /dev/null +++ b/test/configCases/errors/hook-webpack-error/index.js @@ -0,0 +1,5 @@ +import value from "./my-errored-module.js" + +it("should work and cache", function(done) { + done(); +}); diff --git a/test/configCases/errors/hook-webpack-error/infrastructure-log.js b/test/configCases/errors/hook-webpack-error/infrastructure-log.js new file mode 100644 index 000000000..951fafae2 --- /dev/null +++ b/test/configCases/errors/hook-webpack-error/infrastructure-log.js @@ -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 []; +}; diff --git a/test/configCases/errors/hook-webpack-error/loader.js b/test/configCases/errors/hook-webpack-error/loader.js new file mode 100644 index 000000000..6226dc30c --- /dev/null +++ b/test/configCases/errors/hook-webpack-error/loader.js @@ -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")); +}; + diff --git a/test/configCases/errors/hook-webpack-error/my-errored-module.js b/test/configCases/errors/hook-webpack-error/my-errored-module.js new file mode 100644 index 000000000..e69de29bb diff --git a/test/configCases/errors/hook-webpack-error/webpack.config.js b/test/configCases/errors/hook-webpack-error/webpack.config.js new file mode 100644 index 000000000..df59733be --- /dev/null +++ b/test/configCases/errors/hook-webpack-error/webpack.config.js @@ -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") + } + ] + } + ] + } +};