Skip to content

Commit

Permalink
CommonsChunkPlugin: Don't allow blocks to contain duplicate chunks
Browse files Browse the repository at this point in the history
- Add configCase to repro failure
  • Loading branch information
ymichael committed May 31, 2017
1 parent d0c5417 commit 7834e6c
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/optimize/CommonsChunkPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,9 @@ Take a look at the "name"/"names" or async/children option.`);
moveExtractedChunkBlocksToTargetChunk(chunks, targetChunk) {
for(let chunk of chunks) {
for(let block of chunk.blocks) {
block.chunks.unshift(targetChunk);
if(block.chunks.indexOf(targetChunk) === -1) {
block.chunks.unshift(targetChunk);
}
targetChunk.addBlock(block);
}
}
Expand Down
1 change: 1 addition & 0 deletions test/configCases/async-commons-chunk/existing-name/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "a";
1 change: 1 addition & 0 deletions test/configCases/async-commons-chunk/existing-name/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "b";
1 change: 1 addition & 0 deletions test/configCases/async-commons-chunk/existing-name/c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "c";
33 changes: 33 additions & 0 deletions test/configCases/async-commons-chunk/existing-name/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require("should");
const sinon = require("sinon");
const chunkLoadingSpy = sinon.spy(__webpack_require__, 'e');

it("should not have duplicate chunks in blocks", function(done) {
// This split point should contain: a
require.ensure([], function(require) {
require("./a").should.be.eql("a");
}, 'a');

// This split point should contain: a and b - we use CommonsChunksPlugin to
// have it only contain b and make chunk a be an async dependency.
require.ensure([], function(require) {
require("./a").should.be.eql("a");
require("./b").should.be.eql("b");
}, 'a+b');

// This split point should contain: a, b and c - we use CommonsChunksPlugin to
// have it only contain c and make chunks a and a+b be async dependencies.
require.ensure([], function(require) {
require("./a").should.be.eql("a");
require("./b").should.be.eql("b");
require("./c").should.be.eql("c");
}, 'a+b+c');

// Each of the require.ensures above should end up resolving chunks:
// - a
// - a, a+b
// - a, a+b, a+b+c
chunkLoadingSpy.callCount.should.be.eql(6);
chunkLoadingSpy.args.should.be.eql([[0], [0], [1], [0], [1], [2]]);
done();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var webpack = require("../../../../");

module.exports = {
plugins: [
new webpack.optimize.CommonsChunkPlugin({
chunks: ["a+b", "a+b+c"],
async: "a+b",
}),
new webpack.optimize.CommonsChunkPlugin({
chunks: ["a", "a+b"],
async: "a",
}),
]
};

0 comments on commit 7834e6c

Please sign in to comment.