Skip to content

Commit 37b7b5e

Browse files
module: Change precedence of _-prefixed modules
That way stat() only needs to be called once in most cases (assuming non _-prefixed symlinks are more common).
1 parent 6564aae commit 37b7b5e

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

lib/module.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ Module._findPath = function(request, paths, isMain) {
198198
const _basePath = path.resolve(curPath, '_' + request);
199199
const basePath = path.resolve(curPath, request);
200200

201-
const filename = tryFindPath(_basePath, exts, true, isAbsolute) ||
202-
tryFindPath(basePath, exts, isMain, isAbsolute);
201+
const filename = tryFindPath(basePath, exts, isMain, isAbsolute) ||
202+
tryFindPath(_basePath, exts, true, isAbsolute);
203203

204204
if (filename) {
205205
// Warn once if '.' resolved outside the module dir

test/fixtures/module-require-real-path/node_modules/real-module/index.js renamed to test/fixtures/module-require-real-path/node_modules/link-module/index.js

File renamed without changes.

test/parallel/test-require-real-path.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ const fs = require('fs');
77
const realModuleSymlinkPath = path.join(common.fixturesDir,
88
'/module-require-real-path/node_modules/_real-module');
99

10+
const linkModuleSymlinkPath = path.join(common.fixturesDir,
11+
'/module-require-real-path/node_modules/_link-module');
12+
1013
const localRealModulePath = path.join(common.fixturesDir,
1114
'/module-require-real-path/real-module');
1215

@@ -18,13 +21,15 @@ const _require = require(
1821

1922
process.on('exit', function() {
2023
fs.unlinkSync(realModuleSymlinkPath);
24+
fs.unlinkSync(linkModuleSymlinkPath);
2125
});
2226

2327
fs.symlinkSync('../real-module', realModuleSymlinkPath);
28+
fs.symlinkSync('../real-module', linkModuleSymlinkPath);
2429

2530
assert.equal(_require('./real-module'), _require('real-module'));
2631
assert.equal(_require('real-module').dirname, localRealModulePath);
2732

2833
// When required directly with the _-prefix, resolve to path of symlink.
29-
assert.notEqual(_require('./real-module'), _require('_real-module'));
30-
assert.equal(_require('_real-module').dirname, realModuleSymlinkPath);
34+
assert.notEqual(_require('./real-module'), _require('_link-module'));
35+
assert.equal(_require('_link-module').dirname, linkModuleSymlinkPath);

0 commit comments

Comments
 (0)