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

Commit0d327c8

Browse files
aduh95marco-ippolito
authored andcommitted
esm: refactorget_format
PR-URL:#53872Backport-PR-URL:#56927Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>Reviewed-By: Paolo Insogna <paolo@cowtech.it>Reviewed-By: Matteo Collina <matteo.collina@gmail.com>Refs:#52697
1 parentf9dc1ea commit0d327c8

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

‎lib/internal/modules/esm/get_format.js‎

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const {
1717
mimeToFormat,
1818
}=require('internal/modules/esm/formats');
1919

20+
constdetectModule=getOptionValue('--experimental-detect-module');
2021
constexperimentalNetworkImports=
2122
getOptionValue('--experimental-network-imports');
2223
const{ containsModuleSyntax}=internalBinding('contextify');
@@ -33,6 +34,17 @@ const protocolHandlers = {
3334
'node:'(){return'builtin';},
3435
};
3536

37+
/**
38+
* Determine whether the given ambiguous source contains CommonJS or ES module syntax.
39+
*@param {string | Buffer | undefined} source
40+
*@param {URL} url
41+
*/
42+
functiondetectModuleFormat(source,url){
43+
if(!source){returndetectModule ?null :'commonjs';}
44+
if(!detectModule){return'commonjs';}
45+
returncontainsModuleSyntax(`${source}`,fileURLToPath(url),url) ?'module' :'commonjs';
46+
}
47+
3648
/**
3749
*@param {URL} parsed
3850
*@returns {string | null}
@@ -112,26 +124,23 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
112124
default:{// The user did not pass `--experimental-default-type`.
113125
// `source` is undefined when this is called from `defaultResolve`;
114126
// but this gets called again from `defaultLoad`/`defaultLoadSync`.
115-
if(getOptionValue('--experimental-detect-module')){
116-
constformat=source ?
117-
(containsModuleSyntax(`${source}`,fileURLToPath(url),url) ?'module' :'commonjs') :
118-
null;
119-
if(format==='module'){
120-
// This module has a .js extension, a package.json with no `type` field, and ESM syntax.
121-
// Warn about the missing `type` field so that the user can avoid the performance penalty of detection.
122-
typelessPackageJsonFilesWarnedAbout??=newSafeSet();
123-
if(!typelessPackageJsonFilesWarnedAbout.has(pjsonPath)){
124-
constwarning=`${url} parsed as an ES module because module syntax was detected;`+
125-
` to avoid the performance penalty of syntax detection, add "type": "module" to${pjsonPath}`;
126-
process.emitWarning(warning,{
127-
code:'MODULE_TYPELESS_PACKAGE_JSON',
128-
});
129-
typelessPackageJsonFilesWarnedAbout.add(pjsonPath);
130-
}
127+
// For ambiguous files (no type field, .js extension) we return
128+
// undefined from `resolve` and re-run the check in `load`.
129+
constformat=detectModuleFormat(source,url);
130+
if(format==='module'){
131+
// This module has a .js extension, a package.json with no `type` field, and ESM syntax.
132+
// Warn about the missing `type` field so that the user can avoid the performance penalty of detection.
133+
typelessPackageJsonFilesWarnedAbout??=newSafeSet();
134+
if(!typelessPackageJsonFilesWarnedAbout.has(pjsonPath)){
135+
constwarning=`${url} parsed as an ES module because module syntax was detected;`+
136+
` to avoid the performance penalty of syntax detection, add "type": "module" to${pjsonPath}`;
137+
process.emitWarning(warning,{
138+
code:'MODULE_TYPELESS_PACKAGE_JSON',
139+
});
140+
typelessPackageJsonFilesWarnedAbout.add(pjsonPath);
131141
}
132-
returnformat;
133142
}
134-
return'commonjs';
143+
returnformat;
135144
}
136145
}
137146
}
@@ -154,15 +163,14 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
154163
return'commonjs';
155164
}
156165
default:{// The user did not pass `--experimental-default-type`.
157-
if(getOptionValue('--experimental-detect-module')){
158-
if(!source){returnnull;}
159-
constformat=getFormatOfExtensionlessFile(url);
160-
if(format==='module'){
161-
returncontainsModuleSyntax(`${source}`,fileURLToPath(url),url) ?'module' :'commonjs';
162-
}
166+
if(!source){
167+
returnnull;
168+
}
169+
constformat=getFormatOfExtensionlessFile(url);
170+
if(format==='wasm'){
163171
returnformat;
164172
}
165-
return'commonjs';
173+
returndetectModuleFormat(source,url);
166174
}
167175
}
168176
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp