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

fix: don't extract from common async chunks #508

Merged
merged 1 commit into from
Jun 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,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 @@ -35,7 +39,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 @@ -264,7 +268,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 @@ -300,11 +304,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require("./a.txt");
require("./b.txt");
1 change: 1 addition & 0 deletions test/cases/common-async/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
3 changes: 3 additions & 0 deletions test/cases/common-async/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require("./a.txt");
require("./c.txt");

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