Skip to content

Commit 993aeab

Browse files
committed
Add support for .json files out of the box
See webpack#3363
1 parent 73886d5 commit 993aeab

6 files changed

Lines changed: 46 additions & 15 deletions

File tree

lib/CompatibilityPlugin.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ var ConstDependency = require("./dependencies/ConstDependency");
77

88
var NullFactory = require("./NullFactory");
99

10+
var jsonLoaderPath = require.resolve("json-loader");
11+
var matchJson = /\.json$/i;
12+
1013
function CompatibilityPlugin() {}
1114
module.exports = CompatibilityPlugin;
1215

@@ -37,5 +40,16 @@ CompatibilityPlugin.prototype.apply = function(compiler) {
3740
return true;
3841
});
3942
});
43+
44+
params.normalModuleFactory.plugin("after-resolve", function(data, done) {
45+
// if this is a json file and there are no loaders active, we use the json-loader in order to avoid parse errors
46+
// @see https://github.com/webpack/webpack/issues/3363
47+
if(matchJson.test(data.request) && data.loaders.length === 0) {
48+
data.loaders.push({
49+
loader: jsonLoaderPath
50+
});
51+
}
52+
done(null, data);
53+
});
4054
});
4155
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"async": "^2.1.2",
1212
"enhanced-resolve": "^2.2.0",
1313
"interpret": "^1.0.0",
14+
"json-loader": "^0.5.4",
1415
"loader-runner": "^2.2.0",
1516
"loader-utils": "^0.2.16",
1617
"memory-fs": "~0.3.0",
@@ -45,7 +46,6 @@
4546
"jade": "^1.11.0",
4647
"jade-loader": "~0.8.0",
4748
"js-beautify": "^1.5.10",
48-
"json-loader": "~0.5.1",
4949
"less": "^2.5.1",
5050
"less-loader": "^2.0.0",
5151
"mocha": "^3.1.0",

test/TestCases.test.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ describe("TestCases", function() {
128128
modules: ["web_modules", "node_modules"],
129129
mainFields: ["webpack", "browser", "web", "browserify", ["jam", "main"], "main"],
130130
aliasFields: ["browser"],
131-
extensions: [".webpack.js", ".web.js", ".js"]
131+
extensions: [".webpack.js", ".web.js", ".js", ".json"]
132132
},
133133
resolveLoader: {
134134
modules: ["web_loaders", "web_modules", "node_loaders", "node_modules"],
@@ -137,9 +137,6 @@ describe("TestCases", function() {
137137
},
138138
module: {
139139
loaders: [{
140-
test: /\.json$/,
141-
loader: "json-loader"
142-
}, {
143140
test: /\.coffee$/,
144141
loader: "coffee-loader"
145142
}, {
Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
1-
it("should handle the json loader correctly", function() {
2-
require("!json-loader!../../../../package.json").name.should.be.eql("webpack");
3-
require("../../../../package.json").name.should.be.eql("webpack");
1+
var should = require("should");
2+
3+
it("should be able to load JSON files without loader", function() {
4+
var someJson = require("./some.json");
5+
someJson.should.have.property("it", "works");
6+
someJson.should.have.property("number", 42);
7+
});
8+
9+
it("should also work when the json extension is omitted", function() {
10+
var someJson = require("./some");
11+
someJson.should.have.property("it", "works");
12+
someJson.should.have.property("number", 42);
13+
});
14+
15+
it("should still be possible to manually apply the json-loader for compatibility reasons", function() {
16+
var someJson = require("json-loader!./some.json");
17+
someJson.should.have.property("it", "works");
18+
someJson.should.have.property("number", 42);
19+
});
20+
21+
it("should still be possible to use a custom loader", function() {
22+
var someJson = JSON.parse(require("raw-loader!./some.json"));
23+
someJson.should.have.property("it", "works");
24+
someJson.should.have.property("number", 42);
425
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"it": "works",
3+
"number": 42
4+
}
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
module.exports = {
2-
target: "web",
3-
module: {
4-
loaders: [
5-
{ test: /\.json$/, loader: "json-loader" }
6-
]
7-
}
8-
};
2+
target: "web"
3+
};

0 commit comments

Comments
 (0)