Skip to content

Commit adb70ba

Browse files
searlsljharb
authored andcommitted
Fixes #147—Ensures core modules beat shadowed ones
Given 'util' as an example native module which is also installed as an npm package, then in order for browserify/resolve to act consistently with native `require.resolve`, these should be true: ``` resolve.sync('util') // returns 'util' resolve.sync('util/') // returns 'node_modules/util/index.js' ```
1 parent 5686c44 commit adb70ba

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

lib/async.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ module.exports = function resolve(x, options, callback) {
4444
} else loadAsFile(res, opts.package, onfile);
4545
} else loadNodeModules(x, basedir, function (err, n, pkg) {
4646
if (err) cb(err);
47-
else if (n) cb(null, n, pkg);
4847
else if (core[x]) return cb(null, x);
48+
else if (n) return cb(null, n, pkg);
4949
else {
5050
var moduleError = new Error("Cannot find module '" + x + "' from '" + basedir + "'");
5151
moduleError.code = 'MODULE_NOT_FOUND';

lib/sync.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ module.exports = function (x, options) {
3232
if (x === '..' || x.slice(-1) === '/') res += '/';
3333
var m = loadAsFileSync(res) || loadAsDirectorySync(res);
3434
if (m) return m;
35+
} else if (core[x]) {
36+
return x;
3537
} else {
3638
var n = loadNodeModulesSync(x, basedir);
3739
if (n) return n;

test/shadowed_core.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ test('shadowed core modules still return core module', function (t) {
1111
});
1212
});
1313

14+
test('shadowed core modules still return core module [sync]', function (t) {
15+
t.plan(1);
16+
17+
var res = resolve.sync('util', { basedir: path.join(__dirname, 'shadowed_core') });
18+
19+
t.equal(res, 'util');
20+
});
21+
1422
test('shadowed core modules return shadow when appending `/`', function (t) {
1523
t.plan(2);
1624

@@ -19,3 +27,12 @@ test('shadowed core modules return shadow when appending `/`', function (t) {
1927
t.equal(res, path.join(__dirname, 'shadowed_core/node_modules/util/index.js'));
2028
});
2129
});
30+
31+
test('shadowed core modules return shadow when appending `/` [sync]', function (t) {
32+
t.plan(1);
33+
34+
var res = resolve.sync('util/', { basedir: path.join(__dirname, 'shadowed_core') });
35+
36+
t.equal(res, path.join(__dirname, 'shadowed_core/node_modules/util/index.js'));
37+
});
38+

0 commit comments

Comments
 (0)