From 19a2b783cfe2a992f78531ce3b1b510014b868bf Mon Sep 17 00:00:00 2001 From: Alex Rickabaugh Date: Fri, 16 Nov 2018 17:01:56 +0100 Subject: [PATCH] feat(ivy): create a ModuleResolver to map module paths to files (#27697) PR Close #27697 --- .../compiler-cli/src/ngtsc/imports/index.ts | 2 +- .../src/ngtsc/imports/src/resolver.ts | 22 +++++++++++++++++++ packages/compiler-cli/src/ngtsc/program.ts | 4 +++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/packages/compiler-cli/src/ngtsc/imports/index.ts b/packages/compiler-cli/src/ngtsc/imports/index.ts index 275566d131..386775da9d 100644 --- a/packages/compiler-cli/src/ngtsc/imports/index.ts +++ b/packages/compiler-cli/src/ngtsc/imports/index.ts @@ -8,4 +8,4 @@ export {ImportRewriter, NoopImportRewriter, R3SymbolsImportRewriter, validateAndRewriteCoreSymbol} from './src/core'; export {AbsoluteReference, ImportMode, NodeReference, Reference, ResolvedReference} from './src/references'; -export {ReferenceResolver, TsReferenceResolver} from './src/resolver'; +export {ModuleResolver, ReferenceResolver, TsReferenceResolver} from './src/resolver'; diff --git a/packages/compiler-cli/src/ngtsc/imports/src/resolver.ts b/packages/compiler-cli/src/ngtsc/imports/src/resolver.ts index a4ec2bfecb..fbca9427ec 100644 --- a/packages/compiler-cli/src/ngtsc/imports/src/resolver.ts +++ b/packages/compiler-cli/src/ngtsc/imports/src/resolver.ts @@ -17,6 +17,28 @@ export interface ReferenceResolver { Reference; } +/** + * Used by `RouterEntryPointManager` and `NgModuleRouteAnalyzer` (which is in turn is used by + * `NgModuleDecoratorHandler`) for resolving the module source-files references in lazy-loaded + * routes (relative to the source-file containing the `NgModule` that provides the route + * definitions). + */ +export class ModuleResolver { + constructor( + private program: ts.Program, private compilerOptions: ts.CompilerOptions, + private host: ts.CompilerHost) {} + + resolveModuleName(module: string, containingFile: ts.SourceFile): ts.SourceFile|null { + const resolved = + ts.resolveModuleName(module, containingFile.fileName, this.compilerOptions, this.host) + .resolvedModule; + if (resolved === undefined) { + return null; + } + return this.program.getSourceFile(resolved.resolvedFileName) || null; + } +} + export class TsReferenceResolver implements ReferenceResolver { private moduleExportsCache = new Map|null>(); diff --git a/packages/compiler-cli/src/ngtsc/program.ts b/packages/compiler-cli/src/ngtsc/program.ts index 30926d17aa..fbeef8f77b 100644 --- a/packages/compiler-cli/src/ngtsc/program.ts +++ b/packages/compiler-cli/src/ngtsc/program.ts @@ -16,7 +16,7 @@ import {ComponentDecoratorHandler, DirectiveDecoratorHandler, InjectableDecorato import {BaseDefDecoratorHandler} from './annotations/src/base_def'; import {ErrorCode, ngErrorCode} from './diagnostics'; import {FlatIndexGenerator, ReferenceGraph, checkForPrivateExports, findFlatIndexEntryPoint} from './entry_point'; -import {ImportRewriter, NoopImportRewriter, R3SymbolsImportRewriter, Reference, TsReferenceResolver} from './imports'; +import {ImportRewriter, ModuleResolver, NoopImportRewriter, R3SymbolsImportRewriter, Reference, TsReferenceResolver} from './imports'; import {PartialEvaluator} from './partial_evaluator'; import {TypeScriptReflectionHost} from './reflection'; import {HostResourceLoader} from './resource_loader'; @@ -45,6 +45,7 @@ export class NgtscProgram implements api.Program { private flatIndexGenerator: FlatIndexGenerator|null = null; private constructionDiagnostics: ts.Diagnostic[] = []; + private moduleResolver: ModuleResolver; constructor( @@ -124,6 +125,7 @@ export class NgtscProgram implements api.Program { ts.createProgram(rootFiles, options, this.host, oldProgram && oldProgram.getTsProgram()); this.entryPoint = entryPoint !== null ? this.tsProgram.getSourceFile(entryPoint) || null : null; + this.moduleResolver = new ModuleResolver(this.tsProgram, options, this.host); } getTsProgram(): ts.Program { return this.tsProgram; }