From 86f3da458c3e22b9e9a2f25813917ac7a74dae67 Mon Sep 17 00:00:00 2001 From: Scott CAMERON Date: Fri, 7 Apr 2023 07:15:46 +0200 Subject: [PATCH 1/7] Fix non-deterministic moduleId generation The module sort was encountering NaN values, causing the sort to be non-deterministic, resulting in a different moduleId assignment being used in each build run when moduleIds: "size" is configured. This causes the build to be slightly different in size each time it's run. --- lib/ids/OccurrenceModuleIdsPlugin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ids/OccurrenceModuleIdsPlugin.js b/lib/ids/OccurrenceModuleIdsPlugin.js index a135b0976fe..ce0951c7ed8 100644 --- a/lib/ids/OccurrenceModuleIdsPlugin.js +++ b/lib/ids/OccurrenceModuleIdsPlugin.js @@ -81,7 +81,7 @@ class OccurrenceModuleIdsPlugin { ] of moduleGraph.getIncomingConnectionsByOriginModule(module)) { if (!originModule) continue; if (!connections.some(c => c.isTargetActive(undefined))) continue; - sum += initialChunkChunkMap.get(originModule); + sum += (initialChunkChunkMap.get(originModule) || 0); } return sum; }; From bf7c6d16cf1d775c82902b3e1bad9c50a235563d Mon Sep 17 00:00:00 2001 From: Scott CAMERON Date: Fri, 7 Apr 2023 08:59:19 +0200 Subject: [PATCH 2/7] Fix lint error (that I disagree with :-) ) --- lib/ids/OccurrenceModuleIdsPlugin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ids/OccurrenceModuleIdsPlugin.js b/lib/ids/OccurrenceModuleIdsPlugin.js index ce0951c7ed8..71fb2ce047a 100644 --- a/lib/ids/OccurrenceModuleIdsPlugin.js +++ b/lib/ids/OccurrenceModuleIdsPlugin.js @@ -81,7 +81,7 @@ class OccurrenceModuleIdsPlugin { ] of moduleGraph.getIncomingConnectionsByOriginModule(module)) { if (!originModule) continue; if (!connections.some(c => c.isTargetActive(undefined))) continue; - sum += (initialChunkChunkMap.get(originModule) || 0); + sum += initialChunkChunkMap.get(originModule) || 0; } return sum; }; From 9ea8630cde65bf87fe1d392833e840b33486511b Mon Sep 17 00:00:00 2001 From: Scott CAMERON Date: Mon, 10 Apr 2023 21:33:21 +0200 Subject: [PATCH 3/7] Add temporary logging to see if repro case already exists in tests --- lib/ids/OccurrenceModuleIdsPlugin.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/ids/OccurrenceModuleIdsPlugin.js b/lib/ids/OccurrenceModuleIdsPlugin.js index 71fb2ce047a..61c0a002e81 100644 --- a/lib/ids/OccurrenceModuleIdsPlugin.js +++ b/lib/ids/OccurrenceModuleIdsPlugin.js @@ -136,6 +136,9 @@ class OccurrenceModuleIdsPlugin { if (prioritiseInitial) { const aEntryOccurs = occursInInitialChunksMap.get(a); const bEntryOccurs = occursInInitialChunksMap.get(b); + if (Number.isNaN(aEntryOccurs) || Number.isNaN(bEntryOccurs)) { + console.log("SCOTT: found occurrence"); + } if (aEntryOccurs > bEntryOccurs) return -1; if (aEntryOccurs < bEntryOccurs) return 1; } From c8b3ed701e8908b58f4cbd3ce557c777cc59a1d3 Mon Sep 17 00:00:00 2001 From: Scott CAMERON Date: Tue, 11 Apr 2023 07:32:33 +0200 Subject: [PATCH 4/7] Remove temporary logging --- lib/ids/OccurrenceModuleIdsPlugin.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/ids/OccurrenceModuleIdsPlugin.js b/lib/ids/OccurrenceModuleIdsPlugin.js index 61c0a002e81..71fb2ce047a 100644 --- a/lib/ids/OccurrenceModuleIdsPlugin.js +++ b/lib/ids/OccurrenceModuleIdsPlugin.js @@ -136,9 +136,6 @@ class OccurrenceModuleIdsPlugin { if (prioritiseInitial) { const aEntryOccurs = occursInInitialChunksMap.get(a); const bEntryOccurs = occursInInitialChunksMap.get(b); - if (Number.isNaN(aEntryOccurs) || Number.isNaN(bEntryOccurs)) { - console.log("SCOTT: found occurrence"); - } if (aEntryOccurs > bEntryOccurs) return -1; if (aEntryOccurs < bEntryOccurs) return 1; } From 5cc6d13a479f5d93fdd0d346adf94bf11c5ac53b Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Wed, 19 Apr 2023 02:30:04 +0300 Subject: [PATCH 5/7] test: added case --- .../contenthash/module-ids-size/1.jpg | 0 .../contenthash/module-ids-size/async.js | 10 +++++ .../contenthash/module-ids-size/file-1.js | 19 ++++++++++ .../contenthash/module-ids-size/file-2.js | 5 +++ .../contenthash/module-ids-size/file-3.js | 7 ++++ .../contenthash/module-ids-size/file.js | 25 +++++++++++++ .../contenthash/module-ids-size/index.js | 7 ++++ .../module-ids-size/test.config.js | 29 +++++++++++++++ .../module-ids-size/webpack.config.js | 37 +++++++++++++++++++ 9 files changed, 139 insertions(+) create mode 100644 test/configCases/contenthash/module-ids-size/1.jpg create mode 100644 test/configCases/contenthash/module-ids-size/async.js create mode 100644 test/configCases/contenthash/module-ids-size/file-1.js create mode 100644 test/configCases/contenthash/module-ids-size/file-2.js create mode 100644 test/configCases/contenthash/module-ids-size/file-3.js create mode 100644 test/configCases/contenthash/module-ids-size/file.js create mode 100644 test/configCases/contenthash/module-ids-size/index.js create mode 100644 test/configCases/contenthash/module-ids-size/test.config.js create mode 100644 test/configCases/contenthash/module-ids-size/webpack.config.js diff --git a/test/configCases/contenthash/module-ids-size/1.jpg b/test/configCases/contenthash/module-ids-size/1.jpg new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/configCases/contenthash/module-ids-size/async.js b/test/configCases/contenthash/module-ids-size/async.js new file mode 100644 index 00000000000..ec6ae927a27 --- /dev/null +++ b/test/configCases/contenthash/module-ids-size/async.js @@ -0,0 +1,10 @@ +export default function test() { + const a = 1; + const b = 2; + const c = 3; + const d = 4; + const f = 5; + const e = 6; + + return a + b + c + d + f + e; +} diff --git a/test/configCases/contenthash/module-ids-size/file-1.js b/test/configCases/contenthash/module-ids-size/file-1.js new file mode 100644 index 00000000000..a3825a0f846 --- /dev/null +++ b/test/configCases/contenthash/module-ids-size/file-1.js @@ -0,0 +1,19 @@ +async function test() { + const a = 1; + const b = 2; + const c = 3; + const d = 4; + const f = 5; + const e = 6; + + await import("./async.js"); + + return a + b + c + d + f + e; +} + +test(); + +export { test } +export default test; + +test(); diff --git a/test/configCases/contenthash/module-ids-size/file-2.js b/test/configCases/contenthash/module-ids-size/file-2.js new file mode 100644 index 00000000000..5217d109731 --- /dev/null +++ b/test/configCases/contenthash/module-ids-size/file-2.js @@ -0,0 +1,5 @@ +import { test } from "./file-1.js"; + +export default function foobar() { + return "test" + test(); +} diff --git a/test/configCases/contenthash/module-ids-size/file-3.js b/test/configCases/contenthash/module-ids-size/file-3.js new file mode 100644 index 00000000000..5e033ab9f4a --- /dev/null +++ b/test/configCases/contenthash/module-ids-size/file-3.js @@ -0,0 +1,7 @@ +function test() { + return "test"; +} + +test(); + +module.exports = "test"; diff --git a/test/configCases/contenthash/module-ids-size/file.js b/test/configCases/contenthash/module-ids-size/file.js new file mode 100644 index 00000000000..2f8412c218e --- /dev/null +++ b/test/configCases/contenthash/module-ids-size/file.js @@ -0,0 +1,25 @@ +import file from "./file-1.js"; +import file2 from "./file-2.js"; + +async function test() { + const a = 1; + const b = 2; + const c = 3; + const d = 4; + const f = 5; + const e = 6; + + await import(/* webpackMode: "eager" */"./async.js"); + await import(/* webpackMode: "eager" */"./file-3.js"); + + return a + b + c + d + f + e; +} + +test(); + +export { test, file, file2 } +export default function foo() { + return "test"; +} + +test(); diff --git a/test/configCases/contenthash/module-ids-size/index.js b/test/configCases/contenthash/module-ids-size/index.js new file mode 100644 index 00000000000..c43e8c29af8 --- /dev/null +++ b/test/configCases/contenthash/module-ids-size/index.js @@ -0,0 +1,7 @@ +import img from "./1.jpg"; +import file from "./file.js"; + +it("should compile", () => { + expect(typeof img).toBe("string"); + expect(typeof file).toBe("function"); +}); diff --git a/test/configCases/contenthash/module-ids-size/test.config.js b/test/configCases/contenthash/module-ids-size/test.config.js new file mode 100644 index 00000000000..481525cf3cf --- /dev/null +++ b/test/configCases/contenthash/module-ids-size/test.config.js @@ -0,0 +1,29 @@ +const findOutputFiles = require("../../../helpers/findOutputFiles"); + +const allAssets = new Set(); +const allBundles = new Set(); + +module.exports = { + findBundle: function (i, options) { + const bundle = findOutputFiles(options, new RegExp(`^bundle${i}`))[0]; + + allBundles.add(/\.([^.]+)\./.exec(bundle)[1]); + + let asset; + + switch (i) { + case 0: + asset = findOutputFiles(options, /^1\.[^\.]*\.jpg$/, "img")[0]; + break; + } + + if (asset) allAssets.add(asset); + + + return `./${bundle}`; + }, + afterExecute: () => { + // Bundles have the same contenthash + expect(allBundles.size).toBe(1); + } +}; diff --git a/test/configCases/contenthash/module-ids-size/webpack.config.js b/test/configCases/contenthash/module-ids-size/webpack.config.js new file mode 100644 index 00000000000..2b768573875 --- /dev/null +++ b/test/configCases/contenthash/module-ids-size/webpack.config.js @@ -0,0 +1,37 @@ +/** @type {import("../../../../").Configuration} */ +module.exports = [ + { + output: { + filename: "bundle0.[contenthash].a.js", + assetModuleFilename: "img/[name].a.[contenthash][ext]" + }, + optimization: { + moduleIds: "size" + }, + module: { + rules: [ + { + test: /\.jpg$/, + type: "asset/resource" + } + ] + } + }, + { + output: { + filename: "bundle1.[contenthash].b.js", + assetModuleFilename: "img/[name].a.[contenthash][ext]" + }, + optimization: { + moduleIds: "size" + }, + module: { + rules: [ + { + test: /\.jpg$/, + type: "asset/resource" + } + ] + } + } +]; From 90df710283859f666c4997eadd478239af5540a0 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Wed, 19 Apr 2023 02:50:43 +0300 Subject: [PATCH 6/7] chore: fix lint --- test/configCases/contenthash/module-ids-size/test.config.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/configCases/contenthash/module-ids-size/test.config.js b/test/configCases/contenthash/module-ids-size/test.config.js index 481525cf3cf..6e2a349c715 100644 --- a/test/configCases/contenthash/module-ids-size/test.config.js +++ b/test/configCases/contenthash/module-ids-size/test.config.js @@ -19,7 +19,6 @@ module.exports = { if (asset) allAssets.add(asset); - return `./${bundle}`; }, afterExecute: () => { From 56d4fb27e3706ec9d293b6a0bfd87619bc7f3f9e Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Wed, 19 Apr 2023 03:03:23 +0300 Subject: [PATCH 7/7] chore: fix lint again --- test/configCases/contenthash/module-ids-size/test.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/configCases/contenthash/module-ids-size/test.config.js b/test/configCases/contenthash/module-ids-size/test.config.js index 6e2a349c715..2ade34513db 100644 --- a/test/configCases/contenthash/module-ids-size/test.config.js +++ b/test/configCases/contenthash/module-ids-size/test.config.js @@ -13,7 +13,7 @@ module.exports = { switch (i) { case 0: - asset = findOutputFiles(options, /^1\.[^\.]*\.jpg$/, "img")[0]; + asset = findOutputFiles(options, /^1\.[^.]*\.jpg$/, "img")[0]; break; }