Skip to content

Commit

Permalink
Fixed: watch configuration files. (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored and danez committed Jun 16, 2017
1 parent de2d3f3 commit 91f2658
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 63 deletions.
27 changes: 18 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ const babel = require("babel-core");
const loaderUtils = require("loader-utils");
const path = require("path");
const cache = require("./fs-cache.js");
const exists = require("./utils/exists")();
const exists = require("./utils/exists");
const relative = require("./utils/relative");
const read = require("./utils/read")();
const read = require("./utils/read");
const resolveRc = require("./resolve-rc.js");
const pkg = require("../package.json");
const fs = require("fs");

/**
* Error thrown by Babel formatted to conform to Webpack reporting.
Expand Down Expand Up @@ -109,6 +110,15 @@ module.exports = function(source, inputSourceMap) {

// Handle options
const loaderOptions = loaderUtils.getOptions(this) || {};
const fileSystem = this.fs ? this.fs : fs;
const babelrcPath = exists(fileSystem, loaderOptions.babelrc)
? loaderOptions.babelrc
: resolveRc(fileSystem, path.dirname(filename));

if (babelrcPath) {
this.addDependency(babelrcPath);
}

const defaultOptions = {
metadataSubscribers: [],
inputSourceMap: inputSourceMap,
Expand All @@ -117,13 +127,12 @@ module.exports = function(source, inputSourceMap) {
cacheIdentifier: JSON.stringify({
"babel-loader": pkg.version,
"babel-core": babel.version,
babelrc: exists(loaderOptions.babelrc)
? read(loaderOptions.babelrc)
: resolveRc(path.dirname(filename)),
env: loaderOptions.forceEnv ||
process.env.BABEL_ENV ||
process.env.NODE_ENV ||
"development",
babelrc: babelrcPath ? read(fileSystem, babelrcPath) : null,
env:
loaderOptions.forceEnv ||
process.env.BABEL_ENV ||
process.env.NODE_ENV ||
"development",
}),
};

Expand Down
22 changes: 8 additions & 14 deletions src/resolve-rc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,24 @@
* @see http://git.io/vLEvu
*/
const path = require("path");
const exists = require("./utils/exists")({});
const read = require("./utils/read")({});
const exists = require("./utils/exists");

const cache = {};

const find = function find(start, rel) {
const findBabelrcPath = function find(fileSystem, start, rel) {
const file = path.join(start, rel);

if (exists(file)) {
return read(file);
if (exists(fileSystem, file)) {
return file;
}

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

module.exports = function(loc, rel) {
module.exports = function(fileSystem, loc, rel) {
rel = rel || ".babelrc";
const cacheKey = `${loc}/${rel}`;
if (!(cacheKey in cache)) {
cache[cacheKey] = find(loc, rel);
}
return cache[cacheKey];

return findBabelrcPath(fileSystem, loc, rel);
};
23 changes: 11 additions & 12 deletions src/utils/exists.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
const fs = require("fs");
/**
* Check if file exists and cache the result
* return the result in cache
*
* @example
* var exists = require('./helpers/fsExists')({});
* exists('.babelrc'); // false
* var exists = require('./helpers/fsExists');
* exists(require('fs'), '.babelrc'); // false
*/
module.exports = function(cache) {
cache = cache || {};
module.exports = function(fileSystem, filename) {
if (!filename) return false;

return function(filename) {
if (!filename) return false;
let exists = false;

cache[filename] =
cache[filename] ||
(fs.existsSync(filename) && fs.statSync(filename).isFile());
try {
exists = fileSystem.statSync(filename).isFile();
} catch (ignoreError) {
return false;
}

return cache[filename];
};
return exists;
};
22 changes: 8 additions & 14 deletions src/utils/read.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
const fs = require("fs");
/**
* Read the file and cache the result
* return the result in cache
*
* @example
* var read = require('./helpers/fsExists')({});
* read('.babelrc'); // file contents...
* var read = require('./helpers/fsExists');
* read(require('fs'), '.babelrc'); // file contents...
*/
module.exports = function(cache) {
cache = cache || {};
module.exports = function(fileSystem, filename) {
if (!filename) {
throw new Error("filename must be a string");
}

return function(filename) {
if (!filename) {
throw new Error("filename must be a string");
}

cache[filename] = cache[filename] || fs.readFileSync(filename, "utf8");

return cache[filename];
};
// Webpack `fs` return Buffer
return fileSystem.readFileSync(filename).toString("utf8");
};
3 changes: 2 additions & 1 deletion test/resolverc.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import test from "ava";
import path from "path";
import resolveRc from "../lib/resolve-rc.js";
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(start);
const result = resolveRc(fs, start);

t.true(typeof result === "string");
});
13 changes: 5 additions & 8 deletions test/utils/exists.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import test from "ava";
import path from "path";
import exists from "../../lib/utils/exists.js";
import fs from "fs";

const cache = {};
const files = {
const files = {
existent: path.join(__dirname, "../fixtures/basic.js"),
fake: path.join(__dirname, "../fixtures/nonExistentFile.js"),
};

test("should return boolean if file exists", (t) => {
const realFile = exists(cache)(files.existent);
const fakeFile = exists(cache)(files.fake);
test("should return boolean if file exists", t => {
const realFile = exists(fs, files.existent);
const fakeFile = exists(fs, files.fake);

t.true(realFile);
t.false(fakeFile);

t.true(cache[files.existent]);
t.false(cache[files.fake]);
});
8 changes: 3 additions & 5 deletions test/utils/read.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ import fs from "fs";
import path from "path";
import read from "../../lib/utils/read.js";

const cache = {};
const files = {
const files = {
existent: path.join(__dirname, "../fixtures/basic.js"),
};

const content = fs.readFileSync(files.existent, "utf8");

test("should return contents if file exists", (t) => {
const realFile = read(cache)(files.existent);
test("should return contents if file exists", t => {
const realFile = read(fs, files.existent);
t.is(realFile, content);
t.is(cache[files.existent], content);
});

0 comments on commit 91f2658

Please sign in to comment.