@@ -6312,8 +6312,9 @@ namespace ts {
6312
6312
*/
6313
6313
function isFileForcedToBeModuleByFormat ( file : SourceFile ) : true | undefined {
6314
6314
// Excludes declaration files - they still require an explicit `export {}` or the like
6315
- // for back compat purposes.
6316
- return file . impliedNodeFormat === ModuleKind . ESNext && ! file . isDeclarationFile ? true : undefined ;
6315
+ // for back compat purposes. The only non-declaration files _not_ forced to be a module are `.js` files
6316
+ // that aren't esm-mode (meaning not in a `type: module` scope).
6317
+ return ( file . impliedNodeFormat === ModuleKind . ESNext || ( fileExtensionIsOneOf ( file . fileName , [ Extension . Cjs , Extension . Cts ] ) ) ) && ! file . isDeclarationFile ? true : undefined ;
6317
6318
}
6318
6319
6319
6320
export function getSetExternalModuleIndicator ( options : CompilerOptions ) : ( file : SourceFile ) => void {
@@ -6322,7 +6323,7 @@ namespace ts {
6322
6323
case ModuleDetectionKind . Force :
6323
6324
// All non-declaration files are modules, declaration files still do the usual isFileProbablyExternalModule
6324
6325
return ( file : SourceFile ) => {
6325
- file . externalModuleIndicator = ! file . isDeclarationFile || isFileProbablyExternalModule ( file ) ;
6326
+ file . externalModuleIndicator = isFileProbablyExternalModule ( file ) || ! file . isDeclarationFile || undefined ;
6326
6327
} ;
6327
6328
case ModuleDetectionKind . Legacy :
6328
6329
// Files are modules if they have imports, exports, or import.meta
@@ -6382,7 +6383,8 @@ namespace ts {
6382
6383
}
6383
6384
6384
6385
export function getEmitModuleDetectionKind ( options : CompilerOptions ) {
6385
- return options . moduleDetection || ModuleDetectionKind . Auto ;
6386
+ return options . moduleDetection ||
6387
+ ( getEmitModuleKind ( options ) === ModuleKind . Node16 || getEmitModuleKind ( options ) === ModuleKind . NodeNext ? ModuleDetectionKind . Force : ModuleDetectionKind . Auto ) ;
6386
6388
}
6387
6389
6388
6390
export function hasJsonModuleEmitEnabled ( options : CompilerOptions ) {
0 commit comments