Skip to content

Commit 77da0de

Browse files
committed
Factor out cache to be shared and not shared. Projects still are not sharing caches
TODO: - Actual sharing between projects - Handling global cache passes between projects - Effective typeRoots differ and hence type ref resolution differs
1 parent 1f90a3f commit 77da0de

180 files changed

Lines changed: 2319 additions & 17750 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/compiler/_namespaces/ts.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export * from "../builderStatePublic.js";
6565
export * from "../builderState.js";
6666
export * from "../builder.js";
6767
export * from "../builderPublic.js";
68+
export * from "../sharedResolutionCache.js";
6869
export * from "../resolutionCache.js";
6970
export * from "../watch.js";
7071
export * from "../watchPublic.js";

src/compiler/moduleNameResolver.ts

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -636,8 +636,7 @@ export function resolveTypeReferenceDirective(typeReferenceDirectiveName: string
636636
resolutionDiagnostics: initializeResolutionField(diagnostics),
637637
};
638638
if (containingDirectory) {
639-
setPerDirectoryAndNonRelativeNameCacheResult(
640-
cache,
639+
cache?.setPerDirectoryAndNonRelativeNameCacheResult(
641640
typeReferenceDirectiveName,
642641
resolutionMode,
643642
containingDirectory,
@@ -1363,6 +1362,14 @@ function createNonRelativeNameResolutionCache<T>(
13631362
export interface ModuleOrTypeReferenceResolutionCache<T> extends PerDirectoryResolutionCache<T>, NonRelativeNameResolutionCache<T>, PackageJsonInfoCache {
13641363
getPackageJsonInfoCache(): PackageJsonInfoCache;
13651364
compact(availableOptions?: Set<CompilerOptions>, skipOptionsToRedirectsKeyCleanup?: boolean): void;
1365+
setPerDirectoryAndNonRelativeNameCacheResult(
1366+
name: string,
1367+
mode: ResolutionMode,
1368+
directoryName: string,
1369+
redirectedReference: ResolvedProjectReference | undefined,
1370+
result: T,
1371+
): void;
1372+
options: () => CompilerOptions;
13661373
optionsToRedirectsKey: Map<CompilerOptions, RedirectsCacheKey>;
13671374
print(): void;
13681375
}
@@ -1394,27 +1401,31 @@ function createModuleOrTypeReferenceResolutionCache<T>(
13941401
);
13951402
packageJsonInfoCache ??= createPackageJsonInfoCache(currentDirectory, getCanonicalFileName);
13961403

1397-
return {
1404+
const cache: ModuleOrTypeReferenceResolutionCache<T> = {
13981405
...packageJsonInfoCache,
13991406
...perDirectoryResolutionCache,
14001407
...nonRelativeNameResolutionCache,
14011408
clear,
14021409
update,
14031410
compact,
1411+
setPerDirectoryAndNonRelativeNameCacheResult,
1412+
options: () => options!,
14041413
getPackageJsonInfoCache: () => packageJsonInfoCache,
14051414
optionsToRedirectsKey,
14061415
print,
14071416
};
1417+
return cache;
14081418

14091419
function clear() {
14101420
perDirectoryResolutionCache.clear();
14111421
nonRelativeNameResolutionCache.clear();
14121422
packageJsonInfoCache!.clear();
14131423
}
14141424

1415-
function update(options: CompilerOptions) {
1416-
perDirectoryResolutionCache.update(options);
1417-
nonRelativeNameResolutionCache.update(options);
1425+
function update(updatedOptions: CompilerOptions) {
1426+
options = updatedOptions;
1427+
perDirectoryResolutionCache.update(updatedOptions);
1428+
nonRelativeNameResolutionCache.update(updatedOptions);
14181429
}
14191430

14201431
function compact(availableOptions = new Set(optionsToRedirectsKey!.keys()), skipOptionsToRedirectsKeyCleanup?: boolean) {
@@ -1429,6 +1440,21 @@ function createModuleOrTypeReferenceResolutionCache<T>(
14291440
}
14301441
}
14311442

1443+
function setPerDirectoryAndNonRelativeNameCacheResult(
1444+
name: string,
1445+
mode: ResolutionMode,
1446+
directoryName: string,
1447+
redirectedReference: ResolvedProjectReference | undefined,
1448+
result: T,
1449+
): void {
1450+
if (cache.isReadonly) return;
1451+
cache.getOrCreateCacheForDirectory(directoryName, redirectedReference).set(name, mode, result);
1452+
if (!isExternalModuleNameRelative(name)) {
1453+
// put result in per-module name cache
1454+
cache.getOrCreateCacheForNonRelativeName(name, mode, redirectedReference).set(directoryName, result);
1455+
}
1456+
}
1457+
14321458
function print() {
14331459
console.log(`directoryToModuleNameMap::`);
14341460
perDirectoryResolutionCache.directoryToModuleNameMap.forEach((moduleNameMap, directoryPath, redirectsCacheKey) => {
@@ -1451,24 +1477,6 @@ function createModuleOrTypeReferenceResolutionCache<T>(
14511477
}
14521478
}
14531479

1454-
/** @internal */
1455-
export function setPerDirectoryAndNonRelativeNameCacheResult<T>(
1456-
cache: ModuleOrTypeReferenceResolutionCache<T> | undefined,
1457-
name: string,
1458-
mode: ResolutionMode,
1459-
directoryName: string,
1460-
redirectedReference: ResolvedProjectReference | undefined,
1461-
result: T,
1462-
): void {
1463-
if (cache && !cache.isReadonly) {
1464-
cache.getOrCreateCacheForDirectory(directoryName, redirectedReference).set(name, mode, result);
1465-
if (!isExternalModuleNameRelative(name)) {
1466-
// put result in per-module name cache
1467-
cache.getOrCreateCacheForNonRelativeName(name, mode, redirectedReference).set(directoryName, result);
1468-
}
1469-
}
1470-
}
1471-
14721480
export function createModuleResolutionCache(
14731481
currentDirectory: string,
14741482
getCanonicalFileName: (s: string) => string,
@@ -1544,8 +1552,8 @@ export function createTypeReferenceDirectiveResolutionCache(
15441552
}
15451553

15461554
/** @internal */
1547-
export function getOptionsForLibraryResolution(options: CompilerOptions): CompilerOptions {
1548-
return { moduleResolution: ModuleResolutionKind.Node10, traceResolution: options.traceResolution };
1555+
export function getOptionsForLibraryResolution(options: CompilerOptions | undefined): CompilerOptions {
1556+
return { moduleResolution: ModuleResolutionKind.Node10, traceResolution: options?.traceResolution };
15491557
}
15501558

15511559
/** @internal */
@@ -1611,8 +1619,7 @@ export function resolveModuleName(moduleName: string, containingFile: string, co
16111619
return Debug.fail(`Unexpected moduleResolution: ${moduleResolution}`);
16121620
}
16131621

1614-
setPerDirectoryAndNonRelativeNameCacheResult(
1615-
cache,
1622+
cache?.setPerDirectoryAndNonRelativeNameCacheResult(
16161623
moduleName,
16171624
resolutionMode,
16181625
containingDirectory,

0 commit comments

Comments
 (0)