Skip to content

Commit 392c6db

Browse files
committed
more useful error when using require.extensions webpack#104
1 parent cff436e commit 392c6db

File tree

6 files changed

+100
-1
lines changed

6 files changed

+100
-1
lines changed

lib/NodeStuffPlugin.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var ModuleAliasPlugin = require("enhanced-resolve/lib/ModuleAliasPlugin");
77
var ModuleParserHelpers = require("./ModuleParserHelpers");
88
var ConstDependency = require("./dependencies/ConstDependency");
99
var BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
10+
var UnsupportedFeatureWarning = require("./UnsupportedFeatureWarning");
1011

1112
function NodeStuffPlugin(options, context) {
1213
this.options = options;
@@ -62,6 +63,11 @@ NodeStuffPlugin.prototype.apply = function(compiler) {
6263
this.state.current.addDependency(dep);
6364
return true;
6465
});
66+
compiler.parser.plugin("expression require.extensions", function(expr) {
67+
if(!this.state.module) return;
68+
this.state.module.warnings.push(new UnsupportedFeatureWarning(this.state.module, "require.extensions is not supported by webpack. Use a loader instead."));
69+
return true;
70+
});
6571
compiler.parser.plugin("expression module.exports", ignore);
6672
compiler.parser.plugin("expression module.loaded", ignore);
6773
compiler.parser.plugin("expression module.id", ignore);

lib/UnsupportedFeatureWarning.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
function UnsupportedFeatureWarning(module, message) {
6+
Error.call(this);
7+
Error.captureStackTrace(this, UnsupportedFeatureWarning);
8+
this.name = "UnsupportedFeatureWarning";
9+
this.message = message;
10+
this.origin = this.module = module;
11+
}
12+
module.exports = UnsupportedFeatureWarning;
13+
14+
UnsupportedFeatureWarning.prototype = Object.create(Error.prototype);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webpack",
3-
"version": "0.11.0-beta19",
3+
"version": "0.11.0-beta20",
44
"author": "Tobias Koppers @sokra",
55
"description": "Packs CommonJs/AMD/Labeled Modules for the browser. Allows to split your codebase into multiple bundles, which can be loaded on demand. Support loaders to preprocess files, i.e. json, jade, coffee, css, less, ... and your custom stuff.",
66
"dependencies": {

test/Errors.test.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
var should = require("should");
2+
var path = require("path");
3+
4+
var webpack = require("../lib/webpack");
5+
6+
var base = path.join(__dirname, "fixtures", "errors");
7+
8+
describe("Errors", function() {
9+
function getErrors(options, callback) {
10+
options.context = base;
11+
var c = webpack(options);
12+
var files = {};
13+
c.outputFileSystem = {
14+
join: path.join.bind(path),
15+
mkdirp: function(path, callback) {
16+
callback();
17+
},
18+
writeFile: function(name, content, callback) {
19+
files[name] = content.toString("utf-8");
20+
callback();
21+
}
22+
};
23+
c.run(function(err, stats) {
24+
if(err) throw err;
25+
should.exist(stats);
26+
stats = stats.toJson();
27+
should.exist(stats);
28+
stats.should.have.property("errors");
29+
stats.should.have.property("warnings");
30+
Array.isArray(stats.errors).should.be.ok;
31+
Array.isArray(stats.warnings).should.be.ok;
32+
callback(stats.errors, stats.warnings);
33+
});
34+
}
35+
it("should throw an error if file doesn't exist", function(done) {
36+
getErrors({
37+
entry: "./missingFile"
38+
}, function(errors, warnings) {
39+
errors.length.should.be.eql(2);
40+
warnings.length.should.be.eql(0);
41+
var lines = errors[0].split("\n");
42+
lines[0].should.match(/missingFile.js/);
43+
lines[1].should.match(/^Module not found/);
44+
lines[1].should.match(/\.\/missing/);
45+
lines[2].should.match(/missingFile.js 4:0/);
46+
var lines = errors[1].split("\n");
47+
lines[0].should.match(/missingFile.js/);
48+
lines[1].should.match(/^Module not found/);
49+
lines[1].should.match(/\.\/dir\/missing2/);
50+
lines[2].should.match(/missingFile.js 12:9/);
51+
done();
52+
});
53+
});
54+
it("should report require.extensions as unsupported", function(done) {
55+
getErrors({
56+
entry: "./require.extensions"
57+
}, function(errors, warnings) {
58+
errors.length.should.be.eql(0);
59+
warnings.length.should.be.eql(1);
60+
var lines = warnings[0].split("\n");
61+
lines[0].should.match(/require.extensions.js/);
62+
lines[1].should.match(/require.extensions is not supported by webpack/);
63+
done();
64+
});
65+
});
66+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
3+
// on line 4
4+
require("./missing");
5+
6+
7+
8+
9+
10+
11+
// on line 12 char 10
12+
require("./dir/missing2");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require.extensions[".js"] = function() {};

0 commit comments

Comments
 (0)