@@ -3527,56 +3527,50 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
3527
3527
}
3528
3528
3529
3529
function getImpliedNodeFormatForFile ( fileName : string ) {
3530
+ // Map to an output file before looking up a package.json that might
3531
+ // contain `"type": "module"`. This allows the same input `.ts` file to be
3532
+ // compiled as both CommonJS and ESM depending on the tsconfig's `outDir`.
3530
3533
let fileNameForModuleFormatDetection = getNormalizedAbsolutePath ( fileName , currentDirectory ) ;
3531
3534
if ( moduleFormatNeedsPackageJsonLookup ( fileName , options ) ) {
3532
3535
const projectReference = getResolvedProjectReferenceToRedirect ( fileName ) ;
3533
3536
if ( projectReference ) {
3534
3537
// We have a source file that is an input to a referenced project.
3535
- // This should only happen with `useSourceOfProjectReferenceRedirect` enabled.
3538
+ // This should only happen with `useSourceOfProjectReferenceRedirect`
3539
+ // enabled (i.e. TS Server scenarios).
3536
3540
Debug . assert ( useSourceOfProjectReferenceRedirect ) ;
3537
- if ( projectReference . commandLine . options . outDir ) {
3538
- const outputFile = getOutputJSFileName (
3541
+ fileNameForModuleFormatDetection =
3542
+ getOutputDeclarationFileName (
3539
3543
fileNameForModuleFormatDetection ,
3540
3544
projectReference . commandLine ,
3541
- ! host . useCaseSensitiveFileNames ( ) ) ;
3542
- if ( outputFile ) {
3543
- fileNameForModuleFormatDetection = outputFile ;
3544
- }
3545
- }
3546
- else if ( projectReference . commandLine . options . declaration && projectReference . commandLine . options . declarationDir ) {
3547
- const outputFile = getOutputDeclarationFileName (
3545
+ ! host . useCaseSensitiveFileNames ( ) ) ||
3546
+ getOutputJSFileName (
3548
3547
fileNameForModuleFormatDetection ,
3549
3548
projectReference . commandLine ,
3550
- ! host . useCaseSensitiveFileNames ( ) ) ;
3551
- if ( outputFile ) {
3552
- fileNameForModuleFormatDetection = outputFile ;
3553
- }
3554
- }
3555
- }
3556
- else if ( options . outDir ) {
3557
- const outputFile = getOutputJSFileNameWithoutConfigFile (
3558
- fileNameForModuleFormatDetection ,
3559
- options ,
3560
- ! host . useCaseSensitiveFileNames ( ) ,
3561
- currentDirectory ,
3562
- getAssumedCommonSourceDirectory ) ;
3563
- if ( outputFile ) {
3564
- fileNameForModuleFormatDetection = outputFile ;
3565
- }
3549
+ ! host . useCaseSensitiveFileNames ( ) ) ||
3550
+ fileNameForModuleFormatDetection ;
3566
3551
}
3567
- else if ( options . declaration && options . declarationDir ) {
3568
- const outputFile = getOutputDeclarationFileNameWithoutConfigFile (
3569
- fileNameForModuleFormatDetection ,
3570
- options ,
3571
- ! host . useCaseSensitiveFileNames ( ) ,
3572
- currentDirectory ,
3573
- getAssumedCommonSourceDirectory ) ;
3574
- if ( outputFile ) {
3575
- fileNameForModuleFormatDetection = outputFile ;
3576
- }
3552
+ else {
3553
+ fileNameForModuleFormatDetection =
3554
+ getOutputDeclarationFileNameWithoutConfigFile (
3555
+ fileNameForModuleFormatDetection ,
3556
+ options ,
3557
+ ! host . useCaseSensitiveFileNames ( ) ,
3558
+ currentDirectory ,
3559
+ getAssumedCommonSourceDirectory ) ||
3560
+ getOutputJSFileNameWithoutConfigFile (
3561
+ fileNameForModuleFormatDetection ,
3562
+ options ,
3563
+ ! host . useCaseSensitiveFileNames ( ) ,
3564
+ currentDirectory ,
3565
+ getAssumedCommonSourceDirectory ) ||
3566
+ fileNameForModuleFormatDetection ;
3577
3567
}
3578
3568
}
3579
- return getImpliedNodeFormatForFileWorker ( fileNameForModuleFormatDetection , moduleResolutionCache ?. getPackageJsonInfoCache ( ) , host , options ) ;
3569
+ return getImpliedNodeFormatForFileWorker (
3570
+ fileNameForModuleFormatDetection ,
3571
+ moduleResolutionCache ?. getPackageJsonInfoCache ( ) ,
3572
+ host ,
3573
+ options ) ;
3580
3574
}
3581
3575
3582
3576
function getCreateSourceFileOptions ( fileName : string ) : CreateSourceFileOptions {
0 commit comments