From 91f2658e039b036b02f04aff0bee719c839e99ce Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Fri, 16 Jun 2017 17:45:29 +0300 Subject: [PATCH] Fixed: watch configuration files. (#460) --- src/index.js | 27 ++++++++++++++++++--------- src/resolve-rc.js | 22 ++++++++-------------- src/utils/exists.js | 23 +++++++++++------------ src/utils/read.js | 22 ++++++++-------------- test/resolverc.test.js | 3 ++- test/utils/exists.test.js | 13 +++++-------- test/utils/read.test.js | 8 +++----- 7 files changed, 55 insertions(+), 63 deletions(-) diff --git a/src/index.js b/src/index.js index 060224eb..36c3d641 100644 --- a/src/index.js +++ b/src/index.js @@ -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. @@ -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, @@ -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", }), }; diff --git a/src/resolve-rc.js b/src/resolve-rc.js index 24fcdc89..9a6d6d32 100644 --- a/src/resolve-rc.js +++ b/src/resolve-rc.js @@ -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); }; diff --git a/src/utils/exists.js b/src/utils/exists.js index e1f117dc..c01fb68a 100644 --- a/src/utils/exists.js +++ b/src/utils/exists.js @@ -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; }; diff --git a/src/utils/read.js b/src/utils/read.js index d35e6f46..ae1ee07d 100644 --- a/src/utils/read.js +++ b/src/utils/read.js @@ -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"); }; diff --git a/test/resolverc.test.js b/test/resolverc.test.js index 576ab85c..a7e5ad14 100644 --- a/test/resolverc.test.js +++ b/test/resolverc.test.js @@ -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"); }); diff --git a/test/utils/exists.test.js b/test/utils/exists.test.js index 03b87cf7..74dc5df3 100644 --- a/test/utils/exists.test.js +++ b/test/utils/exists.test.js @@ -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]); }); diff --git a/test/utils/read.test.js b/test/utils/read.test.js index cc82d220..ed8da28c 100644 --- a/test/utils/read.test.js +++ b/test/utils/read.test.js @@ -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); });