Skip to content

Implements skipNodeModules #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 30 additions & 24 deletions lib/node-modules-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,7 @@ var path = require('path');
var fs = require('fs');
var parse = path.parse || require('path-parse');

module.exports = function nodeModulesPaths(start, opts, request) {
var modules = opts && opts.moduleDirectory
? [].concat(opts.moduleDirectory)
: ['node_modules'];

// ensure that `start` is an absolute path at this point, resolving against the process' current working directory
var absoluteStart = path.resolve(start);

if (!opts || !opts.preserveSymlinks) {
try {
absoluteStart = fs.realpathSync(absoluteStart);
} catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
}
}

var getNodeModulesDirs = function getNodeModulesDirs(absoluteStart, modules) {
var prefix = '/';
if ((/^([A-Za-z]:)/).test(absoluteStart)) {
prefix = '';
Expand All @@ -34,17 +17,40 @@ module.exports = function nodeModulesPaths(start, opts, request) {
parsed = parse(parsed.dir);
}

var dirs = paths.reduce(function (dirs, aPath) {
return paths.reduce(function (dirs, aPath) {
return dirs.concat(modules.map(function (moduleDir) {
return path.join(prefix, aPath, moduleDir);
}));
}, []);
};

if (!opts || !opts.paths) {
return dirs;
module.exports = function nodeModulesPaths(start, opts, request) {
var modules = opts && opts.moduleDirectory
? [].concat(opts.moduleDirectory)
: ['node_modules'];

// ensure that `start` is an absolute path at this point, resolving against the process' current working directory
var absoluteStart = path.resolve(start);

if (!opts || !opts.preserveSymlinks) {
try {
absoluteStart = fs.realpathSync(absoluteStart);
} catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
}
}
if (typeof opts.paths === 'function') {
return dirs.concat(opts.paths(request, absoluteStart, opts));

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

var dirs = getNodeModulesDirs(absoluteStart, modules);
return opts && opts.paths ? dirs.concat(opts.paths) : dirs;
};
1 change: 1 addition & 0 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ options are:
For advanced users, `paths` can also be a `opts.paths(request, start, opts)` function
* request - the import specifier being resolved
* start - lookup path
* getNodeModulesDirs - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution
* opts - the resolution options

* opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"`
Expand Down
14 changes: 12 additions & 2 deletions test/node-modules-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ test('node-modules-paths', function (t) {
});

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

var start = path.join(__dirname, 'resolver');
Expand All @@ -77,6 +77,16 @@ test('node-modules-paths', function (t) {
t.end();
});

t.test('with paths=function skipping node modules resolution', function (t) {
var paths = function paths(request, absoluteStart, getNodeModulesDirs, opts) {
return [];
};
var start = path.join(__dirname, 'resolver');
var dirs = nodeModulesPaths(start, { paths: paths });
t.deepEqual(dirs, [], 'no node_modules was computed');
t.end();
});

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