Skip to content

Commit

Permalink
[New] async/sync/node-modules-paths: Adds support for “paths” b…
Browse files Browse the repository at this point in the history
…eing a function
  • Loading branch information
Maël Nison authored and ljharb committed Nov 14, 2018
1 parent 3170ce1 commit 15acb75
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,6 @@ module.exports = function resolve(x, options, callback) {
}
}
function loadNodeModules(x, start, cb) {
processDirs(cb, nodeModulesPaths(start, opts));
processDirs(cb, nodeModulesPaths(start, opts, x));
}
};
12 changes: 10 additions & 2 deletions lib/node-modules-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var path = require('path');
var fs = require('fs');
var parse = path.parse || require('path-parse');

module.exports = function nodeModulesPaths(start, opts) {
module.exports = function nodeModulesPaths(start, opts, request) {
var modules = opts && opts.moduleDirectory
? [].concat(opts.moduleDirectory)
: ['node_modules'];
Expand Down Expand Up @@ -40,5 +40,13 @@ module.exports = function nodeModulesPaths(start, opts) {
}));
}, []);

return opts && opts.paths ? dirs.concat(opts.paths) : dirs;
if (opts && opts.paths) {
if (typeof opts.paths === 'function') {
dirs = dirs.concat(opts.paths(request, absoluteStart, opts));
} else {
dirs = dirs.concat(opts.paths);
}
}

return dirs;
};
2 changes: 1 addition & 1 deletion lib/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ module.exports = function (x, options) {
}

function loadNodeModulesSync(x, start) {
var dirs = nodeModulesPaths(start, opts);
var dirs = nodeModulesPaths(start, opts, x);
for (var i = 0; i < dirs.length; i++) {
var dir = dirs[i];
var m = loadAsFileSync(path.join(dir, '/', x));
Expand Down
5 changes: 5 additions & 0 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ options are:

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

For advanced users, `paths` can also be a `opts.paths(request, start, opts)` function
* request - the import specifier being resolved
* start - lookup path
* opts - the resolution options

* opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"`

* opts.preserveSymlinks - if true, doesn't resolve `basedir` to real path before resolving.
Expand Down
22 changes: 20 additions & 2 deletions test/node-modules-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ var nodeModulesPaths = require('../lib/node-modules-paths');

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

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

var counts = {};
Expand Down Expand Up @@ -49,7 +54,7 @@ test('node-modules-paths', function (t) {
t.end();
});

t.test('with paths option', function (t) {
t.test('with paths=array option', function (t) {
var start = path.join(__dirname, 'resolver');
var paths = ['a', 'b'];
var dirs = nodeModulesPaths(start, { paths: paths });
Expand All @@ -59,6 +64,19 @@ test('node-modules-paths', function (t) {
t.end();
});

t.test('with paths=function option', function (t) {
var paths = function paths(request, absoluteStart, opts) {
return [path.join(absoluteStart, 'not node modules', request)];
};

var start = path.join(__dirname, 'resolver');
var dirs = nodeModulesPaths(start, { paths: paths }, 'pkg');

verifyDirs(t, start, dirs, null, [path.join(start, 'not node modules', 'pkg')]);

t.end();
});

t.test('with moduleDirectory option', function (t) {
var start = path.join(__dirname, 'resolver');
var moduleDirectory = 'not node modules';
Expand Down

0 comments on commit 15acb75

Please sign in to comment.