Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support watching of config files #460

Merged
merged 1 commit into from
Jun 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do fs.existsSync(filename) here instead of exception catching. Try catch will make v8 deoptimize this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danez webpack fs doesn't fs.existsSync, because exists is deprecated

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, maybe webpack would be open to add existsSync as it was undeprecated in 7.0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danez hm, your are right, in theory we can create issue and add comment in code, and when issue was resolved change code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danez btw, webpack have many places where use statSync and this does not cause any problems with performance, i think this can be left

} 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);
});