@@ -56,7 +56,9 @@ import {
56
56
getOptionsNameMap ,
57
57
getOwnKeys ,
58
58
getRelativePathFromDirectory ,
59
- getResolvedFileNameForModuleNameToDirectorySet ,
59
+ GetResolutionWithResolvedFileName ,
60
+ getResolvedModuleOfResolution ,
61
+ getResolvedTypeReferenceDirectiveOfResolution ,
60
62
getTsBuildInfoEmitOutputFilePath ,
61
63
handleNoEmitOptions ,
62
64
HostForComputeHash ,
@@ -90,6 +92,8 @@ import {
90
92
ReadBuildProgramHost ,
91
93
ReadonlyCollection ,
92
94
ResolutionMode ,
95
+ ResolutionWithFailedLookupLocations ,
96
+ ResolutionWithResolvedFileName ,
93
97
ResolvedModuleFull ,
94
98
ResolvedModuleWithFailedLookupLocations ,
95
99
ResolvedProjectReference ,
@@ -104,6 +108,7 @@ import {
104
108
sourceFileMayBeEmitted ,
105
109
SourceMapEmitResult ,
106
110
toPath ,
111
+ toPerDirectoryResolution ,
107
112
tryAddToSet ,
108
113
WriteFileCallback ,
109
114
WriteFileCallbackData ,
@@ -1472,8 +1477,8 @@ function getCacheResolutions(state: BuilderProgramState, getCanonicalFileName: G
1472
1477
const dirToPackageJsonMap = new Map < Path , string > ( ) ;
1473
1478
let perDirPackageJsonMap : Map < Path , string > | undefined ;
1474
1479
state . program ! . getSourceFiles ( ) . forEach ( f => {
1475
- modules = toPerDirectoryCache ( state , getCanonicalFileName ! , modules , f , f . resolvedModules , moduleNameToDirectoryMap ) ;
1476
- typeRefs = toPerDirectoryCache ( state , getCanonicalFileName ! , typeRefs , f , f . resolvedTypeReferenceDirectiveNames ) ;
1480
+ modules = toPerDirectoryCache ( state , getCanonicalFileName ! , modules , getResolvedModuleOfResolution , f , f . resolvedModules , moduleNameToDirectoryMap ) ;
1481
+ typeRefs = toPerDirectoryCache ( state , getCanonicalFileName ! , typeRefs , getResolvedTypeReferenceDirectiveOfResolution , f , f . resolvedTypeReferenceDirectiveNames ) ;
1477
1482
if ( f . packageJsonScope ) {
1478
1483
const dirPath = getDirectoryPath ( f . resolvedPath ) ;
1479
1484
if ( ! dirToPackageJsonMap ?. has ( dirPath ) ) {
@@ -1494,7 +1499,7 @@ function getCacheResolutions(state: BuilderProgramState, getCanonicalFileName: G
1494
1499
if ( automaticTypeDirectiveNames . length ) {
1495
1500
const currentDirectory = state . program ! . getCurrentDirectory ( ) ;
1496
1501
const containingPath = toPath ( state . program ! . getAutomaticTypeDirectiveContainingFile ( ) , currentDirectory , getCanonicalFileName ) ;
1497
- typeRefs = toPerDirectoryCache ( state , getCanonicalFileName , typeRefs , containingPath , state . program ! . getAutomaticTypeDirectiveResolutions ( ) ) ;
1502
+ typeRefs = toPerDirectoryCache ( state , getCanonicalFileName , typeRefs , getResolvedTypeReferenceDirectiveOfResolution , containingPath , state . program ! . getAutomaticTypeDirectiveResolutions ( ) ) ;
1498
1503
}
1499
1504
return state . cacheResolutions = {
1500
1505
modules,
@@ -1506,70 +1511,32 @@ function getCacheResolutions(state: BuilderProgramState, getCanonicalFileName: G
1506
1511
} ;
1507
1512
}
1508
1513
1509
- function toPerDirectoryCache (
1514
+ function toPerDirectoryCache < T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > (
1510
1515
state : BuilderProgramState ,
1511
1516
getCanonicalFileName : GetCanonicalFileName ,
1512
- cacheWithRedirects : CacheWithRedirects < Path , ModeAwareCache < ResolvedTypeReferenceDirectiveWithFailedLookupLocations > > | undefined ,
1513
- fOrPath : SourceFile | Path ,
1514
- cache : ModeAwareCache < ResolvedTypeReferenceDirectiveWithFailedLookupLocations > | undefined ,
1515
- ) : CacheWithRedirects < Path , ModeAwareCache < ResolvedTypeReferenceDirectiveWithFailedLookupLocations > > | undefined ;
1516
- function toPerDirectoryCache (
1517
- state : BuilderProgramState ,
1518
- getCanonicalFileName : GetCanonicalFileName ,
1519
- cacheWithRedirects : CacheWithRedirects < Path , ModeAwareCache < ResolvedModuleWithFailedLookupLocations > > | undefined ,
1520
- fOrPath : SourceFile | Path ,
1521
- cache : ModeAwareCache < ResolvedModuleWithFailedLookupLocations > | undefined ,
1522
- moduleNameToDirectoryMap : CacheWithRedirects < ModeAwareCacheKey , Map < Path , ResolvedModuleWithFailedLookupLocations > > ,
1523
- ) : CacheWithRedirects < Path , ModeAwareCache < ResolvedModuleWithFailedLookupLocations > > | undefined ;
1524
- function toPerDirectoryCache < T extends ResolvedModuleWithFailedLookupLocations | ResolvedTypeReferenceDirectiveWithFailedLookupLocations > (
1525
- state : BuilderProgramState ,
1526
- getCanonicalFileName : GetCanonicalFileName ,
1527
- cacheWithRedirects : CacheWithRedirects < Path , ModeAwareCache < T > > | undefined ,
1528
- fOrPath : SourceFile | Path ,
1529
- cache : ModeAwareCache < T > | undefined ,
1517
+ perDirCache : CacheWithRedirects < Path , ModeAwareCache < T > > | undefined ,
1518
+ getResolutionWithResolvedFileName : GetResolutionWithResolvedFileName < T , R > ,
1519
+ sourceFileOrPath : SourceFile | Path ,
1520
+ fileCacheFromProgram : ModeAwareCache < T > | undefined ,
1530
1521
moduleNameToDirectoryMap ?: CacheWithRedirects < ModeAwareCacheKey , Map < Path , ResolvedModuleWithFailedLookupLocations > > | undefined ,
1531
1522
) : CacheWithRedirects < Path , ModeAwareCache < T > > | undefined {
1532
- if ( ! cache ?. size ( ) ) return cacheWithRedirects ;
1533
- let dirPath : Path , redirectedReference : ResolvedProjectReference | undefined ;
1534
- if ( ! isString ( fOrPath ) ) {
1535
- redirectedReference = state . program ! . getRedirectReferenceForResolution ( fOrPath ) ;
1536
- dirPath = getDirectoryPath ( fOrPath . path ) ;
1537
- }
1538
- else {
1539
- dirPath = getDirectoryPath ( fOrPath ) ;
1540
- }
1541
- let perDirResolutionCache = cacheWithRedirects ?. getMapOfCacheRedirects ( redirectedReference ) ;
1542
- let dirCache = perDirResolutionCache ?. get ( dirPath ) ;
1543
- cache . forEach ( ( resolution , name , mode ) => {
1544
- if ( ! ( resolution as ResolvedModuleWithFailedLookupLocations ) . resolvedModule ?. resolvedFileName && ! ( resolution as ResolvedTypeReferenceDirectiveWithFailedLookupLocations ) . resolvedTypeReferenceDirective ?. resolvedFileName ) return ;
1545
- if ( dirCache ?. has ( name , mode ) ) return ;
1546
- // If there was already external module resolution that is same, set for child directory, dont set resolution for this directory
1547
- if ( moduleNameToDirectoryMap &&
1548
- ! isExternalModuleNameRelative ( name ) &&
1549
- moduleNameToDirectoryMap . getMapOfCacheRedirects ( redirectedReference ) ?. get ( createModeAwareCacheKey ( name , mode ) ) ?. get ( dirPath ) ) {
1550
- return ;
1551
- }
1552
- if ( ! dirCache ) {
1553
- perDirResolutionCache ??= ( cacheWithRedirects ??= createCacheWithRedirects ( state . compilerOptions ) ) . getOrCreateMapOfCacheRedirects ( redirectedReference ) ;
1554
- perDirResolutionCache . set ( dirPath , dirCache = createModeAwareCache ( ) ) ;
1555
- }
1556
- dirCache . set ( name , mode , resolution ) ;
1557
- if ( ! moduleNameToDirectoryMap || isExternalModuleNameRelative ( name ) ) return ;
1558
- // put result in per-module name cache and delete everything that is not needed
1559
- const actualModuleNameToDirectoryMap = moduleNameToDirectoryMap . getOrCreateMapOfCacheRedirects ( redirectedReference ) ;
1560
- const key = createModeAwareCacheKey ( name , mode ) ;
1561
- let directoryPathMap = actualModuleNameToDirectoryMap . get ( key ) ;
1562
- if ( ! directoryPathMap ) actualModuleNameToDirectoryMap . set ( key , directoryPathMap = new Map ( ) ) ;
1563
- moduleNameToDirectorySet (
1564
- directoryPathMap ,
1565
- dirPath ,
1566
- resolution as ResolvedModuleWithFailedLookupLocations ,
1567
- getResolvedFileNameForModuleNameToDirectorySet ,
1568
- dir => toPath ( dir , state . program ! . getCurrentDirectory ( ) , getCanonicalFileName ) ,
1569
- ancestorPath => perDirResolutionCache ?. get ( ancestorPath ) ?. delete ( name , mode )
1570
- ) ;
1571
- } ) ;
1572
- return cacheWithRedirects ;
1523
+ return toPerDirectoryResolution (
1524
+ state . program ! ,
1525
+ fileCacheFromProgram ,
1526
+ sourceFileOrPath ,
1527
+ perDirCache ,
1528
+ moduleNameToDirectoryMap ,
1529
+ noop ,
1530
+ ( r , name , mode , redirectedReference , dirPath ) =>
1531
+ // If this is not resolved, dont put in per dir Cache
1532
+ ! getResolutionWithResolvedFileName ( r ) ?. resolvedFileName ||
1533
+ // If there was already external module resolution that is same, set for child directory, dont set resolution for this directory
1534
+ ( moduleNameToDirectoryMap &&
1535
+ ! isExternalModuleNameRelative ( name ) &&
1536
+ moduleNameToDirectoryMap . getMapOfCacheRedirects ( redirectedReference ) ?. get ( createModeAwareCacheKey ( name , mode ) ) ?. get ( dirPath ) ) ,
1537
+ dir => toPath ( dir , state . program ! . getCurrentDirectory ( ) , getCanonicalFileName ) ,
1538
+ ( ancestorPath , name , mode , perDirResolutionCache ) => perDirResolutionCache ?. get ( ancestorPath ) ?. delete ( name , mode ) ,
1539
+ ) ;
1573
1540
}
1574
1541
1575
1542
/** @internal */
0 commit comments