Skip to content

Commit fcd65ba

Browse files
author
Maël Nison
committed
Adds support for "paths" being a function
1 parent 0a8fc9f commit fcd65ba

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

lib/async.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ module.exports = function resolve(x, options, callback) {
225225
}
226226
}
227227
function loadNodeModules(x, start, cb) {
228+
opts.request = x;
228229
processDirs(cb, nodeModulesPaths(start, opts));
229230
}
230231
};

lib/node-modules-paths.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,13 @@ module.exports = function nodeModulesPaths(start, opts) {
4040
}));
4141
}, []);
4242

43-
return opts && opts.paths ? dirs.concat(opts.paths) : dirs;
43+
if (opts && opts.paths) {
44+
if (typeof opts.paths === 'function') {
45+
dirs = dirs.concat(opts.paths(absoluteStart, opts));
46+
} else {
47+
dirs = dirs.concat(opts.paths);
48+
}
49+
}
50+
51+
return dirs;
4452
};

lib/sync.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ module.exports = function (x, options) {
137137
}
138138

139139
function loadNodeModulesSync(x, start) {
140+
opts.request = x;
140141
var dirs = nodeModulesPaths(start, opts);
141142
for (var i = 0; i < dirs.length; i++) {
142143
var dir = dirs[i];

readme.markdown

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ options are:
7474

7575
* opts.paths - require.paths array to use if nothing is found on the normal `node_modules` recursive walk (probably don't use this)
7676

77+
For advanced users, `paths` can also be a `opts.paths(start, opts)` function
78+
* start - lookup path
79+
* opts - the resolution options
80+
* opts.request - the import specifier being resolved
81+
7782
* opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"`
7883

7984
* opts.preserveSymlinks - if true, doesn't resolve `basedir` to real path before resolving.

test/node-modules-paths.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ var nodeModulesPaths = require('../lib/node-modules-paths');
77

88
var verifyDirs = function verifyDirs(t, start, dirs, moduleDirectories, paths) {
99
var moduleDirs = [].concat(moduleDirectories || 'node_modules');
10+
if (paths) {
11+
for (var k = 0; k < paths.length; ++k) {
12+
moduleDirs.push(path.basename(paths[k]));
13+
}
14+
}
1015

1116
var foundModuleDirs = {};
1217
var uniqueDirs = {};
@@ -20,7 +25,7 @@ var verifyDirs = function verifyDirs(t, start, dirs, moduleDirectories, paths) {
2025
}
2126
t.equal(keys(parsedDirs).length >= start.split(path.sep).length, true, 'there are >= dirs than "start" has');
2227
var foundModuleDirNames = keys(foundModuleDirs);
23-
t.deepEqual(foundModuleDirNames, moduleDirs.concat(paths || []), 'all desired module dirs were found');
28+
t.deepEqual(foundModuleDirNames, moduleDirs, 'all desired module dirs were found');
2429
t.equal(keys(uniqueDirs).length, dirs.length, 'all dirs provided were unique');
2530

2631
var counts = {};
@@ -49,7 +54,7 @@ test('node-modules-paths', function (t) {
4954
t.end();
5055
});
5156

52-
t.test('with paths option', function (t) {
57+
t.test('with paths=array option', function (t) {
5358
var start = path.join(__dirname, 'resolver');
5459
var paths = ['a', 'b'];
5560
var dirs = nodeModulesPaths(start, { paths: paths });
@@ -59,6 +64,16 @@ test('node-modules-paths', function (t) {
5964
t.end();
6065
});
6166

67+
t.test('with paths=function option', function (t) {
68+
var paths = function paths(absoluteStart, opts) {
69+
return [path.join(absoluteStart, 'not node modules', opts.request)];
70+
};
71+
var start = path.join(__dirname, 'resolver');
72+
var dirs = nodeModulesPaths(start, { paths: paths, request: 'pkg' });
73+
verifyDirs(t, start, dirs, null, [path.join(start, 'not node modules', 'pkg')]);
74+
t.end();
75+
});
76+
6277
t.test('with moduleDirectory option', function (t) {
6378
var start = path.join(__dirname, 'resolver');
6479
var moduleDirectory = 'not node modules';

0 commit comments

Comments
 (0)