Skip to content

Commit

Permalink
[New] sync/async: add 'includeCoreModules' option
Browse files Browse the repository at this point in the history
  • Loading branch information
tjenkinson authored and ljharb committed Nov 8, 2020
1 parent 881a0bf commit 7f5a63b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/async.js
Expand Up @@ -73,6 +73,7 @@ module.exports = function resolve(x, options, callback) {
var packageIterator = opts.packageIterator;

var extensions = opts.extensions || ['.js'];
var includeCoreModules = opts.includeCoreModules !== false;
var basedir = opts.basedir || path.dirname(caller());
var parent = opts.filename || basedir;

Expand Down Expand Up @@ -113,7 +114,7 @@ module.exports = function resolve(x, options, callback) {
if ((/\/$/).test(x) && res === basedir) {
loadAsDirectory(res, opts.package, onfile);
} else loadAsFile(res, opts.package, onfile);
} else if (isCore(x)) {
} else if (includeCoreModules && isCore(x)) {
return cb(null, x);
} else loadNodeModules(x, basedir, function (err, n, pkg) {
if (err) cb(err);
Expand Down
3 changes: 2 additions & 1 deletion lib/sync.js
Expand Up @@ -66,6 +66,7 @@ module.exports = function resolveSync(x, options) {
var packageIterator = opts.packageIterator;

var extensions = opts.extensions || ['.js'];
var includeCoreModules = opts.includeCoreModules !== false;
var basedir = opts.basedir || path.dirname(caller());
var parent = opts.filename || basedir;

Expand All @@ -85,7 +86,7 @@ module.exports = function resolveSync(x, options) {
if (x === '.' || x === '..' || x.slice(-1) === '/') res += '/';
var m = loadAsFileSync(res) || loadAsDirectorySync(res);
if (m) return maybeRealpathSync(realpathSync, m, opts);
} else if (isCore(x)) {
} else if (includeCoreModules && isCore(x)) {
return x;
} else {
var n = loadNodeModulesSync(x, absoluteStart);
Expand Down
6 changes: 6 additions & 0 deletions readme.markdown
Expand Up @@ -61,6 +61,8 @@ options are:

* opts.extensions - array of file extensions to search in order

* opts.includeCoreModules - set to `false` to exclude node core modules (e.g. `fs`) from the search

* opts.readFile - how to read files asynchronously

* opts.isFile - function to asynchronously test whether a file exists
Expand Down Expand Up @@ -108,6 +110,7 @@ default `opts` values:
paths: [],
basedir: __dirname,
extensions: ['.js'],
includeCoreModules: true,
readFile: fs.readFile,
isFile: function isFile(file, cb) {
fs.stat(file, function (err, stat) {
Expand Down Expand Up @@ -150,6 +153,8 @@ options are:

* opts.extensions - array of file extensions to search in order

* opts.includeCoreModules - set to `false` to exclude node core modules (e.g. `fs`) from the search

* opts.readFile - how to read files synchronously

* opts.isFile - function to synchronously test whether a file exists
Expand Down Expand Up @@ -195,6 +200,7 @@ default `opts` values:
paths: [],
basedir: __dirname,
extensions: ['.js'],
includeCoreModules: true,
readFileSync: fs.readFileSync,
isFile: function isFile(file) {
try {
Expand Down
16 changes: 16 additions & 0 deletions test/shadowed_core.js
Expand Up @@ -36,3 +36,19 @@ test('shadowed core modules return shadow when appending `/` [sync]', function (
t.equal(res, path.join(__dirname, 'shadowed_core/node_modules/util/index.js'));
});

test('shadowed core modules return shadow with `includeCoreModules: false`', function (t) {
t.plan(2);

resolve('util', { basedir: path.join(__dirname, 'shadowed_core'), includeCoreModules: false }, function (err, res) {
t.ifError(err);
t.equal(res, path.join(__dirname, 'shadowed_core/node_modules/util/index.js'));
});
});

test('shadowed core modules return shadow with `includeCoreModules: false` [sync]', function (t) {
t.plan(1);

var res = resolve.sync('util', { basedir: path.join(__dirname, 'shadowed_core'), includeCoreModules: false });

t.equal(res, path.join(__dirname, 'shadowed_core/node_modules/util/index.js'));
});

0 comments on commit 7f5a63b

Please sign in to comment.