Skip to content

Commit d8b73c0

Browse files
authored
fix(resolve-rc): look for babel in package.json and .babelrc.js (#465)
1 parent 8d96c1f commit d8b73c0

File tree

8 files changed

+232
-235
lines changed

8 files changed

+232
-235
lines changed

.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"presets": [
3-
["env", { "loose": true, "targets": { "node": 4 } }]
3+
["env", { "loose": true, "targets": { "node": "4.2" } }]
44
],
55
"env": {
66
"test": {

src/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,11 @@ module.exports = function(source, inputSourceMap) {
128128
"babel-loader": pkg.version,
129129
"babel-core": babel.version,
130130
babelrc: babelrcPath ? read(fileSystem, babelrcPath) : null,
131-
env: loaderOptions.forceEnv ||
132-
process.env.BABEL_ENV ||
133-
process.env.NODE_ENV ||
134-
"development",
131+
env:
132+
loaderOptions.forceEnv ||
133+
process.env.BABEL_ENV ||
134+
process.env.NODE_ENV ||
135+
"development",
135136
}),
136137
};
137138

src/resolve-rc.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
1-
/**
2-
* The purpose of this module, is to find the project's .babelrc and
3-
* use its contents to bust the babel-loader's internal cache whenever an option
4-
* changes.
5-
*
6-
* @see https://github.com/babel/babel-loader/issues/62
7-
* @see http://git.io/vLEvu
8-
*/
91
const path = require("path");
102
const exists = require("./utils/exists");
113

12-
const findBabelrcPath = function find(fileSystem, start, rel) {
13-
const file = path.join(start, rel);
4+
module.exports = function find(fileSystem, start) {
5+
for (const fileName of [".babelrc", ".babelrc.js", "package.json"]) {
6+
const file = path.join(start, fileName);
147

15-
if (exists(fileSystem, file)) {
16-
return file;
8+
if (exists(fileSystem, file)) {
9+
if (
10+
fileName !== "package.json" ||
11+
typeof require(file).babel === "object"
12+
) {
13+
return file;
14+
}
15+
}
1716
}
1817

1918
const up = path.dirname(start);
19+
20+
// Reached root
2021
if (up !== start) {
21-
// Reached root
22-
return find(fileSystem, up, rel);
22+
return find(fileSystem, up);
2323
}
2424
};
25-
26-
module.exports = function(fileSystem, loc, rel) {
27-
rel = rel || ".babelrc";
28-
29-
return findBabelrcPath(fileSystem, loc, rel);
30-
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = {};

test/fixtures/babelrc-test/1/2/5/4/.gitkeep

Whitespace-only changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "package-test",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"babel": {
13+
"stage": 3
14+
}
15+
}

test/resolverc.test.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
import test from "ava";
22
import path from "path";
3-
import resolveRc from "../lib/resolve-rc.js";
3+
import resolveRc from "../lib/resolve-rc";
44
import fs from "fs";
55

66
test("should find the .babelrc file", t => {
77
const start = path.join(__dirname, "fixtures/babelrc-test/1/2/3");
88
const result = resolveRc(fs, start);
99

10-
t.true(typeof result === "string");
10+
t.is(result, path.join(__dirname, "fixtures/babelrc-test/.babelrc"));
11+
});
12+
13+
test("should find the .babelrc.js config", t => {
14+
const start = path.join(__dirname, "fixtures/babelrc-test/1/2/5/4");
15+
const result = resolveRc(fs, start);
16+
17+
t.is(result, path.join(__dirname, "fixtures/babelrc-test/1/2/5/.babelrc.js"));
18+
});
19+
20+
test("should find the package.json babel config", t => {
21+
const start = path.join(__dirname, "fixtures/package-test");
22+
const result = resolveRc(fs, start);
23+
24+
t.is(result, path.join(__dirname, "fixtures/package-test/package.json"));
1125
});

0 commit comments

Comments
 (0)