From 317e2c98890ad7a6cca9fc0da172a4de84ae454d Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Thu, 1 Apr 2021 18:33:42 +0300 Subject: [PATCH 1/2] fix: usage multiple plugins and `deleteOriginalAssets` --- src/index.js | 18 +- test/CompressionPlugin.test.js | 81 +++++++++ .../CompressionPlugin.test.js.snap | 170 ++++++++++++++++++ 3 files changed, 265 insertions(+), 4 deletions(-) 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..682942b 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", + 3493, + Object { + "compressed": true, + "size": 3493, + }, + ], + Array [ + "/br/main.js.map", + 3582, + Object { + "compressed": true, + "size": 3582, + }, + ], + 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", + 3978, + Object { + "compressed": true, + "size": 3978, + }, + ], + Array [ + "main.js.map", + 4081, + Object { + "compressed": true, + "size": 4081, + }, + ], +] +`; + +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", + 3458, + Object { + "compressed": true, + "size": 3458, + }, + ], + 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", + 3954, + Object { + "compressed": true, + "size": 3954, + }, + ], +] +`; + +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 [ From fd7fc35c4354805a700610654ebfe4ed53a138c5 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Thu, 1 Apr 2021 18:37:24 +0300 Subject: [PATCH 2/2] chore: update tests --- .../CompressionPlugin.test.js.snap | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/test/__snapshots__/CompressionPlugin.test.js.snap b/test/__snapshots__/CompressionPlugin.test.js.snap index 682942b..665e67a 100644 --- a/test/__snapshots__/CompressionPlugin.test.js.snap +++ b/test/__snapshots__/CompressionPlugin.test.js.snap @@ -1621,18 +1621,18 @@ Array [ ], Array [ "/br/main.js", - 3493, + 3465, Object { "compressed": true, - "size": 3493, + "size": 3465, }, ], Array [ "/br/main.js.map", - 3582, + 3578, Object { "compressed": true, - "size": 3582, + "size": 3578, }, ], Array [ @@ -1663,18 +1663,18 @@ Array [ ], Array [ "main.js", - 3978, + 3969, Object { "compressed": true, - "size": 3978, + "size": 3969, }, ], Array [ "main.js.map", - 4081, + 4062, Object { "compressed": true, - "size": 4081, + "size": 4062, }, ], ] @@ -1714,10 +1714,10 @@ Array [ ], Array [ "/br/main.js", - 3458, + 3442, Object { "compressed": true, - "size": 3458, + "size": 3442, }, ], Array [ @@ -1748,10 +1748,10 @@ Array [ ], Array [ "main.js", - 3954, + 3946, Object { "compressed": true, - "size": 3954, + "size": 3946, }, ], ]