Skip to content

Commit

Permalink
Resolve JSON files from extensionless parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
SLaks committed May 10, 2013
1 parent 790cdf5 commit bcdb541
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = function (x, opts) {
};
var readFileSync = opts.readFileSync || fs.readFileSync;

var extensions = opts.extensions || [ '.js' ];
var extensions = opts.extensions || [ '.js', '.json' ];

This comment has been minimized.

Copy link
@qfox

qfox Sep 8, 2014

If we really need this then why not to use Object.keys(require.extensions) by default?

var y = opts.basedir
|| path.dirname(require.cache[__filename].parent.filename)
;
Expand Down
19 changes: 19 additions & 0 deletions test/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ test('async foo', function (t) {
});
});

test('async JSON', function (t) {
t.plan(3);
var dir = __dirname + '/resolver';

resolve('./box', { basedir : dir }, function (err, res) {
if (err) t.fail(err);
t.equal(res, dir + '/box.json');
});

resolve('./box.json', { basedir : dir }, function (err, res) {
if (err) t.fail(err);
t.equal(res, dir + '/box.json');
});

resolve('box', { basedir : dir }, function (err) {
t.equal(err.message, "Cannot find module 'box'");
});
});

test('bar', function (t) {
t.plan(2);
var dir = __dirname + '/resolver';
Expand Down
1 change: 1 addition & 0 deletions test/resolver/box.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
20 changes: 20 additions & 0 deletions test/resolver_sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ test('foo', function (t) {
t.end();
});

test('JSON', function (t) {
var dir = __dirname + '/resolver';

t.equal(
resolve.sync('./box', { basedir: dir }),
dir + '/box.json'
);

t.equal(
resolve.sync('./box.json', { basedir: dir }),
dir + '/box.json'
);

t.throws(function () {
resolve.sync('box', { basedir : dir });
});

t.end();
});

test('bar', function (t) {
var dir = __dirname + '/resolver';

Expand Down

0 comments on commit bcdb541

Please sign in to comment.