@@ -55,6 +55,7 @@ import {
55
55
getOriginalOrResolvedModuleFileName ,
56
56
getOriginalOrResolvedTypeReferenceFileName ,
57
57
getOwnKeys ,
58
+ getPackageJsonLocationFromScope ,
58
59
getRelativePathFromDirectory ,
59
60
getTsBuildInfoEmitOutputFilePath ,
60
61
handleNoEmitOptions ,
@@ -66,7 +67,6 @@ import {
66
67
isJsonSourceFile ,
67
68
isNumber ,
68
69
isString ,
69
- last ,
70
70
map ,
71
71
mapDefined ,
72
72
maybeBind ,
@@ -78,8 +78,9 @@ import {
78
78
OldBuildInfoProgramConstructor ,
79
79
OldBuildInfoProgramHost ,
80
80
outFile ,
81
- PackageJsonInfo ,
82
81
PackageJsonInfoCache ,
82
+ PackageJsonInfoContents ,
83
+ PackageJsonScope ,
83
84
Path ,
84
85
PerDirectoryAndNonRelativeNameCache ,
85
86
PerNonRelativeNameCache ,
@@ -187,7 +188,7 @@ export interface ReusableBuilderProgramState extends BuilderState {
187
188
modules : PerDirectoryAndNonRelativeNameCache < ResolvedModuleWithFailedLookupLocations > | undefined ;
188
189
typeRefs : PerDirectoryAndNonRelativeNameCache < ResolvedTypeReferenceDirectiveWithFailedLookupLocations > | undefined ;
189
190
packageJsons : Map < Path , string > | undefined ;
190
- nonRelativePackageJsonsCache : PerNonRelativeNameCache < string > | undefined ;
191
+ packageJsonScopes : PerNonRelativeNameCache < PackageJsonScope > | undefined ;
191
192
packageJsonCache : PackageJsonInfoCache | undefined ;
192
193
} ;
193
194
resuableCacheResolutions ?: {
@@ -1472,20 +1473,19 @@ function getCacheResolutions(state: BuilderProgramState) {
1472
1473
let modules : PerDirectoryAndNonRelativeNameCache < ResolvedModuleWithFailedLookupLocations > | undefined ;
1473
1474
let typeRefs : PerDirectoryAndNonRelativeNameCache < ResolvedTypeReferenceDirectiveWithFailedLookupLocations > | undefined ;
1474
1475
let packageJsons : Map < Path , string > | undefined ;
1475
- let nonRelativePackageJsonsCache : PerNonRelativeNameCache < string > | undefined ;
1476
+ let packageJsonScopes : PerNonRelativeNameCache < PackageJsonScope > | undefined ;
1476
1477
for ( const f of state . program ! . getSourceFiles ( ) ) {
1477
1478
modules = toPerDirectoryAndNonRelativeNameCache ( state , modules , getOriginalOrResolvedModuleFileName , f . resolvedModules , f ) ;
1478
1479
typeRefs = toPerDirectoryAndNonRelativeNameCache ( state , typeRefs , getOriginalOrResolvedTypeReferenceFileName , f . resolvedTypeReferenceDirectiveNames , f ) ;
1479
- if ( f . packageJsonScope ) {
1480
+ if ( f . packageJsonScope ?. contents ) {
1480
1481
const dirPath = getDirectoryPath ( f . resolvedPath ) ;
1481
- if ( ! nonRelativePackageJsonsCache ?. getWithPath ( dirPath ) ) {
1482
- const result = last ( f . packageJsonLocations ! ) ;
1483
- ( packageJsons ??= new Map ( ) ) . set ( dirPath , result ) ;
1484
- ( nonRelativePackageJsonsCache ??= createPerNonRelativeNameCache (
1482
+ if ( ! packageJsonScopes ?. getWithPath ( dirPath ) ) {
1483
+ ( packageJsons ??= new Map ( ) ) . set ( dirPath , getPackageJsonLocationFromScope ( f . packageJsonScope ) ) ;
1484
+ ( packageJsonScopes ??= createPerNonRelativeNameCache (
1485
1485
state . program ! . getCurrentDirectory ( ) ,
1486
1486
state . program ! . getCanonicalFileName ,
1487
- identity ,
1488
- ) ) . setWithPath ( dirPath , result , ancestorPath => packageJsons ! . delete ( ancestorPath ) ) ;
1487
+ getPackageJsonLocationFromScope ,
1488
+ ) ) . setWithPath ( dirPath , f . packageJsonScope , ancestorPath => packageJsons ! . delete ( ancestorPath ) ) ;
1489
1489
}
1490
1490
}
1491
1491
}
@@ -1498,7 +1498,7 @@ function getCacheResolutions(state: BuilderProgramState) {
1498
1498
modules,
1499
1499
typeRefs,
1500
1500
packageJsons,
1501
- nonRelativePackageJsonsCache ,
1501
+ packageJsonScopes ,
1502
1502
packageJsonCache : state . program ! . getModuleResolutionCache ( ) ?. getPackageJsonInfoCache ( ) . clone ( ) ,
1503
1503
} ;
1504
1504
}
@@ -2158,6 +2158,7 @@ export function createOldBuildInfoProgram(
2158
2158
if ( ! cacheResolutions && ! resuableCacheResolutions ) return undefined ;
2159
2159
const fileExistsMap = new Map < string , boolean > ( ) ;
2160
2160
const affectingLoationsSameMap = new Map < string , boolean > ( ) ;
2161
+ const packageJsonInfoContentsMap = new Map < string , PackageJsonInfoContents | false > ( ) ;
2161
2162
2162
2163
type Resolution = ResolvedModuleWithFailedLookupLocations & ResolvedTypeReferenceDirectiveWithFailedLookupLocations ;
2163
2164
type ResolutionEntry = [ name : string , resolutionId : ProgramBuildInfoResolutionId , mode : ResolutionMode ] ;
@@ -2168,6 +2169,7 @@ export function createOldBuildInfoProgram(
2168
2169
const reusableResolvedModules = intializeReusableResolutionsCache ( resuableCacheResolutions ?. cache . modules ) ;
2169
2170
const reusableResolvedTypeRefs = intializeReusableResolutionsCache ( resuableCacheResolutions ?. cache . typeRefs ) ;
2170
2171
let decodedPackageJsons : PerNonRelativeNameCache < string > | undefined ;
2172
+ let packageJsonScopes : Map < string , PackageJsonScope | false > | undefined ;
2171
2173
let decodedHashes : Map < ProgramBuildInfoAbsoluteFileId , string | undefined > | undefined ;
2172
2174
let resolutions : ( Resolution | false ) [ ] | undefined ;
2173
2175
let originalPathOrResolvedFileNames : string [ ] | undefined ;
@@ -2194,7 +2196,7 @@ export function createOldBuildInfoProgram(
2194
2196
dirPath ,
2195
2197
redirectedReference ,
2196
2198
) ,
2197
- getPackageJsonPath ,
2199
+ getPackageJsonScope ,
2198
2200
} ;
2199
2201
2200
2202
function intializeReusableResolutionsCache ( reusable : ProgramBuildInfoResolutionCacheWithRedirects | undefined ) : ReusableResolutionsCache | undefined {
@@ -2209,7 +2211,7 @@ export function createOldBuildInfoProgram(
2209
2211
2210
2212
function affectingLocationsSame (
2211
2213
fileName : string ,
2212
- expected : PackageJsonInfo | boolean | string | undefined
2214
+ expected : PackageJsonInfoContents | string | undefined
2213
2215
) : boolean {
2214
2216
let result = affectingLoationsSameMap . get ( fileName ) ;
2215
2217
if ( result !== undefined ) return result ;
@@ -2219,17 +2221,34 @@ export function createOldBuildInfoProgram(
2219
2221
result = ! ! currentText && ( host . createHash ?? generateDjb2Hash ) ( currentText ) === expected ;
2220
2222
}
2221
2223
else {
2222
- const expectedText = typeof expected === "object" ? expected . contents . packageJsonText : undefined ;
2223
- result = currentText === expectedText ;
2224
+ result = currentText === expected ?. packageJsonText ;
2224
2225
}
2225
2226
affectingLoationsSameMap . set ( fileName , result ) ;
2226
2227
return result ;
2227
2228
}
2228
2229
2229
- function getPackageJsonPath ( dir : string ) {
2230
- const fromCache = cacheResolutions ?. nonRelativePackageJsonsCache ?. get ( dir ) ;
2230
+ function getPackageJsonInfoContents ( fileName : string ) {
2231
+ let result = packageJsonInfoContentsMap . get ( fileName ) ;
2232
+ if ( result === undefined ) packageJsonInfoContentsMap . set ( fileName , result = host . getPackageJsonInfo ( fileName ) ?. contents || false ) ;
2233
+ return result || undefined ;
2234
+ }
2235
+
2236
+ function getPackageJsonScope ( dir : string ) : PackageJsonScope | undefined {
2237
+ const fromCache = cacheResolutions ?. packageJsonScopes ?. get ( dir ) ;
2231
2238
if ( fromCache ) {
2232
- return fileExists ( fromCache ) ? fromCache : undefined ;
2239
+ const packageJson = getPackageJsonLocationFromScope ( fromCache ) ! ;
2240
+ let result = packageJsonScopes ?. get ( packageJson ) ;
2241
+ if ( result === undefined ) {
2242
+ ( packageJsonScopes ??= new Map ( ) ) . set (
2243
+ packageJson ,
2244
+ result = affectingLocationsSame ( packageJson , fromCache . contents ) ?
2245
+ fromCache :
2246
+ fileExists ( packageJson ) ?
2247
+ { contents : getPackageJsonInfoContents ( packageJson ) , affectingLocations : [ packageJson ] } :
2248
+ false
2249
+ ) ;
2250
+ }
2251
+ return result || undefined ;
2233
2252
}
2234
2253
if ( ! resuableCacheResolutions ?. cache . packageJsons ) return ;
2235
2254
if ( ! decodedPackageJsons ) {
@@ -2252,8 +2271,27 @@ export function createOldBuildInfoProgram(
2252
2271
decodedPackageJsons . setWithPath ( dirPath , packageJson , noop ) ;
2253
2272
}
2254
2273
}
2255
- const fromDecoded = decodedPackageJsons . get ( dir ) ;
2256
- return fromDecoded && fileExists ( fromDecoded ) ? fromDecoded : undefined ;
2274
+ return toPackageJsonScope ( decodedPackageJsons . get ( dir ) ) ;
2275
+ }
2276
+
2277
+ function toPackageJsonScope ( file : string | undefined ) : PackageJsonScope | undefined {
2278
+ if ( ! file ) return undefined ;
2279
+ let result = packageJsonScopes ?. get ( file ) ;
2280
+ if ( result !== undefined ) return result || undefined ;
2281
+ ( packageJsonScopes ??= new Map ( ) ) ;
2282
+ if ( fileExists ( file ) ) {
2283
+ result = {
2284
+ contents : getPackageJsonInfoContents ( file ) ,
2285
+ affectingLocations : [ file ]
2286
+ } ;
2287
+ }
2288
+ packageJsonScopes . set ( file , result || false ) ;
2289
+ return result ;
2290
+ }
2291
+
2292
+ function getPackageJsonContentsFromCachedResolutions ( fileName : string ) {
2293
+ const info = cacheResolutions ! . packageJsonCache ?. getPackageJsonInfo ( fileName ) ;
2294
+ return typeof info === "object" ? info . contents : undefined ;
2257
2295
}
2258
2296
2259
2297
function getResolvedFromCache < T extends ResolvedModuleWithFailedLookupLocations | ResolvedTypeReferenceDirectiveWithFailedLookupLocations > (
@@ -2277,7 +2315,7 @@ export function createOldBuildInfoProgram(
2277
2315
fromCache . affectingLocations ,
2278
2316
fileName => affectingLocationsSame (
2279
2317
fileName ,
2280
- cacheResolutions ! . packageJsonCache ?. getPackageJsonInfo ( fileName )
2318
+ getPackageJsonContentsFromCachedResolutions ( fileName )
2281
2319
)
2282
2320
) ? fromCache : undefined ;
2283
2321
}
0 commit comments