@@ -6305,8 +6305,9 @@ namespace ts {
63056305 */
63066306 function isFileForcedToBeModuleByFormat ( file : SourceFile ) : true | undefined {
63076307 // 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 ;
63106311 }
63116312
63126313 export function getSetExternalModuleIndicator ( options : CompilerOptions ) : ( file : SourceFile ) => void {
@@ -6315,7 +6316,7 @@ namespace ts {
63156316 case ModuleDetectionKind . Force :
63166317 // All non-declaration files are modules, declaration files still do the usual isFileProbablyExternalModule
63176318 return ( file : SourceFile ) => {
6318- file . externalModuleIndicator = ! file . isDeclarationFile || isFileProbablyExternalModule ( file ) ;
6319+ file . externalModuleIndicator = isFileProbablyExternalModule ( file ) || ! file . isDeclarationFile || undefined ;
63196320 } ;
63206321 case ModuleDetectionKind . Legacy :
63216322 // Files are modules if they have imports, exports, or import.meta
@@ -6375,7 +6376,8 @@ namespace ts {
63756376 }
63766377
63776378 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 ) ;
63796381 }
63806382
63816383 export function hasJsonModuleEmitEnabled ( options : CompilerOptions ) {
0 commit comments