Skip to content

Commit 4cf8928

Browse files
committed
[Refactor] check existence of node_modules
This offers a small but useful performance improvement by avoiding unnecessary stat calls.
1 parent 3971931 commit 4cf8928

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

lib/async.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,14 @@ module.exports = function resolve(x, options, callback) {
233233
if (dirs.length === 0) return cb(null, undefined);
234234
var dir = dirs[0];
235235

236-
var file = path.join(dir, x);
237-
loadAsFile(file, opts.package, onfile);
236+
isDirectory(dir, isdir);
237+
238+
function isdir(err, isdir) {
239+
if (err) return cb(err);
240+
if (!isdir) return processDirs(cb, dirs.slice(1));
241+
var file = path.join(dir, x);
242+
loadAsFile(file, opts.package, onfile);
243+
}
238244

239245
function onfile(err, m, pkg) {
240246
if (err) return cb(err);

test/mock.js

+2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ test('mock package', function (t) {
108108

109109
var dirs = {};
110110
dirs[path.resolve('/foo')] = true;
111+
dirs[path.resolve('/foo/node_modules')] = true;
111112

112113
function opts(basedir) {
113114
return {
@@ -142,6 +143,7 @@ test('mock package from package', function (t) {
142143

143144
var dirs = {};
144145
dirs[path.resolve('/foo')] = true;
146+
dirs[path.resolve('/foo/node_modules')] = true;
145147

146148
function opts(basedir) {
147149
return {

0 commit comments

Comments
 (0)