Skip to content

Commit 998870d

Browse files
committed
Warn about modules with matching names if case is ignored
fixes webpack#210
1 parent 67cae93 commit 998870d

6 files changed

Lines changed: 62 additions & 0 deletions

File tree

lib/CaseSensitiveModulesWarning.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
function CaseSensitiveModulesWarning(module) {
6+
Error.call(this);
7+
Error.captureStackTrace(this, CaseSensitiveModulesWarning);
8+
this.name = "CaseSensitiveModulesWarning";
9+
this.message = "There is another module with a equal name when case is ignored.\n" +
10+
"This can lead to unexpected behavior when compiling on a filesystem with other case-semantic.\n" +
11+
"Rename module if multiple modules are expected or use equal casing if one module is expected.";
12+
this.origin = this.module = module;
13+
}
14+
module.exports = CaseSensitiveModulesWarning;
15+
16+
CaseSensitiveModulesWarning.prototype = Object.create(Error.prototype);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var CaseSensitiveModulesWarning = require("./CaseSensitiveModulesWarning");
6+
7+
function WarnCaseSensitiveModulesPlugin() {
8+
}
9+
module.exports = WarnCaseSensitiveModulesPlugin;
10+
11+
WarnCaseSensitiveModulesPlugin.prototype.apply = function(compiler) {
12+
compiler.plugin("compilation", function(compilation) {
13+
compilation.plugin("seal", function() {
14+
var moduleWithoutCase = {};
15+
this.modules.forEach(function(module) {
16+
var ident = module.identifier().toLowerCase();
17+
if(moduleWithoutCase["$"+ident]) {
18+
if(moduleWithoutCase["$"+ident] !== true)
19+
this.warnings.push(new CaseSensitiveModulesWarning(moduleWithoutCase["$"+ident]));
20+
this.warnings.push(new CaseSensitiveModulesWarning(module));
21+
moduleWithoutCase["$"+ident] = true;
22+
} else {
23+
moduleWithoutCase["$"+ident] = module;
24+
}
25+
}, this);
26+
});
27+
});
28+
};

lib/WebpackOptionsApply.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var CompatibilityPlugin = require("./CompatibilityPlugin");
2121
var DefinePlugin = require("./DefinePlugin");
2222

2323
var MovedToPluginWarningPlugin = require("./MovedToPluginWarningPlugin");
24+
var WarnCaseSensitiveModulesPlugin = require("./WarnCaseSensitiveModulesPlugin");
2425

2526
var LoaderPlugin = require("./dependencies/LoaderPlugin");
2627
var CommonJsPlugin = require("./dependencies/CommonJsPlugin");
@@ -203,6 +204,8 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
203204

204205
compiler.apply(new RecordIdsPlugin());
205206

207+
compiler.apply(new WarnCaseSensitiveModulesPlugin());
208+
206209
if(options.optimize && options.optimize.occurenceOrder) {
207210
compiler.apply(new MovedToPluginWarningPlugin("optimize.occurenceOrder", "optimize.OccurrenceOrderPlugin"));
208211
var OccurrenceOrderPlugin = require("./optimize/OccurrenceOrderPlugin");

test/Errors.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,17 @@ describe("Errors", function() {
8282
done();
8383
});
8484
});
85+
it("should warn about case-sensitive module names", function(done) {
86+
getErrors({
87+
entry: "./case-sensitive"
88+
}, function(errors, warnings) {
89+
errors.length.should.be.eql(0);
90+
warnings.length.should.be.eql(2);
91+
var lines = warnings[0].split("\n");
92+
lines[0].should.match(/file\.js/);
93+
lines = warnings[1].split("\n");
94+
lines[0].should.match(/FILE\.js/);
95+
done();
96+
});
97+
});
8598
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require("./file");
2+
require("./FILE");

test/fixtures/errors/file.js

Whitespace-only changes.

0 commit comments

Comments
 (0)