Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Commit

Permalink
fix: don't extract from common async chunks (#508)
Browse files Browse the repository at this point in the history
  • Loading branch information
numical authored and michael-ciniawsky committed Jun 8, 2017
1 parent a8ae003 commit e595417
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 5 deletions.
14 changes: 9 additions & 5 deletions index.js
Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
});
Expand Down
2 changes: 2 additions & 0 deletions test/cases/common-async/a.js
@@ -0,0 +1,2 @@
require("./a.txt");
require("./b.txt");
1 change: 1 addition & 0 deletions test/cases/common-async/a.txt
@@ -0,0 +1 @@
a
3 changes: 3 additions & 0 deletions test/cases/common-async/b.js
@@ -0,0 +1,3 @@
require("./a.txt");
require("./c.txt");

1 change: 1 addition & 0 deletions test/cases/common-async/b.txt
@@ -0,0 +1 @@
b
1 change: 1 addition & 0 deletions test/cases/common-async/c.txt
@@ -0,0 +1 @@
c
3 changes: 3 additions & 0 deletions test/cases/common-async/expected/file.css
@@ -0,0 +1,3 @@
a
b
c
14 changes: 14 additions & 0 deletions 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'
);
17 changes: 17 additions & 0 deletions 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
})
]
};

0 comments on commit e595417

Please sign in to comment.