Skip to content

Commit

Permalink
fix(resolve-rc): look for babel in package.json and .babelrc.js (#465)
Browse files Browse the repository at this point in the history
  • Loading branch information
danez committed Jun 16, 2017
1 parent 8d96c1f commit d8b73c0
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 235 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"presets": [
["env", { "loose": true, "targets": { "node": 4 } }]
["env", { "loose": true, "targets": { "node": "4.2" } }]
],
"env": {
"test": {
Expand Down
9 changes: 5 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@ module.exports = function(source, inputSourceMap) {
"babel-loader": pkg.version,
"babel-core": babel.version,
babelrc: babelrcPath ? read(fileSystem, babelrcPath) : null,
env: loaderOptions.forceEnv ||
process.env.BABEL_ENV ||
process.env.NODE_ENV ||
"development",
env:
loaderOptions.forceEnv ||
process.env.BABEL_ENV ||
process.env.NODE_ENV ||
"development",
}),
};

Expand Down
34 changes: 14 additions & 20 deletions src/resolve-rc.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
/**
* The purpose of this module, is to find the project's .babelrc and
* use its contents to bust the babel-loader's internal cache whenever an option
* changes.
*
* @see https://github.com/babel/babel-loader/issues/62
* @see http://git.io/vLEvu
*/
const path = require("path");
const exists = require("./utils/exists");

const findBabelrcPath = function find(fileSystem, start, rel) {
const file = path.join(start, rel);
module.exports = function find(fileSystem, start) {
for (const fileName of [".babelrc", ".babelrc.js", "package.json"]) {
const file = path.join(start, fileName);

if (exists(fileSystem, file)) {
return file;
if (exists(fileSystem, file)) {
if (
fileName !== "package.json" ||
typeof require(file).babel === "object"
) {
return file;
}
}
}

const up = path.dirname(start);

// Reached root
if (up !== start) {
// Reached root
return find(fileSystem, up, rel);
return find(fileSystem, up);
}
};

module.exports = function(fileSystem, loc, rel) {
rel = rel || ".babelrc";

return findBabelrcPath(fileSystem, loc, rel);
};
1 change: 1 addition & 0 deletions test/fixtures/babelrc-test/1/2/5/.babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
Empty file.
15 changes: 15 additions & 0 deletions test/fixtures/package-test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "package-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"babel": {
"stage": 3
}
}
18 changes: 16 additions & 2 deletions test/resolverc.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import test from "ava";
import path from "path";
import resolveRc from "../lib/resolve-rc.js";
import resolveRc from "../lib/resolve-rc";
import fs from "fs";

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

t.true(typeof result === "string");
t.is(result, path.join(__dirname, "fixtures/babelrc-test/.babelrc"));
});

test("should find the .babelrc.js config", t => {
const start = path.join(__dirname, "fixtures/babelrc-test/1/2/5/4");
const result = resolveRc(fs, start);

t.is(result, path.join(__dirname, "fixtures/babelrc-test/1/2/5/.babelrc.js"));
});

test("should find the package.json babel config", t => {
const start = path.join(__dirname, "fixtures/package-test");
const result = resolveRc(fs, start);

t.is(result, path.join(__dirname, "fixtures/package-test/package.json"));
});
Loading

0 comments on commit d8b73c0

Please sign in to comment.