Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Unexpected behavior in module customization hooks / loader, forrequire #55808

Open
Labels
loadersIssues and PRs related to ES module loaders
@nomagick

Description

@nomagick

It seems I am one of the early adopters of module hooks.
I liked the idea of the module customization hooks, and tried to use it in my project.
However, I believe the current behavior ofrequire handling in the esm loader is buggy.

Relevantsource code, here

constrequireFn=functionrequire(specifier){
letimportAttributes=kEmptyObject;
if(!StringPrototypeStartsWith(specifier,'node:')&&!BuiltinModule.normalizeRequirableId(specifier)){
// TODO: do not depend on the monkey-patchable CJS loader here.
constpath=CJSModule._resolveFilename(specifier,module);
switch(extname(path)){
case'.json':
importAttributes={__proto__:null,type:'json'};
break;
case'.node':
returnwrapModuleLoad(specifier,module);
default:
// fall through
}
specifier=`${pathToFileURL(path)}`;
}
constjob=cascadedLoader.getModuleJobForRequireInImportedCJS(specifier,url,importAttributes);
job.runSync();
returncjsCache.get(job.url).exports;
};
setOwnProperty(requireFn,'resolve',functionresolve(specifier){
if(!StringPrototypeStartsWith(specifier,'node:')){
constpath=CJSModule._resolveFilename(specifier,module);
if(specifier!==path){
specifier=`${pathToFileURL(path)}`;
}
}
const{url:resolvedURL}=cascadedLoader.resolveSync(specifier,url,kEmptyObject);
returnurlToFilename(resolvedURL);
});

The resolving of thespecifier is done in the main thread, usingModule._resolveFilename, before being sent to the customization hooks.
The resolver hook is then receiving a resolvedfile:// URL, instead of the realspecifier.

These are not expected according to the documentation of the module customization hooks.
And for my use case, it has made the hooks useless.

To make it clear there are two bugs:

  1. The cjs specifiers are being resolved in the wrong thread.
    The hooks, therefore, cannot really work, because if the "expected" file does not really exist on the hard drive. The cjsModule._resolveFilename will throw an error in the first place.
  2. The resolve hook is receiving a wrongspecifier, if the first bug would not trigger.

The root of this issue might be the default resolver in customization hooks does not support resolving CJS modules.
An easy fix would be to move theModule._resolveFilename into the default resolver.
The loading of.node native add-ons may also get in the way.
But IMO it should also go through the hooks, and eventually be loaded as{type: 'commonjs', source: undefined, url: 'file://... .node'}

Metadata

Metadata

Assignees

No one assigned

    Labels

    loadersIssues and PRs related to ES module loaders

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp