Merge pull request #12370 from webpack/bugfix/split-chunk-min-size

fix bug where module size is added multiple times to the split chunk
This commit is contained in:
Tobias Koppers 2021-01-07 23:20:12 +01:00 committed by GitHub
commit 06b39fa973
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 46 additions and 5 deletions

View File

@ -232,7 +232,7 @@ const compareEntries = (a, b) => {
const diffSizeReduce = aSizeReduce - bSizeReduce;
if (diffSizeReduce) return diffSizeReduce;
// 4. by cache group index
const indexDiff = a.cacheGroupIndex - b.cacheGroupIndex;
const indexDiff = b.cacheGroupIndex - a.cacheGroupIndex;
if (indexDiff) return indexDiff;
// 5. by number of modules (to be able to compare by identifier)
const modulesA = a.modules;
@ -1113,12 +1113,16 @@ module.exports = class SplitChunksPlugin {
})
);
}
const oldSize = info.modules.size;
info.modules.add(module);
for (const type of module.getSourceTypes()) {
info.sizes[type] = (info.sizes[type] || 0) + module.size(type);
if (info.modules.size !== oldSize) {
for (const type of module.getSourceTypes()) {
info.sizes[type] = (info.sizes[type] || 0) + module.size(type);
}
}
if (!info.chunksKeys.has(selectedChunksKey)) {
info.chunksKeys.add(selectedChunksKey);
const oldChunksKeysSize = info.chunksKeys.size;
info.chunksKeys.add(selectedChunksKey);
if (oldChunksKeysSize !== info.chunksKeys.size) {
for (const chunk of selectedChunks) {
info.chunks.add(chunk);
}

View File

@ -0,0 +1,2 @@
import "./a-only-module";
import "./shared-module";

View File

@ -0,0 +1 @@
import("./shared-module");

View File

@ -0,0 +1,4 @@
it("should not split the shared-modules into a separate chunk", () => {
const shared = __STATS__.modules.find(m => m.name.includes("shared-module"));
expect(shared.chunks).toEqual(["a", "shared-module_js"]);
});

View File

@ -0,0 +1 @@
// content content content content content content content content content

View File

@ -0,0 +1,29 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
entry: {
bundle0: "./index",
a: "./a",
b: "./b"
},
output: {
filename: "[name].js"
},
optimization: {
chunkIds: "named",
sideEffects: false,
splitChunks: {
cacheGroups: {
default: false,
defaultVendors: false,
test: {
test: /shared/,
minChunks: 1,
usedExports: false,
chunks: "initial",
minSize: 100,
minRemainingSize: 0
}
}
}
}
};