@@ -73,6 +73,7 @@ namespace ts {
73
73
modules : CacheWithRedirects < Path , ModeAwareCache < ResolvedModuleWithFailedLookupLocations > > | undefined ;
74
74
typeRefs : CacheWithRedirects < Path , ModeAwareCache < ResolvedTypeReferenceDirectiveWithFailedLookupLocations > > | undefined ;
75
75
moduleNameToDirectoryMap : CacheWithRedirects < ModeAwareCacheKey , ESMap < Path , ResolvedModuleWithFailedLookupLocations > > ;
76
+ perDirPackageJsonMap : ESMap < Path , string > | undefined ;
76
77
packageJsonCache : PackageJsonInfoCache | undefined ;
77
78
} ;
78
79
/**
@@ -820,13 +821,15 @@ namespace ts {
820
821
own : ProgramBuildInfoResolutionCache | undefined ;
821
822
redirects : readonly ProgramBuildInfoResolutionRedirectsCache [ ] ;
822
823
} ;
824
+ export type ProgramBuildInfoPackageJsons = ( ProgramBuildInfoAbsoluteFileId | [ dirId : ProgramBuildInfoFileId , packageJson : ProgramBuildInfoAbsoluteFileId ] ) [ ] ;
823
825
export interface ProgramBuildInfoCacheResolutions {
824
826
resolutions : readonly ProgramBuildInfoResolution [ ] ;
825
827
names : readonly string [ ] ;
826
828
hash : readonly ProgramBuildInfoHash [ ] | undefined ;
827
829
resolutionEntries : ProgramBuildInfoResolutionEntry [ ] ;
828
- modules ?: ProgramBuildInfoResolutionCacheWithRedirects ;
829
- typeRefs ?: ProgramBuildInfoResolutionCacheWithRedirects ;
830
+ modules : ProgramBuildInfoResolutionCacheWithRedirects | undefined ;
831
+ typeRefs : ProgramBuildInfoResolutionCacheWithRedirects | undefined ;
832
+ packageJsons : ProgramBuildInfoPackageJsons | undefined ;
830
833
}
831
834
832
835
export interface ProgramMultiFileEmitBuildInfo {
@@ -1113,6 +1116,7 @@ namespace ts {
1113
1116
const cacheResolutions = getCacheResolutions ( state ) ;
1114
1117
const modules = toProgramBuildInfoResolutionCacheWithRedirects ( cacheResolutions ?. modules ) ;
1115
1118
const typeRefs = toProgramBuildInfoResolutionCacheWithRedirects ( cacheResolutions ?. typeRefs ) ;
1119
+ const packageJsons = toProgramBuildInfoPackageJsons ( cacheResolutions ?. perDirPackageJsonMap ) ;
1116
1120
if ( ! resolutions ) return ;
1117
1121
Debug . assertIsDefined ( names ) ;
1118
1122
Debug . assertIsDefined ( resolutionEntries ) ;
@@ -1124,12 +1128,27 @@ namespace ts {
1124
1128
resolutionEntries,
1125
1129
modules,
1126
1130
typeRefs,
1131
+ packageJsons,
1127
1132
} ,
1128
1133
getProgramBuildInfoFilePathDecoder : memoize ( ( ) => getProgramBuildInfoFilePathDecoder ( fileNames , buildInfoPath , currentDirectory , getCanonicalFileName ) )
1129
1134
} ;
1130
1135
return state . resuableCacheResolutions . cache ;
1131
1136
}
1132
1137
1138
+ function toProgramBuildInfoPackageJsons ( cache : ESMap < Path , string > | undefined ) : ProgramBuildInfoPackageJsons | undefined {
1139
+ let result : ProgramBuildInfoPackageJsons | undefined ;
1140
+ cache ?. forEach ( ( packageJson , dirPath ) => {
1141
+ const packageJsonDirPath = getDirectoryPath ( toPath ( packageJson , currentDirectory , getCanonicalFileName ) ) ;
1142
+ ( result ??= [ ] ) . push ( packageJsonDirPath === dirPath ?
1143
+ toAbsoluteFileId ( packageJson ) :
1144
+ [
1145
+ toFileId ( dirPath ) ,
1146
+ toAbsoluteFileId ( packageJson ) ,
1147
+ ] ) ;
1148
+ } ) ;
1149
+ return result ;
1150
+ }
1151
+
1133
1152
function toProgramBuildInfoResolutionCacheWithRedirects < T extends ResolvedModuleWithFailedLookupLocations | ResolvedTypeReferenceDirectiveWithFailedLookupLocations > (
1134
1153
cache : CacheWithRedirects < Path , ModeAwareCache < T > > | undefined
1135
1154
) : ProgramBuildInfoResolutionCacheWithRedirects | undefined {
@@ -1251,10 +1270,27 @@ namespace ts {
1251
1270
let modules : CacheWithRedirects < Path , ModeAwareCache < ResolvedModuleWithFailedLookupLocations > > | undefined ;
1252
1271
let typeRefs : CacheWithRedirects < Path , ModeAwareCache < ResolvedTypeReferenceDirectiveWithFailedLookupLocations > > | undefined ;
1253
1272
const moduleNameToDirectoryMap = createCacheWithRedirects < ModeAwareCacheKey , ESMap < Path , ResolvedModuleWithFailedLookupLocations > > ( state . compilerOptions ) ;
1273
+ const dirToPackageJsonMap = new Map < Path , string > ( ) ;
1274
+ let perDirPackageJsonMap : ESMap < Path , string > | undefined ;
1254
1275
const getCanonicalFileName = createGetCanonicalFileName ( state . program ! . useCaseSensitiveFileNames ( ) ) ;
1255
1276
state . program ! . getSourceFiles ( ) . forEach ( f => {
1256
1277
modules = toPerDirectoryCache ( state , getCanonicalFileName , modules , f , f . resolvedModules , moduleNameToDirectoryMap ) ;
1257
1278
typeRefs = toPerDirectoryCache ( state , getCanonicalFileName , typeRefs , f , f . resolvedTypeReferenceDirectiveNames ) ;
1279
+ if ( f . packageJsonScope ) {
1280
+ const dirPath = getDirectoryPath ( f . resolvedPath ) ;
1281
+ if ( ! dirToPackageJsonMap ?. has ( dirPath ) ) {
1282
+ const result = last ( f . packageJsonLocations ! ) ;
1283
+ ( perDirPackageJsonMap ??= new Map ( ) ) . set ( dirPath , result ) ;
1284
+ moduleNameToDirectorySet (
1285
+ dirToPackageJsonMap ,
1286
+ dirPath ,
1287
+ result ,
1288
+ identity ,
1289
+ dir => toPath ( dir , state . program ! . getCurrentDirectory ( ) , getCanonicalFileName ) ,
1290
+ ancestorPath => perDirPackageJsonMap ?. delete ( ancestorPath ) ,
1291
+ ) ;
1292
+ }
1293
+ }
1258
1294
} ) ;
1259
1295
const automaticTypeDirectiveNames = state . program ! . getAutomaticTypeDirectiveNames ( ) ;
1260
1296
if ( automaticTypeDirectiveNames . length ) {
@@ -1266,6 +1302,7 @@ namespace ts {
1266
1302
modules,
1267
1303
typeRefs,
1268
1304
moduleNameToDirectoryMap,
1305
+ perDirPackageJsonMap,
1269
1306
packageJsonCache : state . program ! . getModuleResolutionCache ( ) ?. getPackageJsonInfoCache ( ) . clone ( ) ,
1270
1307
} ;
1271
1308
}
0 commit comments