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

Resolve JSON files from extensionless parameters #20

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

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

Choose a reason for hiding this comment

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

This is missing .node - instead, could we use the keys in require.extensions, which should ensure that the array always matches the require algorithm?

(if there's a concern that browserify won't be able to handle require.extensions, then let's add .node to the list also)

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';
Copy link
Member

Choose a reason for hiding this comment

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

this should use path.join(__dirname, 'resolver') instead


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