diff --git a/src/index.js b/src/index.js index dd650a0..38a80f6 100644 --- a/src/index.js +++ b/src/index.js @@ -279,10 +279,20 @@ class CompressionPlugin { } if (this.options.deleteOriginalAssets) { - if (this.options.deleteOriginalAssets === "keep-source-map") { - compilation.updateAsset(name, source, { - related: { sourceMap: null }, - }); + const needKeepSourceMap = + this.options.deleteOriginalAssets === "keep-source-map"; + const isSameFilanem = name === newName; + + if (needKeepSourceMap || isSameFilanem) { + const updatedAssetInfo = {}; + + if (isSameFilanem) { + updatedAssetInfo.related = null; + } else { + updatedAssetInfo.related = { sourceMap: null }; + } + + compilation.updateAsset(name, source, updatedAssetInfo); } compilation.deleteAsset(name); diff --git a/test/CompressionPlugin.test.js b/test/CompressionPlugin.test.js index a4851ab..180c2e0 100644 --- a/test/CompressionPlugin.test.js +++ b/test/CompressionPlugin.test.js @@ -412,6 +412,87 @@ describe("CompressionPlugin", () => { }); }); + it("should work with multiple plugins and using same filename", async () => { + const compiler = getCompiler( + "./entry.js", + {}, + { + output: { + path: `${__dirname}/dist`, + filename: "[name].js", + chunkFilename: "[id].[name].js", + }, + } + ); + + new CompressionPlugin({ + algorithm: "brotliCompress", + filename: "[path]/br/[file]", + minRatio: Infinity, + }).apply(compiler); + new CompressionPlugin({ + algorithm: "gzip", + filename: "[file]", + minRatio: Infinity, + deleteOriginalAssets: true, + }).apply(compiler); + + await new Promise(async (resolve) => { + const newStats = await compile(compiler); + + // expect(newStats.compilation.emittedAssets.size).toBe(8); + + expect(getAssetsNameAndSize(newStats, compiler)).toMatchSnapshot( + "assets" + ); + expect(getWarnings(newStats)).toMatchSnapshot("errors"); + expect(getErrors(newStats)).toMatchSnapshot("warnings"); + + resolve(); + }); + }); + + it("should work with multiple plugins and using same filename and keep source maps", async () => { + const compiler = getCompiler( + "./entry.js", + {}, + { + devtool: "source-map", + output: { + path: `${__dirname}/dist`, + filename: "[name].js", + chunkFilename: "[id].[name].js", + }, + } + ); + + new CompressionPlugin({ + algorithm: "brotliCompress", + filename: "[path]/br/[file]", + minRatio: Infinity, + }).apply(compiler); + new CompressionPlugin({ + algorithm: "gzip", + filename: "[file]", + minRatio: Infinity, + deleteOriginalAssets: "keep-source-map", + }).apply(compiler); + + await new Promise(async (resolve) => { + const newStats = await compile(compiler); + + // expect(newStats.compilation.emittedAssets.size).toBe(8); + + expect(getAssetsNameAndSize(newStats, compiler)).toMatchSnapshot( + "assets" + ); + expect(getWarnings(newStats)).toMatchSnapshot("errors"); + expect(getErrors(newStats)).toMatchSnapshot("warnings"); + + resolve(); + }); + }); + it('should work and do not use memory cache when the "cache" option is "false"', async () => { const compiler = getCompiler( "./entry.js", diff --git a/test/__snapshots__/CompressionPlugin.test.js.snap b/test/__snapshots__/CompressionPlugin.test.js.snap index 4fb5559..665e67a 100644 --- a/test/__snapshots__/CompressionPlugin.test.js.snap +++ b/test/__snapshots__/CompressionPlugin.test.js.snap @@ -1591,6 +1591,176 @@ exports[`CompressionPlugin should work with assets info: errors 1`] = `Array []` exports[`CompressionPlugin should work with assets info: warnings 1`] = `Array []`; +exports[`CompressionPlugin should work with multiple plugins and using same filename and keep source maps: assets 1`] = ` +Array [ + Array [ + "/br/09a1a1112c577c2794359715edfcb5ac.png", + 73329, + Object { + "compressed": true, + "immutable": true, + "size": 73329, + }, + ], + Array [ + "/br/23fc1d3ac606d117e05a140e0de79806.svg", + 363, + Object { + "compressed": true, + "immutable": true, + "size": 363, + }, + ], + Array [ + "/br/async.async.js", + 123, + Object { + "compressed": true, + "size": 123, + }, + ], + Array [ + "/br/main.js", + 3465, + Object { + "compressed": true, + "size": 3465, + }, + ], + Array [ + "/br/main.js.map", + 3578, + Object { + "compressed": true, + "size": 3578, + }, + ], + Array [ + "09a1a1112c577c2794359715edfcb5ac.png", + 73160, + Object { + "compressed": true, + "immutable": true, + "size": 73160, + }, + ], + Array [ + "23fc1d3ac606d117e05a140e0de79806.svg", + 393, + Object { + "compressed": true, + "immutable": true, + "size": 393, + }, + ], + Array [ + "async.async.js", + 134, + Object { + "compressed": true, + "size": 134, + }, + ], + Array [ + "main.js", + 3969, + Object { + "compressed": true, + "size": 3969, + }, + ], + Array [ + "main.js.map", + 4062, + Object { + "compressed": true, + "size": 4062, + }, + ], +] +`; + +exports[`CompressionPlugin should work with multiple plugins and using same filename and keep source maps: errors 1`] = `Array []`; + +exports[`CompressionPlugin should work with multiple plugins and using same filename and keep source maps: warnings 1`] = `Array []`; + +exports[`CompressionPlugin should work with multiple plugins and using same filename: assets 1`] = ` +Array [ + Array [ + "/br/09a1a1112c577c2794359715edfcb5ac.png", + 73329, + Object { + "compressed": true, + "immutable": true, + "size": 73329, + }, + ], + Array [ + "/br/23fc1d3ac606d117e05a140e0de79806.svg", + 363, + Object { + "compressed": true, + "immutable": true, + "size": 363, + }, + ], + Array [ + "/br/async.async.js", + 123, + Object { + "compressed": true, + "size": 123, + }, + ], + Array [ + "/br/main.js", + 3442, + Object { + "compressed": true, + "size": 3442, + }, + ], + Array [ + "09a1a1112c577c2794359715edfcb5ac.png", + 73160, + Object { + "compressed": true, + "immutable": true, + "size": 73160, + }, + ], + Array [ + "23fc1d3ac606d117e05a140e0de79806.svg", + 393, + Object { + "compressed": true, + "immutable": true, + "size": 393, + }, + ], + Array [ + "async.async.js", + 134, + Object { + "compressed": true, + "size": 134, + }, + ], + Array [ + "main.js", + 3946, + Object { + "compressed": true, + "size": 3946, + }, + ], +] +`; + +exports[`CompressionPlugin should work with multiple plugins and using same filename: errors 1`] = `Array []`; + +exports[`CompressionPlugin should work with multiple plugins and using same filename: warnings 1`] = `Array []`; + exports[`CompressionPlugin should work with multiple plugins: assets 1`] = ` Array [ Array [