Skip to content

Commit

Permalink
Use require instead of requireFromString.
Browse files Browse the repository at this point in the history
Although there's some niceness in using the string data straight from the filesystem, you lose the ability to use anything that hooks into the native `require` call – namely `babel` and friends. By using vanilla `require` it becomes possible to use ES6 files for your configuration (or indeed anything else that hooks into standard node module loading machinery). Performance-wise the change should be negligible, given the filesystem/stat cache will be hot from having just loaded the contents of the file.
  • Loading branch information
izaakschroeder committed Jan 3, 2017
1 parent 833385b commit e210a0c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
10 changes: 5 additions & 5 deletions lib/loadDefinedFile.js
Expand Up @@ -2,8 +2,8 @@

var yaml = require('js-yaml');
var parseJson = require('parse-json');
var requireFromString = require('require-from-string');
var readFile = require('./readFile');
var irequire = require('./require');

module.exports = function (filepath, options) {
return readFile(filepath, { throwNotFound: true }).then(function (content) {
Expand All @@ -16,7 +16,7 @@ module.exports = function (filepath, options) {
filename: filepath,
});
case 'js':
return requireFromString(content, filepath);
return irequire(filepath);
default:
return tryAllParsing(content, filepath);
}
Expand All @@ -37,7 +37,7 @@ module.exports = function (filepath, options) {

function tryAllParsing(content, filepath) {
return tryYaml(content, filepath, function () {
return tryRequire(content, filepath, function () {
return tryRequire(filepath, function () {
return null;
});
});
Expand All @@ -57,9 +57,9 @@ function tryYaml(content, filepath, cb) {
}
}

function tryRequire(content, filepath, cb) {
function tryRequire(filepath, cb) {
try {
return requireFromString(content, filepath);
return irequire(filepath);
} catch (e) {
return cb();
}
Expand Down
4 changes: 2 additions & 2 deletions lib/loadJs.js
@@ -1,14 +1,14 @@
'use strict';

var requireFromString = require('require-from-string');
var readFile = require('./readFile');
var irequire = require('./require');

module.exports = function (filepath) {
return readFile(filepath).then(function (content) {
if (!content) return null;

return {
config: requireFromString(content, filepath),
config: irequire(filepath),
filepath: filepath,
};
});
Expand Down
4 changes: 2 additions & 2 deletions lib/loadRc.js
Expand Up @@ -2,8 +2,8 @@

var yaml = require('js-yaml');
var parseJson = require('parse-json');
var requireFromString = require('require-from-string');
var readFile = require('./readFile');
var irequire = require('./require');

module.exports = function (filepath, options) {
return loadExtensionlessRc().then(function (result) {
Expand Down Expand Up @@ -68,7 +68,7 @@ module.exports = function (filepath, options) {
if (content.config) return content;
var successFilepath = filepath + '.js';
return {
config: requireFromString(content, successFilepath),
config: irequire(filepath),
filepath: successFilepath,
};
}
Expand Down
6 changes: 6 additions & 0 deletions lib/require.js
@@ -0,0 +1,6 @@
'use strict';

module.exports = function (path) {
var obj = require(path);
return obj && obj.__esModule ? obj['default'] : obj;
};
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -36,8 +36,7 @@
"minimist": "^1.2.0",
"object-assign": "^4.1.0",
"os-homedir": "^1.0.1",
"parse-json": "^2.2.0",
"require-from-string": "^1.1.0"
"parse-json": "^2.2.0"
},
"devDependencies": {
"ava": "0.16.0",
Expand Down

0 comments on commit e210a0c

Please sign in to comment.