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

Commit e595417

Browse files
numicalmichael-ciniawsky
authored andcommitted
fix: don't extract from common async chunks (#508)
1 parent a8ae003 commit e595417

File tree

9 files changed

+51
-5
lines changed

9 files changed

+51
-5
lines changed

index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ function ExtractTextPluginCompilation() {
1919
this.modulesByIdentifier = {};
2020
}
2121

22+
function isInitialOrHasNoParents(chunk) {
23+
return chunk.isInitial() || chunk.parents.length === 0;
24+
}
25+
2226
ExtractTextPlugin.prototype.mergeNonInitialChunks = function(chunk, intoChunk, checkedChunks) {
2327
if(!intoChunk) {
2428
checkedChunks = [];
2529
chunk.chunks.forEach(function(c) {
26-
if(c.isInitial()) return;
30+
if(isInitialOrHasNoParents(c)) return;
2731
this.mergeNonInitialChunks(c, chunk, checkedChunks);
2832
}, this);
2933
} else if(checkedChunks.indexOf(chunk) < 0) {
@@ -33,7 +37,7 @@ ExtractTextPlugin.prototype.mergeNonInitialChunks = function(chunk, intoChunk, c
3337
module.addChunk(intoChunk);
3438
});
3539
chunk.chunks.forEach(function(c) {
36-
if(c.isInitial()) return;
40+
if(isInitialOrHasNoParents(c)) return;
3741
this.mergeNonInitialChunks(c, intoChunk, checkedChunks);
3842
}, this);
3943
}
@@ -262,7 +266,7 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
262266
});
263267
async.forEach(chunks, function(chunk, callback) {
264268
var extractedChunk = extractedChunks[chunks.indexOf(chunk)];
265-
var shouldExtract = !!(options.allChunks || chunk.isInitial());
269+
var shouldExtract = !!(options.allChunks || isInitialOrHasNoParents(chunk));
266270
async.forEach(chunk.modules.slice(), function(module, callback) {
267271
var meta = module[NS];
268272
if(meta && (!meta.options.id || meta.options.id === id)) {
@@ -298,11 +302,11 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
298302
}, function(err) {
299303
if(err) return callback(err);
300304
extractedChunks.forEach(function(extractedChunk) {
301-
if(extractedChunk.isInitial())
305+
if(isInitialOrHasNoParents(extractedChunk))
302306
this.mergeNonInitialChunks(extractedChunk);
303307
}, this);
304308
extractedChunks.forEach(function(extractedChunk) {
305-
if(!extractedChunk.isInitial()) {
309+
if(!isInitialOrHasNoParents(extractedChunk)) {
306310
extractedChunk.modules.slice().forEach(function(module) {
307311
extractedChunk.removeModule(module);
308312
});

test/cases/common-async/a.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require("./a.txt");
2+
require("./b.txt");

test/cases/common-async/a.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a

test/cases/common-async/b.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require("./a.txt");
2+
require("./c.txt");
3+

test/cases/common-async/b.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
b

test/cases/common-async/c.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
a
2+
b
3+
c

test/cases/common-async/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require.ensure(
2+
[],
3+
function() {
4+
require("./a.js");
5+
},
6+
'async-chunk-a'
7+
);
8+
require.ensure(
9+
[],
10+
function() {
11+
require("./b.js");
12+
},
13+
'async-chunk-b'
14+
);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var webpack = require('webpack');
2+
var ExtractTextPlugin = require("../../../index");
3+
4+
module.exports = {
5+
entry: "./index",
6+
plugins: [
7+
new webpack.optimize.CommonsChunkPlugin({
8+
name: 'common',
9+
filename: 'common.js',
10+
chunks: ['async-chunk-a', 'async-chunk-b']
11+
}),
12+
new ExtractTextPlugin({
13+
filename: "file.css",
14+
allChunks: true
15+
})
16+
]
17+
};

0 commit comments

Comments
 (0)