@@ -33,6 +33,16 @@ const protocolHandlers = {
33
33
'node:' ( ) { return 'builtin' ; } ,
34
34
} ;
35
35
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
+
36
46
/**
37
47
* @param {URL } parsed
38
48
* @returns {string | null }
@@ -112,26 +122,23 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
112
122
default : { // The user did not pass `--experimental-default-type`.
113
123
// `source` is undefined when this is called from `defaultResolve`;
114
124
// 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 ) ;
131
139
}
132
- return format ;
133
140
}
134
- return 'commonjs' ;
141
+ return format ;
135
142
}
136
143
}
137
144
}
@@ -154,15 +161,14 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
154
161
return 'commonjs' ;
155
162
}
156
163
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' ) {
163
169
return format ;
164
170
}
165
- return 'commonjs' ;
171
+ return detectModuleFormat ( source , url ) ;
166
172
}
167
173
}
168
174
}
0 commit comments