-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: add global path in require.resolve.paths #13633
Changes from 4 commits
46da204
c96b300
6dc8310
349dd7d
4ae19b9
b010ad2
1c88b5c
c026cd9
d27aa8b
4a9642c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1066,7 +1066,13 @@ export default class Runtime { | |
} else { | ||
// Only include the fromPath if a moduleName is given. Else treat as root. | ||
const fromPath = moduleName ? from : null; | ||
this._execModule(localModule, options, moduleRegistry, fromPath); | ||
this._execModule( | ||
localModule, | ||
options, | ||
moduleRegistry, | ||
fromPath, | ||
moduleName, | ||
); | ||
} | ||
localModule.loaded = true; | ||
} | ||
|
@@ -1398,6 +1404,7 @@ export default class Runtime { | |
} | ||
|
||
private _requireResolvePaths(from: string, moduleName?: string) { | ||
const fromDir = path.resolve(from, '..'); | ||
if (moduleName == null) { | ||
throw new Error( | ||
'The first argument to require.resolve.paths must be a string. Received null or undefined.', | ||
|
@@ -1410,19 +1417,22 @@ export default class Runtime { | |
} | ||
|
||
if (moduleName[0] === '.') { | ||
return [path.resolve(from, '..')]; | ||
return [fromDir]; | ||
} | ||
if (this._resolver.isCoreModule(moduleName)) { | ||
return null; | ||
} | ||
return this._resolver.getModulePaths(path.resolve(from, '..')); | ||
const modulePaths = this._resolver.getModulePaths(fromDir); | ||
const globalPaths = this._resolver.getGlobalPaths(fromDir, moduleName); | ||
return [...modulePaths, ...globalPaths]; | ||
} | ||
|
||
private _execModule( | ||
localModule: InitialModule, | ||
options: InternalModuleOptions | undefined, | ||
moduleRegistry: ModuleRegistry, | ||
from: string | null, | ||
moduleName?: string, | ||
) { | ||
if (this.isTornDown) { | ||
this._logFormattedReferenceError( | ||
|
@@ -1454,8 +1464,10 @@ export default class Runtime { | |
return moduleRegistry.get(key) || null; | ||
}, | ||
}); | ||
|
||
module.paths = this._resolver.getModulePaths(module.path); | ||
module.paths = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While I haven't profiled this and can't know the perf impact for sure, this is a code path that runs very often, so I'd rather be safe and do something like module.paths = this._resolver.getModulePaths(module.path);
if(moduleName) module.paths=[...module.paths, ...this._resolver.getGlobalPaths(etc)] But read the other comment first, perhaps this code might change anyway There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, cc @SimenB who knows more about the ESM impl cause I don't know all implications changing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
...this._resolver.getModulePaths(module.path), | ||
...this._resolver.getGlobalPaths(module.path, moduleName), | ||
]; | ||
Object.defineProperty(module, 'require', { | ||
value: this._createRequireImplementation(module, options), | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I may be missing something, but do we need to spend CPU time to grab the global paths every time? I'm thinking the solution could be as simple as grabbing those global dirs once, probably just straight away in
nodeModulesPaths.ts
:and append them to the paths list?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with grabbing only once, but it cannot be directly in funtion
nodeModulesPaths
since module paths will be cached. For instance, if the path without module name is cached. then it won't update. I'd add a new function to exportThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I understand the caching problem. We're talking about the "global paths" that come after
/node_modules
. Aren't they independent of themoduleName
, something like