diff --git a/lib/ids/OccurrenceModuleIdsPlugin.js b/lib/ids/OccurrenceModuleIdsPlugin.js index a135b0976fe..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); + sum += initialChunkChunkMap.get(originModule) || 0; } return sum; }; 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..2ade34513db --- /dev/null +++ b/test/configCases/contenthash/module-ids-size/test.config.js @@ -0,0 +1,28 @@ +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" + } + ] + } + } +];