resolve
Advanced tools
Comparing version 1.10.1 to 1.11.0
@@ -5,4 +5,8 @@ ### Changelog | ||
#### [Unreleased](https://github.com/browserify/resolve/compare/v1.10.0...HEAD) | ||
#### [Unreleased](https://github.com/browserify/resolve/compare/v1.11.0...HEAD) | ||
#### [v1.11.0](https://github.com/browserify/resolve/compare/v1.10.1...v1.11.0) - 15 May 2019 | ||
- [New] Add `isDirectory`; use to speed up `node_modules` lookups (#192, #191, #190) | ||
- [Tests] up to `node` `v12.2`, `v11.15`, `v6.17` | ||
#### [v1.10.1](https://github.com/browserify/resolve/compare/v1.10.0...v1.10.1) - 23 April 2019 | ||
@@ -9,0 +13,0 @@ - [Fix] `core`: 6 `v8/` core modules and 3 `node-inspect/` core modules no longer exist in node 12 ([`b54d33a`][]) |
@@ -18,2 +18,12 @@ var core = require('./core'); | ||
var defaultIsDir = function isDirectory(dir, cb) { | ||
fs.stat(dir, function (err, stat) { | ||
if (!err) { | ||
return cb(null, stat.isDirectory()); | ||
} | ||
if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false); | ||
return cb(err); | ||
}); | ||
}; | ||
module.exports = function resolve(x, options, callback) { | ||
@@ -36,2 +46,3 @@ var cb = callback; | ||
var isFile = opts.isFile || defaultIsFile; | ||
var isDirectory = opts.isDirectory || defaultIsDir; | ||
var readFile = opts.readFile || fs.readFile; | ||
@@ -213,5 +224,11 @@ | ||
var file = path.join(dir, x); | ||
loadAsFile(file, opts.package, onfile); | ||
isDirectory(dir, isdir); | ||
function isdir(err, isdir) { | ||
if (err) return cb(err); | ||
if (!isdir) return processDirs(cb, dirs.slice(1)); | ||
var file = path.join(dir, x); | ||
loadAsFile(file, opts.package, onfile); | ||
} | ||
function onfile(err, m, pkg) { | ||
@@ -218,0 +235,0 @@ if (err) return cb(err); |
@@ -18,2 +18,12 @@ var core = require('./core'); | ||
var defaultIsDir = function isDirectory(dir) { | ||
try { | ||
var stat = fs.statSync(dir); | ||
} catch (e) { | ||
if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false; | ||
throw e; | ||
} | ||
return stat.isDirectory(); | ||
}; | ||
module.exports = function (x, options) { | ||
@@ -27,2 +37,3 @@ if (typeof x !== 'string') { | ||
var readFileSync = opts.readFileSync || fs.readFileSync; | ||
var isDirectory = opts.isDirectory || defaultIsDir; | ||
@@ -150,8 +161,10 @@ var extensions = opts.extensions || ['.js']; | ||
var dir = dirs[i]; | ||
var m = loadAsFileSync(path.join(dir, '/', x)); | ||
if (m) return m; | ||
var n = loadAsDirectorySync(path.join(dir, '/', x)); | ||
if (n) return n; | ||
if (isDirectory(dir)) { | ||
var m = loadAsFileSync(path.join(dir, '/', x)); | ||
if (m) return m; | ||
var n = loadAsDirectorySync(path.join(dir, '/', x)); | ||
if (n) return n; | ||
} | ||
} | ||
} | ||
}; |
{ | ||
"name": "resolve", | ||
"description": "resolve like require.resolve() on behalf of files asynchronously and synchronously", | ||
"version": "1.10.1", | ||
"version": "1.11.0", | ||
"repository": { | ||
@@ -6,0 +6,0 @@ "type": "git", |
@@ -11,2 +11,5 @@ var path = require('path'); | ||
var dirs = {}; | ||
dirs[path.resolve('/foo/bar')] = true; | ||
function opts(basedir) { | ||
@@ -16,6 +19,9 @@ return { | ||
isFile: function (file) { | ||
return Object.prototype.hasOwnProperty.call(files, file); | ||
return Object.prototype.hasOwnProperty.call(files, path.resolve(file)); | ||
}, | ||
isDirectory: function (dir) { | ||
return !!dirs[path.resolve(dir)]; | ||
}, | ||
readFileSync: function (file) { | ||
return files[file]; | ||
return files[path.resolve(file)]; | ||
} | ||
@@ -53,2 +59,6 @@ }; | ||
var dirs = {}; | ||
dirs[path.resolve('/foo')] = true; | ||
dirs[path.resolve('/foo/node_modules')] = true; | ||
function opts(basedir) { | ||
@@ -58,6 +68,9 @@ return { | ||
isFile: function (file) { | ||
return Object.prototype.hasOwnProperty.call(files, file); | ||
return Object.prototype.hasOwnProperty.call(files, path.resolve(file)); | ||
}, | ||
isDirectory: function (dir) { | ||
return !!dirs[path.resolve(dir)]; | ||
}, | ||
readFileSync: function (file) { | ||
return files[file]; | ||
return files[path.resolve(file)]; | ||
} | ||
@@ -64,0 +77,0 @@ }; |
@@ -11,2 +11,5 @@ var path = require('path'); | ||
var dirs = {}; | ||
dirs[path.resolve('/foo/bar')] = true; | ||
function opts(basedir) { | ||
@@ -18,2 +21,5 @@ return { | ||
}, | ||
isDirectory: function (dir, cb) { | ||
cb(null, !!dirs[path.resolve(dir)]); | ||
}, | ||
readFile: function (file, cb) { | ||
@@ -54,2 +60,5 @@ cb(null, files[path.resolve(file)]); | ||
var dirs = {}; | ||
dirs[path.resolve('/foo/bar')] = true; | ||
function opts(basedir) { | ||
@@ -61,2 +70,5 @@ return { | ||
}, | ||
isDirectory: function (dir, cb) { | ||
cb(null, !!dirs[path.resolve(dir)]); | ||
}, | ||
'package': { main: 'bar' }, | ||
@@ -101,2 +113,6 @@ readFile: function (file, cb) { | ||
var dirs = {}; | ||
dirs[path.resolve('/foo')] = true; | ||
dirs[path.resolve('/foo/node_modules')] = true; | ||
function opts(basedir) { | ||
@@ -108,2 +124,5 @@ return { | ||
}, | ||
isDirectory: function (dir, cb) { | ||
cb(null, !!dirs[path.resolve(dir)]); | ||
}, | ||
readFile: function (file, cb) { | ||
@@ -131,2 +150,6 @@ cb(null, files[path.resolve(file)]); | ||
var dirs = {}; | ||
dirs[path.resolve('/foo')] = true; | ||
dirs[path.resolve('/foo/node_modules')] = true; | ||
function opts(basedir) { | ||
@@ -138,2 +161,5 @@ return { | ||
}, | ||
isDirectory: function (dir, cb) { | ||
cb(null, !!dirs[path.resolve(dir)]); | ||
}, | ||
'package': { main: 'bar' }, | ||
@@ -140,0 +166,0 @@ readFile: function (file, cb) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
140059
1980
202