mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
module: check file ext before dir as documented
The documented resolution algorithm started to search for package.json files prior to searching for file extensions when searching for a specifier. Oddly, it did not search for index files at same time it searched for package.json. This restores the documented behavior of searching for file extensions prior to searching directories. PR-URL: https://github.com/nodejs/node/pull/15015 Fixes: https://github.com/nodejs/node/issues/14990 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Khaidi Chu <i@2333.moe>
This commit is contained in:
parent
8b69d4aca9
commit
1ed36aeb53
@ -219,6 +219,9 @@ Module._findPath = function(request, paths, isMain) {
|
||||
var exts;
|
||||
var trailingSlash = request.length > 0 &&
|
||||
request.charCodeAt(request.length - 1) === CHAR_FORWARD_SLASH;
|
||||
if (!trailingSlash) {
|
||||
trailingSlash = /(?:^|\/)\.?\.$/.test(request);
|
||||
}
|
||||
|
||||
// For each path
|
||||
for (var i = 0; i < paths.length; i++) {
|
||||
@ -236,10 +239,6 @@ Module._findPath = function(request, paths, isMain) {
|
||||
} else {
|
||||
filename = toRealPath(basePath);
|
||||
}
|
||||
} else if (rc === 1) { // Directory.
|
||||
if (exts === undefined)
|
||||
exts = Object.keys(Module._extensions);
|
||||
filename = tryPackage(basePath, exts, isMain);
|
||||
}
|
||||
|
||||
if (!filename) {
|
||||
@ -251,11 +250,13 @@ Module._findPath = function(request, paths, isMain) {
|
||||
}
|
||||
|
||||
if (!filename && rc === 1) { // Directory.
|
||||
// try it with each of the extensions at "index"
|
||||
if (exts === undefined)
|
||||
exts = Object.keys(Module._extensions);
|
||||
filename = tryPackage(basePath, exts, isMain) ||
|
||||
// try it with each of the extensions at "index"
|
||||
tryExtensions(path.resolve(basePath, 'index'), exts, isMain);
|
||||
filename = tryPackage(basePath, exts, isMain);
|
||||
if (!filename) {
|
||||
filename = tryExtensions(path.resolve(basePath, 'index'), exts, isMain);
|
||||
}
|
||||
}
|
||||
|
||||
if (filename) {
|
||||
|
1
test/fixtures/module-extension-over-directory/inner.js
vendored
Normal file
1
test/fixtures/module-extension-over-directory/inner.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
module.exports = {};
|
3
test/fixtures/module-extension-over-directory/inner/package.json
vendored
Normal file
3
test/fixtures/module-extension-over-directory/inner/package.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"main": "./package.json"
|
||||
}
|
30
test/parallel/test-require-extension-over-directory.js
Normal file
30
test/parallel/test-require-extension-over-directory.js
Normal file
@ -0,0 +1,30 @@
|
||||
'use strict';
|
||||
// fixes regression from v4
|
||||
require('../common');
|
||||
const assert = require('assert');
|
||||
const fixtures = require('../common/fixtures');
|
||||
const path = require('path');
|
||||
|
||||
const fixturesRequire = require(
|
||||
fixtures.path('module-extension-over-directory', 'inner'));
|
||||
|
||||
assert.strictEqual(
|
||||
fixturesRequire,
|
||||
require(fixtures.path('module-extension-over-directory', 'inner.js')),
|
||||
'test-require-extension-over-directory failed to import fixture' +
|
||||
' requirements'
|
||||
);
|
||||
|
||||
const fakePath = [
|
||||
fixtures.path('module-extension-over-directory', 'inner'),
|
||||
'fake',
|
||||
'..'
|
||||
].join(path.sep);
|
||||
const fixturesRequireDir = require(fakePath);
|
||||
|
||||
assert.strictEqual(
|
||||
fixturesRequireDir,
|
||||
require(fixtures.path('module-extension-over-directory', 'inner/')),
|
||||
'test-require-extension-over-directory failed to import fixture' +
|
||||
' requirements'
|
||||
);
|
Loading…
Reference in New Issue
Block a user