diff --git a/lib/serialization/ErrorObjectSerializer.js b/lib/serialization/ErrorObjectSerializer.js index 14f3b9c1a..40f47fc27 100644 --- a/lib/serialization/ErrorObjectSerializer.js +++ b/lib/serialization/ErrorObjectSerializer.js @@ -21,6 +21,7 @@ class ErrorObjectSerializer { serialize(obj, context) { context.write(obj.message); context.write(obj.stack); + context.write(/** @type {Error & { cause: "unknown" }} */ (obj).cause); } /** * @param {ObjectDeserializerContext} context context @@ -31,6 +32,8 @@ class ErrorObjectSerializer { err.message = context.read(); err.stack = context.read(); + /** @type {Error & { cause: "unknown" }} */ + (err).cause = context.read(); return err; } diff --git a/test/Compiler-filesystem-caching.test.js b/test/Compiler-filesystem-caching.test.js index cad5f6792..16f2a1456 100644 --- a/test/Compiler-filesystem-caching.test.js +++ b/test/Compiler-filesystem-caching.test.js @@ -41,6 +41,51 @@ describe("Compiler (filesystem caching)", () => { ] }; + options.plugins = [ + { + apply(compiler) { + const name = "TestCachePlugin"; + + compiler.hooks.thisCompilation.tap(name, compilation => { + compilation.hooks.processAssets.tapPromise( + { + name, + stage: + compiler.webpack.Compilation + .PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE + }, + async () => { + const cache = compilation.getCache(name); + const ident = "test.ext"; + const cacheItem = cache.getItemCache(ident, null); + + const result = await cacheItem.getPromise(ident); + + if (result) { + expect(result.number).toEqual(42); + expect(result.string).toEqual("string"); + expect(result.error.cause.message).toEqual("cause"); + expect(result.error1.cause.string).toBe("string"); + expect(result.error1.cause.number).toBe(42); + + return; + } + + const number = 42; + const string = "string"; + const error = new Error("error", { cause: new Error("cause") }); + const error1 = new Error("error", { + cause: { string, number } + }); + + await cacheItem.storePromise({ number, string, error, error1 }); + } + ); + }); + } + } + ]; + function runCompiler(onSuccess, onError) { const c = webpack(options); c.hooks.compilation.tap(