@@ -2357,27 +2357,40 @@ func IsDefaultImport(node *Node /*ImportDeclaration | ImportEqualsDeclaration |
2357
2357
return importClause != nil && importClause .AsImportClause ().name != nil
2358
2358
}
2359
2359
2360
- func GetEmitModuleFormatOfFileWorker (sourceFile * SourceFile , options * core.CompilerOptions ) core.ModuleKind {
2361
- result := GetImpliedNodeFormatForEmitWorker (sourceFile , options )
2360
+ func GetImpliedNodeFormatForFile (path string , packageJsonType string ) core.ModuleKind {
2361
+ impliedNodeFormat := core .ResolutionModeNone
2362
+ if tspath .FileExtensionIsOneOf (path , []string {tspath .ExtensionDmts , tspath .ExtensionMts , tspath .ExtensionMjs }) {
2363
+ impliedNodeFormat = core .ResolutionModeESM
2364
+ } else if tspath .FileExtensionIsOneOf (path , []string {tspath .ExtensionDcts , tspath .ExtensionCts , tspath .ExtensionCjs }) {
2365
+ impliedNodeFormat = core .ResolutionModeCommonJS
2366
+ } else if packageJsonType != "" && tspath .FileExtensionIsOneOf (path , []string {tspath .ExtensionDts , tspath .ExtensionTs , tspath .ExtensionTsx , tspath .ExtensionJs , tspath .ExtensionJsx }) {
2367
+ impliedNodeFormat = core .IfElse (packageJsonType == "module" , core .ResolutionModeESM , core .ResolutionModeCommonJS )
2368
+ }
2369
+
2370
+ return impliedNodeFormat
2371
+ }
2372
+
2373
+ func GetEmitModuleFormatOfFileWorker (sourceFile * SourceFile , options * core.CompilerOptions , sourceFileMetaData * SourceFileMetaData ) core.ModuleKind {
2374
+ result := GetImpliedNodeFormatForEmitWorker (sourceFile .FileName (), options , sourceFileMetaData )
2362
2375
if result != core .ModuleKindNone {
2363
2376
return result
2364
2377
}
2365
2378
return options .GetEmitModuleKind ()
2366
2379
}
2367
2380
2368
- func GetImpliedNodeFormatForEmitWorker (sourceFile * SourceFile , options * core.CompilerOptions ) core.ResolutionMode {
2381
+ func GetImpliedNodeFormatForEmitWorker (fileName string , options * core.CompilerOptions , sourceFileMetaData * SourceFileMetaData ) core.ModuleKind {
2369
2382
moduleKind := options .GetEmitModuleKind ()
2370
- if core .ModuleKindNode16 <= moduleKind && moduleKind <= core .ModuleKindNodeNext {
2371
- return sourceFile .ImpliedNodeFormat
2383
+ if core .ModuleKindNode16 <= moduleKind && moduleKind <= core .ModuleKindNodeNext && sourceFileMetaData != nil {
2384
+ return sourceFileMetaData .ImpliedNodeFormat
2372
2385
}
2373
- if sourceFile .ImpliedNodeFormat == core .ModuleKindCommonJS &&
2374
- ( /*sourceFile.packageJsonScope.contents.packageJsonContent.type == "commonjs " ||*/ // !!!
2375
- tspath .FileExtensionIsOneOf (sourceFile . FileName () , []string {tspath .ExtensionCjs , tspath .ExtensionCts })) {
2386
+ if sourceFileMetaData != nil && sourceFileMetaData .ImpliedNodeFormat == core .ModuleKindCommonJS &&
2387
+ (sourceFileMetaData . PackageJsonType != "module " ||
2388
+ tspath .FileExtensionIsOneOf (fileName , []string {tspath .ExtensionCjs , tspath .ExtensionCts })) {
2376
2389
return core .ModuleKindCommonJS
2377
2390
}
2378
- if sourceFile .ImpliedNodeFormat == core .ModuleKindESNext &&
2379
- ( /*sourceFile.packageJsonScope?.contents.packageJsonContent.type === "module" ||*/ // !!!
2380
- tspath .FileExtensionIsOneOf (sourceFile . fileName , []string {tspath .ExtensionMjs , tspath .ExtensionMts })) {
2391
+ if sourceFileMetaData != nil && sourceFileMetaData .ImpliedNodeFormat == core .ModuleKindESNext &&
2392
+ (sourceFileMetaData . PackageJsonType == "module" ||
2393
+ tspath .FileExtensionIsOneOf (fileName , []string {tspath .ExtensionMjs , tspath .ExtensionMts })) {
2381
2394
return core .ModuleKindESNext
2382
2395
}
2383
2396
return core .ModuleKindNone
0 commit comments