Skip to content

Commit 84b3415

Browse files
committed
emit an error when using the CommonChunksPlugin wrongly
webpack#946
1 parent f36f4a9 commit 84b3415

2 files changed

Lines changed: 48 additions & 15 deletions

File tree

‎lib/optimize/CommonsChunkPlugin.js‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ CommonsChunkPlugin.prototype.apply = function(compiler) {
3838
var minSize = this.minSize;
3939
compiler.plugin("this-compilation", function(compilation) {
4040
compilation.plugin(["optimize-chunks", "optimize-extracted-chunks"], function(chunks) {
41+
var commonChunks;
4142
if(!chunkNames && (selectedChunks === false || async)) {
42-
var commonChunks = chunks;
43+
commonChunks = chunks;
4344
} else if(Array.isArray(chunkNames)) {
44-
var commonChunks = chunks.filter(function(chunk) {
45+
commonChunks = chunks.filter(function(chunk) {
4546
return chunkNames.indexOf(chunk.name) >= 0;
4647
});
4748
} else {
48-
var commonChunks = chunks.filter(function(chunk) {
49+
commonChunks = chunks.filter(function(chunk) {
4950
return chunk.name === chunkNames;
5051
});
5152
}
@@ -54,22 +55,28 @@ CommonsChunkPlugin.prototype.apply = function(compiler) {
5455
chunk.initial = chunk.entry = true;
5556
commonChunks = [chunk];
5657
}
57-
commonChunks.forEach(function processCommonChunk(commonChunk) {
58+
commonChunks.forEach(function processCommonChunk(commonChunk, idx) {
5859
var commonModulesCount = [];
5960
var commonModules = [];
61+
var usedChunks;
6062
if(Array.isArray(selectedChunks)) {
61-
var usedChunks = chunks.filter(function(chunk) {
63+
usedChunks = chunks.filter(function(chunk) {
6264
if(chunk === commonChunk) return false;
6365
return selectedChunks.indexOf(chunk.name) >= 0;
6466
});
65-
} else if(selectedChunks === false || !commonChunk.entry || async) {
66-
var usedChunks = (commonChunk.chunks || []).filter(function(chunk) {
67+
} else if(selectedChunks === false || async) {
68+
usedChunks = (commonChunk.chunks || []).filter(function(chunk) {
6769
// we can only move modules from this chunk if the "commonChunk" is the only parent
6870
return async || chunk.parents.length === 1;
6971
});
7072
} else {
71-
var usedChunks = chunks.filter(function(chunk) {
72-
if(chunk === commonChunk) return false;
73+
if(!commonChunk.entry) {
74+
compilation.errors.push(new Error("CommonsChunkPlugin: While running in normal mode it's not allowed to use a non-entry chunk"));
75+
return;
76+
}
77+
usedChunks = chunks.filter(function(chunk) {
78+
var found = commonChunks.indexOf(chunk);
79+
if(found >= idx) return false;
7380
return chunk.entry;
7481
});
7582
}

‎test/Errors.test.js‎

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/*globals describe it */
12
var should = require("should");
23
var path = require("path");
34

@@ -6,9 +7,7 @@ var webpack = require("../lib/webpack");
67
var base = path.join(__dirname, "fixtures", "errors");
78

89
describe("Errors", function() {
9-
function getErrors(options, callback) {
10-
options.context = base;
11-
var c = webpack(options);
10+
function customOutputFilesystem(c) {
1211
var files = {};
1312
c.outputFileSystem = {
1413
join: path.join.bind(path),
@@ -20,15 +19,21 @@ describe("Errors", function() {
2019
callback();
2120
}
2221
};
22+
return files;
23+
}
24+
function getErrors(options, callback) {
25+
options.context = base;
26+
var c = webpack(options);
27+
customOutputFilesystem(c);
2328
c.run(function(err, stats) {
2429
if(err) throw err;
2530
should.strictEqual(typeof stats, "object");
2631
stats = stats.toJson({ errorDetails: false });
2732
should.strictEqual(typeof stats, "object");
2833
stats.should.have.property("errors");
2934
stats.should.have.property("warnings");
30-
Array.isArray(stats.errors).should.be.ok;
31-
Array.isArray(stats.warnings).should.be.ok;
35+
Array.isArray(stats.errors).should.be.ok; // eslint-disable-line no-unused-expressions
36+
Array.isArray(stats.warnings).should.be.ok; // eslint-disable-line no-unused-expressions
3237
callback(stats.errors, stats.warnings);
3338
});
3439
}
@@ -44,7 +49,7 @@ describe("Errors", function() {
4449
lines[1].should.match(/^Module not found/);
4550
lines[1].should.match(/\.\/dir\/missing2/);
4651
lines[2].should.match(/missingFile.js 12:9/);
47-
var lines = errors[1].split("\n");
52+
lines = errors[1].split("\n");
4853
lines[0].should.match(/missingFile.js/);
4954
lines[1].should.match(/^Module not found/);
5055
lines[1].should.match(/\.\/missing/);
@@ -101,4 +106,25 @@ describe("Errors", function() {
101106
done();
102107
});
103108
});
109+
it("should throw an error when using incorrect CommonsChunkPlugin configuration", function(done) {
110+
getErrors({
111+
entry: {
112+
a: "./entry-point",
113+
b: "./entry-point",
114+
c: "./entry-point"
115+
},
116+
plugins: [
117+
new webpack.optimize.CommonsChunkPlugin("a", "a.js", Infinity),
118+
new webpack.optimize.CommonsChunkPlugin("b", "b.js", Infinity)
119+
]
120+
}, function(errors, warnings) {
121+
errors.length.should.be.eql(1);
122+
warnings.length.should.be.eql(0);
123+
var lines = errors[0].split("\n");
124+
lines[0].should.match(/CommonsChunkPlugin/);
125+
lines[0].should.match(/non-entry/);
126+
done();
127+
});
128+
129+
});
104130
});

0 commit comments

Comments
 (0)