diff --git a/packages/compiler-cli/src/ngtsc/core/src/host.ts b/packages/compiler-cli/src/ngtsc/core/src/host.ts index 24683b131b..1dfeba4f18 100644 --- a/packages/compiler-cli/src/ngtsc/core/src/host.ts +++ b/packages/compiler-cli/src/ngtsc/core/src/host.ts @@ -101,6 +101,12 @@ export class NgCompilerHost extends DelegatingCompilerHost implements this.constructionDiagnostics = diagnostics; this.inputFiles = [...inputFiles, ...shimAdapter.extraInputFiles]; this.rootDirs = rootDirs; + + if (this.resolveModuleNames === undefined) { + // In order to reuse the module resolution cache during the creation of the type-check + // program, we'll need to provide `resolveModuleNames` if the delegate did not provide one. + this.resolveModuleNames = this.createCachedResolveModuleNamesFunction(); + } } /** @@ -263,4 +269,17 @@ export class NgCompilerHost extends DelegatingCompilerHost implements get unifiedModulesHost(): UnifiedModulesHost|null { return this.fileNameToModuleName !== undefined ? this as UnifiedModulesHost : null; } + + private createCachedResolveModuleNamesFunction(): ts.CompilerHost['resolveModuleNames'] { + const moduleResolutionCache = ts.createModuleResolutionCache( + this.getCurrentDirectory(), this.getCanonicalFileName.bind(this)); + + return (moduleNames, containingFile, reusedNames, redirectedReference, options) => { + return moduleNames.map(moduleName => { + const module = ts.resolveModuleName( + moduleName, containingFile, options, this, moduleResolutionCache, redirectedReference); + return module.resolvedModule; + }); + }; + } }