Skip to content

Commit

Permalink
Merge pull request #148 from searls/147-shadowed-core-dependencies
Browse files Browse the repository at this point in the history
Make behavior consistent with require.resolve() for "shadowed" core modules
  • Loading branch information
ljharb authored Apr 8, 2018
2 parents 75108bc + 9e3104b commit 197288e
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ module.exports = function resolve(x, options, callback) {
} else loadAsFile(res, opts.package, onfile);
} else loadNodeModules(x, basedir, function (err, n, pkg) {
if (err) cb(err);
else if (n) cb(null, n, pkg);
else if (core[x]) return cb(null, x);
else if (n) return cb(null, n, pkg);
else {
var moduleError = new Error("Cannot find module '" + x + "' from '" + basedir + "'");
moduleError.code = 'MODULE_NOT_FOUND';
Expand Down
2 changes: 2 additions & 0 deletions lib/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ module.exports = function (x, options) {
if (x === '..' || x.slice(-1) === '/') res += '/';
var m = loadAsFileSync(res) || loadAsDirectorySync(res);
if (m) return m;
} else if (core[x]) {
return x;
} else {
var n = loadNodeModulesSync(x, basedir);
if (n) return n;
Expand Down
11 changes: 0 additions & 11 deletions test/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,17 +268,6 @@ test('without basedir', function (t) {
});
});

test('#25: node modules with the same name as node stdlib modules', function (t) {
t.plan(1);

var resolverDir = path.join(__dirname, 'resolver/punycode');

resolve('punycode', { basedir: resolverDir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(resolverDir, 'node_modules/punycode/index.js'));
});
});

test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) {
t.plan(2);

Expand Down
11 changes: 0 additions & 11 deletions test/resolver_sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,6 @@ test('incorrect main', function (t) {
t.end();
});

test('#25: node modules with the same name as node stdlib modules', function (t) {
var resolverDir = path.join(__dirname, 'resolver/punycode');

t.equal(
resolve.sync('punycode', { basedir: resolverDir }),
path.join(resolverDir, 'node_modules/punycode/index.js')
);

t.end();
});

var stubStatSync = function stubStatSync(fn) {
var fs = require('fs');
var statSync = fs.statSync;
Expand Down
38 changes: 38 additions & 0 deletions test/shadowed_core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var test = require('tape');
var resolve = require('../');
var path = require('path');

test('shadowed core modules still return core module', function (t) {
t.plan(2);

resolve('util', { basedir: path.join(__dirname, 'shadowed_core') }, function (err, res) {
t.ifError(err);
t.equal(res, 'util');
});
});

test('shadowed core modules still return core module [sync]', function (t) {
t.plan(1);

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

t.equal(res, 'util');
});

test('shadowed core modules return shadow when appending `/`', function (t) {
t.plan(2);

resolve('util/', { basedir: path.join(__dirname, 'shadowed_core') }, 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 when appending `/` [sync]', function (t) {
t.plan(1);

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

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

1 change: 1 addition & 0 deletions test/shadowed_core/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!/node_modules
Empty file.

0 comments on commit 197288e

Please sign in to comment.