Skip to content

Commit 16f90ca

Browse files
JoostKAndrewKushnir
authored andcommitted
perf(compiler-cli): ensure module resolution cache is reused for type-check program (#39693)
The Angular compiler creates two `ts.Program`s; one for emit and one for template type-checking. The creation of the type-check program could benefit from reusing the `ts.ModuleResolutionCache` that was primed during the creation of the emit program. This requires that the compiler host implements `resolveModuleNames`, as otherwise TypeScript will setup a `ts.ModuleResolutionHost` of its own for both programs. This commit ensures that `resolveModuleNames` is always implemented, even if the originally provided compiler host does not. This is beneficial for the `ngc` binary. PR Close #39693
1 parent e7ce857 commit 16f90ca

File tree

1 file changed

+19
-0
lines changed
  • packages/compiler-cli/src/ngtsc/core/src

1 file changed

+19
-0
lines changed

packages/compiler-cli/src/ngtsc/core/src/host.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ export class NgCompilerHost extends DelegatingCompilerHost implements
101101
this.constructionDiagnostics = diagnostics;
102102
this.inputFiles = [...inputFiles, ...shimAdapter.extraInputFiles];
103103
this.rootDirs = rootDirs;
104+
105+
if (this.resolveModuleNames === undefined) {
106+
// In order to reuse the module resolution cache during the creation of the type-check
107+
// program, we'll need to provide `resolveModuleNames` if the delegate did not provide one.
108+
this.resolveModuleNames = this.createCachedResolveModuleNamesFunction();
109+
}
104110
}
105111

106112
/**
@@ -263,4 +269,17 @@ export class NgCompilerHost extends DelegatingCompilerHost implements
263269
get unifiedModulesHost(): UnifiedModulesHost|null {
264270
return this.fileNameToModuleName !== undefined ? this as UnifiedModulesHost : null;
265271
}
272+
273+
private createCachedResolveModuleNamesFunction(): ts.CompilerHost['resolveModuleNames'] {
274+
const moduleResolutionCache = ts.createModuleResolutionCache(
275+
this.getCurrentDirectory(), this.getCanonicalFileName.bind(this));
276+
277+
return (moduleNames, containingFile, reusedNames, redirectedReference, options) => {
278+
return moduleNames.map(moduleName => {
279+
const module = ts.resolveModuleName(
280+
moduleName, containingFile, options, this, moduleResolutionCache, redirectedReference);
281+
return module.resolvedModule;
282+
});
283+
};
284+
}
266285
}

0 commit comments

Comments
 (0)