From 59c4cd8dd0446d32a18293e61aefd3be6fd8d7b3 Mon Sep 17 00:00:00 2001 From: Josh Duff Date: Thu, 4 Jan 2018 13:15:48 -0600 Subject: [PATCH 1/4] Update default extensions to match node Extension list sourced from https://nodejs.org/api/modules.html#modules_file_modules Fixes #137 --- lib/async.js | 2 +- lib/sync.js | 2 +- readme.markdown | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/async.js b/lib/async.js index ef1bde78..2785f797 100644 --- a/lib/async.js +++ b/lib/async.js @@ -29,7 +29,7 @@ module.exports = function resolve(x, options, callback) { }; var readFile = opts.readFile || fs.readFile; - var extensions = opts.extensions || ['.js']; + var extensions = opts.extensions || ['.js', '.json', '.node']; var y = opts.basedir || path.dirname(caller()); opts.paths = opts.paths || []; diff --git a/lib/sync.js b/lib/sync.js index bc9e287a..3f9bf5dc 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -20,7 +20,7 @@ module.exports = function (x, options) { }; var readFileSync = opts.readFileSync || fs.readFileSync; - var extensions = opts.extensions || ['.js']; + var extensions = opts.extensions || ['.js', '.json', '.node']; var y = opts.basedir || path.dirname(caller()); opts.paths = opts.paths || []; diff --git a/readme.markdown b/readme.markdown index 6000624a..8fcd3c7a 100644 --- a/readme.markdown +++ b/readme.markdown @@ -84,7 +84,7 @@ default `opts` values: { paths: [], basedir: __dirname, - extensions: [ '.js' ], + extensions: [ '.js', '.json', '.node' ], readFile: fs.readFile, isFile: function (file, cb) { fs.stat(file, function (err, stat) { @@ -132,7 +132,7 @@ default `opts` values: { paths: [], basedir: __dirname, - extensions: [ '.js' ], + extensions: [ '.js', '.json', '.node' ], readFileSync: fs.readFileSync, isFile: function (file) { try { return fs.statSync(file).isFile() } From a578a0fe0cffd9fa0c02ac364b1cae56ea6ede87 Mon Sep 17 00:00:00 2001 From: Josh Duff Date: Thu, 4 Jan 2018 13:15:48 -0600 Subject: [PATCH 2/4] Update default extensions to match node Extension list sourced from https://nodejs.org/api/modules.html#modules_file_modules Fixes #137 --- lib/async.js | 2 +- lib/sync.js | 2 +- readme.markdown | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/async.js b/lib/async.js index dd889dc7..12bb6ed5 100644 --- a/lib/async.js +++ b/lib/async.js @@ -31,7 +31,7 @@ module.exports = function resolve(x, options, callback) { var isFile = opts.isFile || defaultIsFile; var readFile = opts.readFile || fs.readFile; - var extensions = opts.extensions || ['.js']; + var extensions = opts.extensions || ['.js', '.json', '.node']; var basedir = opts.basedir || path.dirname(caller()); opts.paths = opts.paths || []; diff --git a/lib/sync.js b/lib/sync.js index 2e0d5f24..a191c856 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -22,7 +22,7 @@ module.exports = function (x, options) { var isFile = opts.isFile || defaultIsFile; var readFileSync = opts.readFileSync || fs.readFileSync; - var extensions = opts.extensions || ['.js']; + var extensions = opts.extensions || ['.js', '.json', '.node']; var basedir = opts.basedir || path.dirname(caller()); opts.paths = opts.paths || []; diff --git a/readme.markdown b/readme.markdown index b59f2ab9..8c843988 100644 --- a/readme.markdown +++ b/readme.markdown @@ -82,7 +82,7 @@ default `opts` values: { paths: [], basedir: __dirname, - extensions: [ '.js' ], + extensions: [ '.js', '.json', '.node' ], readFile: fs.readFile, isFile: function isFile(file, cb) { fs.stat(file, function (err, stat) { @@ -136,7 +136,7 @@ default `opts` values: { paths: [], basedir: __dirname, - extensions: [ '.js' ], + extensions: [ '.js', '.json', '.node' ], readFileSync: fs.readFileSync, isFile: function isFile(file) { try { From 410df04fb7e7cfbb5119eac11a33d07deb3cf515 Mon Sep 17 00:00:00 2001 From: Josh Duff Date: Tue, 11 Sep 2018 17:55:07 -0500 Subject: [PATCH 3/4] Tests for resolving json/node extensions by default --- test/resolver.js | 15 +++++++++++++++ test/resolver/foo-json.json | 1 + test/resolver/foo-node.node | 1 + 3 files changed, 17 insertions(+) create mode 100644 test/resolver/foo-json.json create mode 100644 test/resolver/foo-node.node diff --git a/test/resolver.js b/test/resolver.js index 56641dfd..2a46afde 100644 --- a/test/resolver.js +++ b/test/resolver.js @@ -36,6 +36,21 @@ test('async foo', function (t) { }); }); +test('non-default extensions', function (t) { + t.plan(2); + var dir = path.join(__dirname, 'resolver'); + + resolve('./foo-json', { basedir: dir }, function (err, res, pkg) { + if (err) t.fail(err); + t.equal(res, path.join(dir, 'foo-json.json')); + }); + + resolve('./foo-node', { basedir: dir }, function (err, res, pkg) { + if (err) t.fail(err); + t.equal(res, path.join(dir, 'foo-node.node')); + }); +}); + test('bar', function (t) { t.plan(6); var dir = path.join(__dirname, 'resolver'); diff --git a/test/resolver/foo-json.json b/test/resolver/foo-json.json new file mode 100644 index 00000000..d00491fd --- /dev/null +++ b/test/resolver/foo-json.json @@ -0,0 +1 @@ +1 diff --git a/test/resolver/foo-node.node b/test/resolver/foo-node.node new file mode 100644 index 00000000..bd816eab --- /dev/null +++ b/test/resolver/foo-node.node @@ -0,0 +1 @@ +module.exports = 1; From 0b80213dc9cbdc5c563893bb2e300d9f4a3d1e6d Mon Sep 17 00:00:00 2001 From: Josh Duff Date: Tue, 11 Sep 2018 18:04:59 -0500 Subject: [PATCH 4/4] Sync tests for non-default extensions --- test/resolver_sync.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/resolver_sync.js b/test/resolver_sync.js index 88448288..ed4896de 100644 --- a/test/resolver_sync.js +++ b/test/resolver_sync.js @@ -38,6 +38,22 @@ test('foo', function (t) { t.end(); }); +test('non-default extensions', function (t) { + var dir = path.join(__dirname, 'resolver'); + + t.equal( + resolve.sync('./foo-json', { basedir: dir }), + path.join(dir, 'foo-json.json') + ); + + t.equal( + resolve.sync('./foo-node', { basedir: dir }), + path.join(dir, 'foo-node.node') + ); + + t.end(); +}); + test('bar', function (t) { var dir = path.join(__dirname, 'resolver');