Skip to content

Commit d5dc919

Browse files
Maël Nisonljharb
Maël Nison
authored andcommitted
[New] async/sync/node-modules-paths: Adds support for “paths” being a function
1 parent 3170ce1 commit d5dc919

File tree

5 files changed

+35
-6
lines changed

5 files changed

+35
-6
lines changed

lib/async.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,6 @@ module.exports = function resolve(x, options, callback) {
228228
}
229229
}
230230
function loadNodeModules(x, start, cb) {
231-
processDirs(cb, nodeModulesPaths(start, opts));
231+
processDirs(cb, nodeModulesPaths(start, opts, x));
232232
}
233233
};

lib/node-modules-paths.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var path = require('path');
22
var fs = require('fs');
33
var parse = path.parse || require('path-parse');
44

5-
module.exports = function nodeModulesPaths(start, opts) {
5+
module.exports = function nodeModulesPaths(start, opts, request) {
66
var modules = opts && opts.moduleDirectory
77
? [].concat(opts.moduleDirectory)
88
: ['node_modules'];
@@ -40,5 +40,11 @@ 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+
return dirs;
45+
}
46+
if (typeof opts.paths === 'function') {
47+
return dirs.concat(opts.paths(request, absoluteStart, opts));
48+
}
49+
return dirs.concat(opts.paths);
4450
};

lib/sync.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ module.exports = function (x, options) {
139139
}
140140

141141
function loadNodeModulesSync(x, start) {
142-
var dirs = nodeModulesPaths(start, opts);
142+
var dirs = nodeModulesPaths(start, opts, x);
143143
for (var i = 0; i < dirs.length; i++) {
144144
var dir = dirs[i];
145145
var m = loadAsFileSync(path.join(dir, '/', x));

readme.markdown

+5
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(request, start, opts)` function
78+
* request - the import specifier being resolved
79+
* start - lookup path
80+
* opts - the resolution options
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

+20-2
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,19 @@ 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(request, absoluteStart, opts) {
69+
return [path.join(absoluteStart, 'not node modules', request)];
70+
};
71+
72+
var start = path.join(__dirname, 'resolver');
73+
var dirs = nodeModulesPaths(start, { paths: paths }, 'pkg');
74+
75+
verifyDirs(t, start, dirs, null, [path.join(start, 'not node modules', 'pkg')]);
76+
77+
t.end();
78+
});
79+
6280
t.test('with moduleDirectory option', function (t) {
6381
var start = path.join(__dirname, 'resolver');
6482
var moduleDirectory = 'not node modules';

0 commit comments

Comments
 (0)