From 7834e6cd570317d3e81c613b98c60defc85c1001 Mon Sep 17 00:00:00 2001 From: Michael Yong Date: Tue, 30 May 2017 20:56:34 -0700 Subject: [PATCH] CommonsChunkPlugin: Don't allow blocks to contain duplicate chunks - Add configCase to repro failure --- lib/optimize/CommonsChunkPlugin.js | 4 ++- .../async-commons-chunk/existing-name/a.js | 1 + .../async-commons-chunk/existing-name/b.js | 1 + .../async-commons-chunk/existing-name/c.js | 1 + .../existing-name/index.js | 33 +++++++++++++++++++ .../existing-name/webpack.config.js | 14 ++++++++ 6 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 test/configCases/async-commons-chunk/existing-name/a.js create mode 100644 test/configCases/async-commons-chunk/existing-name/b.js create mode 100644 test/configCases/async-commons-chunk/existing-name/c.js create mode 100644 test/configCases/async-commons-chunk/existing-name/index.js create mode 100644 test/configCases/async-commons-chunk/existing-name/webpack.config.js diff --git a/lib/optimize/CommonsChunkPlugin.js b/lib/optimize/CommonsChunkPlugin.js index 4a9bd38da33..99bb1aebb1c 100644 --- a/lib/optimize/CommonsChunkPlugin.js +++ b/lib/optimize/CommonsChunkPlugin.js @@ -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); } } diff --git a/test/configCases/async-commons-chunk/existing-name/a.js b/test/configCases/async-commons-chunk/existing-name/a.js new file mode 100644 index 00000000000..01cd3e7139e --- /dev/null +++ b/test/configCases/async-commons-chunk/existing-name/a.js @@ -0,0 +1 @@ +module.exports = "a"; \ No newline at end of file diff --git a/test/configCases/async-commons-chunk/existing-name/b.js b/test/configCases/async-commons-chunk/existing-name/b.js new file mode 100644 index 00000000000..fba3204fe66 --- /dev/null +++ b/test/configCases/async-commons-chunk/existing-name/b.js @@ -0,0 +1 @@ +module.exports = "b"; \ No newline at end of file diff --git a/test/configCases/async-commons-chunk/existing-name/c.js b/test/configCases/async-commons-chunk/existing-name/c.js new file mode 100644 index 00000000000..ebaf7584d34 --- /dev/null +++ b/test/configCases/async-commons-chunk/existing-name/c.js @@ -0,0 +1 @@ +module.exports = "c"; \ No newline at end of file diff --git a/test/configCases/async-commons-chunk/existing-name/index.js b/test/configCases/async-commons-chunk/existing-name/index.js new file mode 100644 index 00000000000..72e5842dd18 --- /dev/null +++ b/test/configCases/async-commons-chunk/existing-name/index.js @@ -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(); +}); diff --git a/test/configCases/async-commons-chunk/existing-name/webpack.config.js b/test/configCases/async-commons-chunk/existing-name/webpack.config.js new file mode 100644 index 00000000000..2c8ad555fec --- /dev/null +++ b/test/configCases/async-commons-chunk/existing-name/webpack.config.js @@ -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", + }), + ] +};