Skip to content

Commit e43bb4b

Browse files
authored
Merge pull request #12372 from webpack/bugfix/split-chunks-min-size-4
fix bug where module size is added multiple times to the split chunk info
2 parents c572c15 + 4de8451 commit e43bb4b

File tree

8 files changed

+44
-4
lines changed

8 files changed

+44
-4
lines changed

lib/optimize/SplitChunksPlugin.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const compareEntries = (a, b) => {
7676
const diffSizeReduce = aSizeReduce - bSizeReduce;
7777
if (diffSizeReduce) return diffSizeReduce;
7878
// 4. by cache group index
79-
const indexDiff = a.cacheGroupIndex - b.cacheGroupIndex;
79+
const indexDiff = b.cacheGroupIndex - a.cacheGroupIndex;
8080
if (indexDiff) return indexDiff;
8181
// 5. by number of modules (to be able to compare by identifier)
8282
const modulesA = a.modules;
@@ -523,10 +523,14 @@ module.exports = class SplitChunksPlugin {
523523
})
524524
);
525525
}
526+
const oldSize = info.modules.size;
526527
info.modules.add(module);
527-
info.size += module.size();
528-
if (!info.chunksKeys.has(selectedChunksKey)) {
529-
info.chunksKeys.add(selectedChunksKey);
528+
if (info.modules.size !== oldSize) {
529+
info.size += module.size();
530+
}
531+
const oldChunksKeysSize = info.chunksKeys.size;
532+
info.chunksKeys.add(selectedChunksKey);
533+
if (oldChunksKeysSize !== info.chunksKeys.size) {
530534
for (const chunk of selectedChunks) {
531535
info.chunks.add(chunk);
532536
}

test/ConfigTestCases.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ describe("ConfigTestCases", () => {
207207
expect,
208208
jest,
209209
_globalAssign: { expect },
210+
__STATS__: jsonStats,
210211
nsObj: m => {
211212
Object.defineProperty(m, Symbol.toStringTag, {
212213
value: "Module"

test/configCases/split-chunks/issue-12307/a-only-module.js

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import "./a-only-module";
2+
import "./shared-module";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import(/* webpackChunkName: "shared-module" */ "./shared-module");
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
it("should not split the shared-modules into a separate chunk", () => {
2+
const shared = __STATS__.modules.find(m => m.name.includes("shared-module"));
3+
expect(shared.chunks).toEqual(["a", "shared-module"]);
4+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// content content content content content content content content content
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/** @type {import("../../../../").Configuration} */
2+
module.exports = {
3+
entry: {
4+
bundle0: "./index",
5+
a: "./a",
6+
b: "./b"
7+
},
8+
output: {
9+
filename: "[name].js"
10+
},
11+
optimization: {
12+
chunkIds: "named",
13+
sideEffects: false,
14+
splitChunks: {
15+
cacheGroups: {
16+
default: false,
17+
defaultVendors: false,
18+
test: {
19+
test: /shared/,
20+
minChunks: 1,
21+
chunks: "initial",
22+
minSize: 100
23+
}
24+
}
25+
}
26+
}
27+
};

0 commit comments

Comments
 (0)