@@ -33,6 +33,16 @@ const protocolHandlers = {
3333 'node:' ( ) { return 'builtin' ; } ,
3434} ;
3535
36+ /**
37+ * Determine whether the given source contains CommonJS or ES module syntax.
38+ * @param {string | Buffer | undefined } source
39+ * @param {URL } url
40+ */
41+ function detectModuleFormat ( source , url ) {
42+ if ( ! source ) { return ; }
43+ return containsModuleSyntax ( `${ source } ` , fileURLToPath ( url ) , url ) ? 'module' : 'commonjs' ;
44+ }
45+
3646/**
3747 * @param {URL } parsed
3848 * @returns {string | null }
@@ -112,26 +122,23 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
112122 default : { // The user did not pass `--experimental-default-type`.
113123 // `source` is undefined when this is called from `defaultResolve`;
114124 // but this gets called again from `defaultLoad`/`defaultLoadSync`.
115- if ( getOptionValue ( '--experimental-detect-module' ) ) {
116- const format = 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 ??= new SafeSet ( ) ;
123- if ( ! typelessPackageJsonFilesWarnedAbout . has ( pjsonPath ) ) {
124- const warning = `${ 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- }
125+ // For ambiguous files (no type field, .js extension) we return
126+ // undefined from `resolve` and re-run the check in `load`.
127+ const format = detectModuleFormat ( source , url ) ;
128+ if ( format === 'module' ) {
129+ // This module has a .js extension, a package.json with no `type` field, and ESM syntax.
130+ // Warn about the missing `type` field so that the user can avoid the performance penalty of detection.
131+ typelessPackageJsonFilesWarnedAbout ??= new SafeSet ( ) ;
132+ if ( ! typelessPackageJsonFilesWarnedAbout . has ( pjsonPath ) ) {
133+ const warning = `${ url } parsed as an ES module because module syntax was detected;` +
134+ ` to avoid the performance penalty of syntax detection, add "type": "module" to ${ pjsonPath } ` ;
135+ process . emitWarning ( warning , {
136+ code : 'MODULE_TYPELESS_PACKAGE_JSON' ,
137+ } ) ;
138+ typelessPackageJsonFilesWarnedAbout . add ( pjsonPath ) ;
131139 }
132- return format ;
133140 }
134- return 'commonjs' ;
141+ return format ;
135142 }
136143 }
137144 }
@@ -154,15 +161,14 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
154161 return 'commonjs' ;
155162 }
156163 default : { // The user did not pass `--experimental-default-type`.
157- if ( getOptionValue ( '--experimental-detect-module' ) ) {
158- if ( ! source ) { return null ; }
159- const format = getFormatOfExtensionlessFile ( url ) ;
160- if ( format === 'module' ) {
161- return containsModuleSyntax ( `${ source } ` , fileURLToPath ( url ) , url ) ? 'module' : 'commonjs' ;
162- }
164+ if ( ! source ) {
165+ return ;
166+ }
167+ const format = getFormatOfExtensionlessFile ( url ) ;
168+ if ( format === 'wasm' ) {
163169 return format ;
164170 }
165- return 'commonjs' ;
171+ return detectModuleFormat ( source , url ) ;
166172 }
167173 }
168174 }
0 commit comments