@@ -22,6 +22,8 @@ const experimentalNetworkImports =
22
22
const { containsModuleSyntax } = internalBinding ( 'contextify' ) ;
23
23
const { getPackageScopeConfig, getPackageType } = require ( 'internal/modules/package_json_reader' ) ;
24
24
const { fileURLToPath } = require ( 'internal/url' ) ;
25
+ const { readFileSync } = require ( 'fs' ) ;
26
+ const { Buffer } = require ( 'buffer' ) ;
25
27
const { ERR_UNKNOWN_FILE_EXTENSION } = require ( 'internal/errors' ) . codes ;
26
28
27
29
const protocolHandlers = {
@@ -82,6 +84,24 @@ function underNodeModules(url) {
82
84
return StringPrototypeIncludes ( url . pathname , '/node_modules/' ) ;
83
85
}
84
86
87
+ /**
88
+ * Determine whether the given source contains CJS or ESM module syntax.
89
+ * @param {string } source
90
+ * @param {URL } url
91
+ */
92
+ function detectModuleFormat ( source , url ) {
93
+ try {
94
+ let realSource = source ?? readFileSync ( url , 'utf8' ) ;
95
+ if ( Buffer . isBuffer ( realSource ) ) {
96
+ // `containsModuleSyntax` requires source to be passed in as string
97
+ realSource = realSource . toString ( ) ;
98
+ }
99
+ return containsModuleSyntax ( realSource , fileURLToPath ( url ) , url ) ? 'module' : 'commonjs' ;
100
+ } catch {
101
+ return 'commonjs' ;
102
+ }
103
+ }
104
+
85
105
let typelessPackageJsonFilesWarnedAbout ;
86
106
/**
87
107
* @param {URL } url
@@ -92,12 +112,6 @@ let typelessPackageJsonFilesWarnedAbout;
92
112
function getFileProtocolModuleFormat ( url , context = { __proto__ : null } , ignoreErrors ) {
93
113
const { source } = context ;
94
114
const ext = extname ( url ) ;
95
- const deduceFormat = ( fromSource , fromUrl ) => {
96
- const { existsSync, readFileSync } = require ( 'fs' ) ;
97
- const realSource = fromSource ?? existsSync ( fromUrl ) ? readFileSync ( fromUrl ) . toString ( ) : undefined ;
98
- return realSource ? // Do we have a source? check for module syntax
99
- ( containsModuleSyntax ( realSource , fileURLToPath ( fromUrl ) , fromUrl ) ? 'module' : 'commonjs' ) : 'commonjs' ;
100
- } ;
101
115
102
116
if ( ext === '.js' ) {
103
117
const { type : packageType , pjsonPath } = getPackageScopeConfig ( url ) ;
@@ -119,7 +133,7 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
119
133
// `source` is undefined when this is called from `defaultResolve`;
120
134
// but this gets called again from `defaultLoad`/`defaultLoadSync`.
121
135
if ( getOptionValue ( '--experimental-detect-module' ) ) {
122
- const format = deduceFormat ( source , url ) ;
136
+ const format = detectModuleFormat ( source , url ) ;
123
137
if ( format === 'module' ) {
124
138
// This module has a .js extension, a package.json with no `type` field, and ESM syntax.
125
139
// Warn about the missing `type` field so that the user can avoid the performance penalty of detection.
@@ -160,7 +174,7 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
160
174
default : { // The user did not pass `--experimental-default-type`.
161
175
if ( getOptionValue ( '--experimental-detect-module' ) ) {
162
176
const format = getFormatOfExtensionlessFile ( url ) ;
163
- return ( format === 'module' ) ? deduceFormat ( source , url ) : format ;
177
+ return ( format === 'module' ) ? detectModuleFormat ( source , url ) : format ;
164
178
}
165
179
return 'commonjs' ;
166
180
}
0 commit comments