From e59541723dee6f3a372671eb45973b2b5ed3e115 Mon Sep 17 00:00:00 2001 From: Mike Evans Date: Thu, 8 Jun 2017 05:48:38 +0100 Subject: [PATCH] fix: don't extract from common async chunks (#508) --- index.js | 14 +++++++++----- test/cases/common-async/a.js | 2 ++ test/cases/common-async/a.txt | 1 + test/cases/common-async/b.js | 3 +++ test/cases/common-async/b.txt | 1 + test/cases/common-async/c.txt | 1 + test/cases/common-async/expected/file.css | 3 +++ test/cases/common-async/index.js | 14 ++++++++++++++ test/cases/common-async/webpack.config.js | 17 +++++++++++++++++ 9 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 test/cases/common-async/a.js create mode 100644 test/cases/common-async/a.txt create mode 100644 test/cases/common-async/b.js create mode 100644 test/cases/common-async/b.txt create mode 100644 test/cases/common-async/c.txt create mode 100644 test/cases/common-async/expected/file.css create mode 100644 test/cases/common-async/index.js create mode 100644 test/cases/common-async/webpack.config.js diff --git a/index.js b/index.js index 0b7b992c..e6ee9e7a 100644 --- a/index.js +++ b/index.js @@ -19,11 +19,15 @@ function ExtractTextPluginCompilation() { this.modulesByIdentifier = {}; } +function isInitialOrHasNoParents(chunk) { + return chunk.isInitial() || chunk.parents.length === 0; +} + ExtractTextPlugin.prototype.mergeNonInitialChunks = function(chunk, intoChunk, checkedChunks) { if(!intoChunk) { checkedChunks = []; chunk.chunks.forEach(function(c) { - if(c.isInitial()) return; + if(isInitialOrHasNoParents(c)) return; this.mergeNonInitialChunks(c, chunk, checkedChunks); }, this); } else if(checkedChunks.indexOf(chunk) < 0) { @@ -33,7 +37,7 @@ ExtractTextPlugin.prototype.mergeNonInitialChunks = function(chunk, intoChunk, c module.addChunk(intoChunk); }); chunk.chunks.forEach(function(c) { - if(c.isInitial()) return; + if(isInitialOrHasNoParents(c)) return; this.mergeNonInitialChunks(c, intoChunk, checkedChunks); }, this); } @@ -262,7 +266,7 @@ ExtractTextPlugin.prototype.apply = function(compiler) { }); async.forEach(chunks, function(chunk, callback) { var extractedChunk = extractedChunks[chunks.indexOf(chunk)]; - var shouldExtract = !!(options.allChunks || chunk.isInitial()); + var shouldExtract = !!(options.allChunks || isInitialOrHasNoParents(chunk)); async.forEach(chunk.modules.slice(), function(module, callback) { var meta = module[NS]; if(meta && (!meta.options.id || meta.options.id === id)) { @@ -298,11 +302,11 @@ ExtractTextPlugin.prototype.apply = function(compiler) { }, function(err) { if(err) return callback(err); extractedChunks.forEach(function(extractedChunk) { - if(extractedChunk.isInitial()) + if(isInitialOrHasNoParents(extractedChunk)) this.mergeNonInitialChunks(extractedChunk); }, this); extractedChunks.forEach(function(extractedChunk) { - if(!extractedChunk.isInitial()) { + if(!isInitialOrHasNoParents(extractedChunk)) { extractedChunk.modules.slice().forEach(function(module) { extractedChunk.removeModule(module); }); diff --git a/test/cases/common-async/a.js b/test/cases/common-async/a.js new file mode 100644 index 00000000..2d7d820b --- /dev/null +++ b/test/cases/common-async/a.js @@ -0,0 +1,2 @@ +require("./a.txt"); +require("./b.txt"); diff --git a/test/cases/common-async/a.txt b/test/cases/common-async/a.txt new file mode 100644 index 00000000..78981922 --- /dev/null +++ b/test/cases/common-async/a.txt @@ -0,0 +1 @@ +a diff --git a/test/cases/common-async/b.js b/test/cases/common-async/b.js new file mode 100644 index 00000000..caac53f4 --- /dev/null +++ b/test/cases/common-async/b.js @@ -0,0 +1,3 @@ +require("./a.txt"); +require("./c.txt"); + diff --git a/test/cases/common-async/b.txt b/test/cases/common-async/b.txt new file mode 100644 index 00000000..61780798 --- /dev/null +++ b/test/cases/common-async/b.txt @@ -0,0 +1 @@ +b diff --git a/test/cases/common-async/c.txt b/test/cases/common-async/c.txt new file mode 100644 index 00000000..f2ad6c76 --- /dev/null +++ b/test/cases/common-async/c.txt @@ -0,0 +1 @@ +c diff --git a/test/cases/common-async/expected/file.css b/test/cases/common-async/expected/file.css new file mode 100644 index 00000000..de980441 --- /dev/null +++ b/test/cases/common-async/expected/file.css @@ -0,0 +1,3 @@ +a +b +c diff --git a/test/cases/common-async/index.js b/test/cases/common-async/index.js new file mode 100644 index 00000000..624fa419 --- /dev/null +++ b/test/cases/common-async/index.js @@ -0,0 +1,14 @@ +require.ensure( + [], + function() { + require("./a.js"); + }, + 'async-chunk-a' +); +require.ensure( + [], + function() { + require("./b.js"); + }, + 'async-chunk-b' +); diff --git a/test/cases/common-async/webpack.config.js b/test/cases/common-async/webpack.config.js new file mode 100644 index 00000000..758d08a1 --- /dev/null +++ b/test/cases/common-async/webpack.config.js @@ -0,0 +1,17 @@ +var webpack = require('webpack'); +var ExtractTextPlugin = require("../../../index"); + +module.exports = { + entry: "./index", + plugins: [ + new webpack.optimize.CommonsChunkPlugin({ + name: 'common', + filename: 'common.js', + chunks: ['async-chunk-a', 'async-chunk-b'] + }), + new ExtractTextPlugin({ + filename: "file.css", + allChunks: true + }) + ] +};