Skip to content

Commit 7834e6c

Browse files
committed
CommonsChunkPlugin: Don't allow blocks to contain duplicate chunks
- Add configCase to repro failure
1 parent d0c5417 commit 7834e6c

6 files changed

Lines changed: 53 additions & 1 deletion

File tree

lib/optimize/CommonsChunkPlugin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,9 @@ Take a look at the "name"/"names" or async/children option.`);
340340
moveExtractedChunkBlocksToTargetChunk(chunks, targetChunk) {
341341
for(let chunk of chunks) {
342342
for(let block of chunk.blocks) {
343-
block.chunks.unshift(targetChunk);
343+
if(block.chunks.indexOf(targetChunk) === -1) {
344+
block.chunks.unshift(targetChunk);
345+
}
344346
targetChunk.addBlock(block);
345347
}
346348
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "a";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "b";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "c";
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require("should");
2+
const sinon = require("sinon");
3+
const chunkLoadingSpy = sinon.spy(__webpack_require__, 'e');
4+
5+
it("should not have duplicate chunks in blocks", function(done) {
6+
// This split point should contain: a
7+
require.ensure([], function(require) {
8+
require("./a").should.be.eql("a");
9+
}, 'a');
10+
11+
// This split point should contain: a and b - we use CommonsChunksPlugin to
12+
// have it only contain b and make chunk a be an async dependency.
13+
require.ensure([], function(require) {
14+
require("./a").should.be.eql("a");
15+
require("./b").should.be.eql("b");
16+
}, 'a+b');
17+
18+
// This split point should contain: a, b and c - we use CommonsChunksPlugin to
19+
// have it only contain c and make chunks a and a+b be async dependencies.
20+
require.ensure([], function(require) {
21+
require("./a").should.be.eql("a");
22+
require("./b").should.be.eql("b");
23+
require("./c").should.be.eql("c");
24+
}, 'a+b+c');
25+
26+
// Each of the require.ensures above should end up resolving chunks:
27+
// - a
28+
// - a, a+b
29+
// - a, a+b, a+b+c
30+
chunkLoadingSpy.callCount.should.be.eql(6);
31+
chunkLoadingSpy.args.should.be.eql([[0], [0], [1], [0], [1], [2]]);
32+
done();
33+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var webpack = require("../../../../");
2+
3+
module.exports = {
4+
plugins: [
5+
new webpack.optimize.CommonsChunkPlugin({
6+
chunks: ["a+b", "a+b+c"],
7+
async: "a+b",
8+
}),
9+
new webpack.optimize.CommonsChunkPlugin({
10+
chunks: ["a", "a+b"],
11+
async: "a",
12+
}),
13+
]
14+
};

0 commit comments

Comments
 (0)