Skip to content

Commit

Permalink
fix bug where module size is added multiple times to the split chunk …
Browse files Browse the repository at this point in the history
…info

fixes webpack#12307
  • Loading branch information
sokra committed Jan 7, 2021
1 parent c572c15 commit 3f69f3c
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/optimize/SplitChunksPlugin.js
Expand Up @@ -523,10 +523,14 @@ module.exports = class SplitChunksPlugin {
})
);
}
const oldSize = info.modules.size;
info.modules.add(module);
info.size += module.size();
if (!info.chunksKeys.has(selectedChunksKey)) {
info.chunksKeys.add(selectedChunksKey);
if (info.modules.size !== oldSize) {
info.size += module.size();
}
const oldChunksKeysSize = info.chunksKeys.size;
info.chunksKeys.add(selectedChunksKey);
if (oldChunksKeysSize !== info.chunksKeys.size) {
for (const chunk of selectedChunks) {
info.chunks.add(chunk);
}
Expand Down
1 change: 1 addition & 0 deletions test/ConfigTestCases.test.js
Expand Up @@ -207,6 +207,7 @@ describe("ConfigTestCases", () => {
expect,
jest,
_globalAssign: { expect },
__STATS__: jsonStats,
nsObj: m => {
Object.defineProperty(m, Symbol.toStringTag, {
value: "Module"
Expand Down
Empty file.
2 changes: 2 additions & 0 deletions test/configCases/split-chunks/issue-12307/a.js
@@ -0,0 +1,2 @@
import "./a-only-module";
import "./shared-module";
1 change: 1 addition & 0 deletions test/configCases/split-chunks/issue-12307/b.js
@@ -0,0 +1 @@
import(/* webpackChunkName: "shared-module" */ "./shared-module");
4 changes: 4 additions & 0 deletions test/configCases/split-chunks/issue-12307/index.js
@@ -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"]);
});
1 change: 1 addition & 0 deletions test/configCases/split-chunks/issue-12307/shared-module.js
@@ -0,0 +1 @@
// content content content content content content content content content
27 changes: 27 additions & 0 deletions test/configCases/split-chunks/issue-12307/webpack.config.js
@@ -0,0 +1,27 @@
/** @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,
chunks: "initial",
minSize: 100
}
}
}
}
};

0 comments on commit 3f69f3c

Please sign in to comment.