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