mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
module: replace default paths in require.resolve()
Prior to this commit, the default search paths would be included in the require.resolve() process, even if user specified paths were provided. This commit causes the default paths to be omitted by using a fake parent module. Refs: https://github.com/nodejs/node/issues/5963 PR-URL: https://github.com/nodejs/node/pull/17113 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
4b34e6fef1
commit
8578e81b2b
@ -521,11 +521,14 @@ Module._resolveFilename = function(request, parent, isMain, options) {
|
|||||||
|
|
||||||
if (typeof options === 'object' && options !== null &&
|
if (typeof options === 'object' && options !== null &&
|
||||||
Array.isArray(options.paths)) {
|
Array.isArray(options.paths)) {
|
||||||
|
const fakeParent = new Module('', null);
|
||||||
|
|
||||||
paths = [];
|
paths = [];
|
||||||
|
|
||||||
for (var i = 0; i < options.paths.length; i++) {
|
for (var i = 0; i < options.paths.length; i++) {
|
||||||
const path = options.paths[i];
|
const path = options.paths[i];
|
||||||
const lookupPaths = Module._resolveLookupPaths(path, parent, true);
|
fakeParent.paths = Module._nodeModulePaths(path);
|
||||||
|
const lookupPaths = Module._resolveLookupPaths(request, fakeParent, true);
|
||||||
|
|
||||||
if (!paths.includes(path))
|
if (!paths.includes(path))
|
||||||
paths.push(path);
|
paths.push(path);
|
||||||
|
0
test/fixtures/resolve-paths/default/node_modules/dep/index.js
generated
vendored
Normal file
0
test/fixtures/resolve-paths/default/node_modules/dep/index.js
generated
vendored
Normal file
21
test/fixtures/resolve-paths/default/verify-paths.js
vendored
Normal file
21
test/fixtures/resolve-paths/default/verify-paths.js
vendored
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
'use strict';
|
||||||
|
require('../../../common');
|
||||||
|
const assert = require('assert');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
// By default, resolving 'dep' should return
|
||||||
|
// fixturesDir/resolve-paths/default/node_modules/dep/index.js. By setting
|
||||||
|
// the path to fixturesDir/resolve-paths/default, the 'default' directory
|
||||||
|
// structure should be ignored.
|
||||||
|
|
||||||
|
assert.strictEqual(
|
||||||
|
require.resolve('dep'),
|
||||||
|
path.join(__dirname, 'node_modules', 'dep', 'index.js')
|
||||||
|
);
|
||||||
|
|
||||||
|
const paths = [path.resolve(__dirname, '..', 'defined')];
|
||||||
|
|
||||||
|
assert.strictEqual(
|
||||||
|
require.resolve('dep', { paths }),
|
||||||
|
path.join(paths[0], 'node_modules', 'dep', 'index.js')
|
||||||
|
);
|
0
test/fixtures/resolve-paths/defined/node_modules/dep/index.js
generated
vendored
Normal file
0
test/fixtures/resolve-paths/defined/node_modules/dep/index.js
generated
vendored
Normal file
@ -37,3 +37,4 @@ assert.strictEqual('path', require.resolve('path'));
|
|||||||
|
|
||||||
// Test configurable resolve() paths.
|
// Test configurable resolve() paths.
|
||||||
require(fixtures.path('require-resolve.js'));
|
require(fixtures.path('require-resolve.js'));
|
||||||
|
require(fixtures.path('resolve-paths', 'default', 'verify-paths.js'));
|
||||||
|
Loading…
Reference in New Issue
Block a user