diff --git a/lib/Compilation.js b/lib/Compilation.js index 838d26a79f4..1dd47fe4d48 100644 --- a/lib/Compilation.js +++ b/lib/Compilation.js @@ -2995,6 +2995,12 @@ This prevents using hashes of each other and should be avoided.` } } } + // TODO If this becomes a performance problem + // store a reverse mapping from asset to chunk + for (const chunk of this.chunks) { + chunk.files.delete(file); + chunk.auxiliaryFiles.delete(file); + } } getAssets() { diff --git a/test/configCases/assets/delete-asset/chunk2.js b/test/configCases/assets/delete-asset/chunk2.js new file mode 100644 index 00000000000..d9b6390e877 --- /dev/null +++ b/test/configCases/assets/delete-asset/chunk2.js @@ -0,0 +1,3 @@ +/**! Chunk */ + +console.log("Fail"); diff --git a/test/configCases/assets/delete-asset/index.js b/test/configCases/assets/delete-asset/index.js index a80919089ca..07c8964ef51 100644 --- a/test/configCases/assets/delete-asset/index.js +++ b/test/configCases/assets/delete-asset/index.js @@ -6,4 +6,9 @@ it("should fail loading a deleted asset", async () => { code: "ENOENT" }) ); + await expect(import("./chunk2.js")).rejects.toEqual( + expect.objectContaining({ + code: "ENOENT" + }) + ); }); diff --git a/test/configCases/assets/delete-asset/webpack.config.js b/test/configCases/assets/delete-asset/webpack.config.js index a44e79a0bbb..8f2a1c7f2a9 100644 --- a/test/configCases/assets/delete-asset/webpack.config.js +++ b/test/configCases/assets/delete-asset/webpack.config.js @@ -1,4 +1,4 @@ -const { Compilation } = require("../../../../"); +const { Compilation, BannerPlugin } = require("../../../../"); const TerserPlugin = require("terser-webpack-plugin"); /** @type {import("../../../../").Configuration} */ @@ -16,8 +16,20 @@ module.exports = { }, devtool: "source-map", plugins: [ + new BannerPlugin({ + banner: "Test" + }), compiler => { compiler.hooks.compilation.tap("Test", compilation => { + compilation.hooks.processAssets.tap( + { + name: "Test", + stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL + }, + () => { + compilation.deleteAsset("chunk2_js.bundle0.js"); + } + ); compilation.hooks.processAssets.tap( { name: "Test", @@ -51,6 +63,12 @@ module.exports = { expect(compilation.getAsset("chunk_js.bundle0.js.map")).toBe( undefined ); + expect(compilation.getAsset("chunk2_js.bundle0.js")).toBe( + undefined + ); + expect(compilation.getAsset("chunk2_js.bundle0.js.map")).toBe( + undefined + ); expect(compilation.getAsset("LICENSES.txt")).not.toBe(undefined); } );