@@ -92,8 +92,9 @@ let typelessPackageJsonFilesWarnedAbout; |
92 | 92 | function warnTypelessPackageJsonFile(pjsonPath, url) { |
93 | 93 | typelessPackageJsonFilesWarnedAbout ??= new SafeSet(); |
94 | 94 | if (!typelessPackageJsonFilesWarnedAbout.has(pjsonPath)) { |
95 | | - const warning = `${url} parsed as an ES module because module syntax was detected;` + |
96 | | - ` to avoid the performance penalty of syntax detection, add "type": "module" to ${pjsonPath}`; |
| 95 | + const warning = `Module type of ${url} is not specified and it doesn't parse as CommonJS.\n` + |
| 96 | + 'Reparsing as ES module because module syntax was detected. This incurs a performance overhead.\n' + |
| 97 | + `To eliminate this warning, add "type": "module" to ${pjsonPath}.`; |
97 | 98 | process.emitWarning(warning, { |
98 | 99 | code: 'MODULE_TYPELESS_PACKAGE_JSON', |
99 | 100 | }); |
@@ -112,7 +113,7 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE |
112 | 113 | const ext = extname(url); |
113 | 114 |
|
114 | 115 | if (ext === '.js') { |
115 | | - const { type: packageType, pjsonPath } = getPackageScopeConfig(url); |
| 116 | + const { type: packageType, pjsonPath, exists: foundPackageJson } = getPackageScopeConfig(url); |
116 | 117 | if (packageType !== 'none') { |
117 | 118 | return packageType; |
118 | 119 | } |
@@ -133,7 +134,7 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE |
133 | 134 | // For ambiguous files (no type field, .js extension) we return |
134 | 135 | // undefined from `resolve` and re-run the check in `load`. |
135 | 136 | const format = detectModuleFormat(source, url); |
136 | | - if (format === 'module') { |
| 137 | + if (format === 'module' && foundPackageJson) { |
137 | 138 | // This module has a .js extension, a package.json with no `type` field, and ESM syntax. |
138 | 139 | // Warn about the missing `type` field so that the user can avoid the performance penalty of detection. |
139 | 140 | warnTypelessPackageJsonFile(pjsonPath, url); |
@@ -143,7 +144,7 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE |
143 | 144 | } |
144 | 145 | } |
145 | 146 | if (ext === '.ts' && getOptionValue('--experimental-strip-types')) { |
146 | | - const { type: packageType, pjsonPath } = getPackageScopeConfig(url); |
| 147 | + const { type: packageType, pjsonPath, exists: foundPackageJson } = getPackageScopeConfig(url); |
147 | 148 | if (packageType !== 'none') { |
148 | 149 | return `${packageType}-typescript`; |
149 | 150 | } |
@@ -168,7 +169,7 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE |
168 | 169 | const detectedFormat = detectModuleFormat(parsedSource, url); |
169 | 170 | // When source is undefined, default to module-typescript. |
170 | 171 | const format = detectedFormat ? `${detectedFormat}-typescript` : 'module-typescript'; |
171 | | - if (format === 'module-typescript') { |
| 172 | + if (format === 'module-typescript' && foundPackageJson) { |
172 | 173 | // This module has a .js extension, a package.json with no `type` field, and ESM syntax. |
173 | 174 | // Warn about the missing `type` field so that the user can avoid the performance penalty of detection. |
174 | 175 | warnTypelessPackageJsonFile(pjsonPath, url); |
|