emit an error when using the CommonChunksPlugin wrongly

#946
This commit is contained in:
Tobias Koppers 2015-04-26 22:47:47 +02:00
parent f36f4a9b73
commit 84b34151da
2 changed files with 48 additions and 15 deletions

View File

@ -38,14 +38,15 @@ CommonsChunkPlugin.prototype.apply = function(compiler) {
var minSize = this.minSize;
compiler.plugin("this-compilation", function(compilation) {
compilation.plugin(["optimize-chunks", "optimize-extracted-chunks"], function(chunks) {
var commonChunks;
if(!chunkNames && (selectedChunks === false || async)) {
var commonChunks = chunks;
commonChunks = chunks;
} else if(Array.isArray(chunkNames)) {
var commonChunks = chunks.filter(function(chunk) {
commonChunks = chunks.filter(function(chunk) {
return chunkNames.indexOf(chunk.name) >= 0;
});
} else {
var commonChunks = chunks.filter(function(chunk) {
commonChunks = chunks.filter(function(chunk) {
return chunk.name === chunkNames;
});
}
@ -54,22 +55,28 @@ CommonsChunkPlugin.prototype.apply = function(compiler) {
chunk.initial = chunk.entry = true;
commonChunks = [chunk];
}
commonChunks.forEach(function processCommonChunk(commonChunk) {
commonChunks.forEach(function processCommonChunk(commonChunk, idx) {
var commonModulesCount = [];
var commonModules = [];
var usedChunks;
if(Array.isArray(selectedChunks)) {
var usedChunks = chunks.filter(function(chunk) {
usedChunks = chunks.filter(function(chunk) {
if(chunk === commonChunk) return false;
return selectedChunks.indexOf(chunk.name) >= 0;
});
} else if(selectedChunks === false || !commonChunk.entry || async) {
var usedChunks = (commonChunk.chunks || []).filter(function(chunk) {
} else if(selectedChunks === false || async) {
usedChunks = (commonChunk.chunks || []).filter(function(chunk) {
// we can only move modules from this chunk if the "commonChunk" is the only parent
return async || chunk.parents.length === 1;
});
} else {
var usedChunks = chunks.filter(function(chunk) {
if(chunk === commonChunk) return false;
if(!commonChunk.entry) {
compilation.errors.push(new Error("CommonsChunkPlugin: While running in normal mode it's not allowed to use a non-entry chunk"));
return;
}
usedChunks = chunks.filter(function(chunk) {
var found = commonChunks.indexOf(chunk);
if(found >= idx) return false;
return chunk.entry;
});
}

View File

@ -1,3 +1,4 @@
/*globals describe it */
var should = require("should");
var path = require("path");
@ -6,9 +7,7 @@ var webpack = require("../lib/webpack");
var base = path.join(__dirname, "fixtures", "errors");
describe("Errors", function() {
function getErrors(options, callback) {
options.context = base;
var c = webpack(options);
function customOutputFilesystem(c) {
var files = {};
c.outputFileSystem = {
join: path.join.bind(path),
@ -20,6 +19,12 @@ describe("Errors", function() {
callback();
}
};
return files;
}
function getErrors(options, callback) {
options.context = base;
var c = webpack(options);
customOutputFilesystem(c);
c.run(function(err, stats) {
if(err) throw err;
should.strictEqual(typeof stats, "object");
@ -27,8 +32,8 @@ describe("Errors", function() {
should.strictEqual(typeof stats, "object");
stats.should.have.property("errors");
stats.should.have.property("warnings");
Array.isArray(stats.errors).should.be.ok;
Array.isArray(stats.warnings).should.be.ok;
Array.isArray(stats.errors).should.be.ok; // eslint-disable-line no-unused-expressions
Array.isArray(stats.warnings).should.be.ok; // eslint-disable-line no-unused-expressions
callback(stats.errors, stats.warnings);
});
}
@ -44,7 +49,7 @@ describe("Errors", function() {
lines[1].should.match(/^Module not found/);
lines[1].should.match(/\.\/dir\/missing2/);
lines[2].should.match(/missingFile.js 12:9/);
var lines = errors[1].split("\n");
lines = errors[1].split("\n");
lines[0].should.match(/missingFile.js/);
lines[1].should.match(/^Module not found/);
lines[1].should.match(/\.\/missing/);
@ -101,4 +106,25 @@ describe("Errors", function() {
done();
});
});
it("should throw an error when using incorrect CommonsChunkPlugin configuration", function(done) {
getErrors({
entry: {
a: "./entry-point",
b: "./entry-point",
c: "./entry-point"
},
plugins: [
new webpack.optimize.CommonsChunkPlugin("a", "a.js", Infinity),
new webpack.optimize.CommonsChunkPlugin("b", "b.js", Infinity)
]
}, function(errors, warnings) {
errors.length.should.be.eql(1);
warnings.length.should.be.eql(0);
var lines = errors[0].split("\n");
lines[0].should.match(/CommonsChunkPlugin/);
lines[0].should.match(/non-entry/);
done();
});
});
});