feat(ivy): use fileNameToModuleName to emit imports when it's available (#28523)
The ultimate goal of this commit is to make use of fileNameToModuleName to get the module specifier to use when generating an import, when that API is available in the CompilerHost that ngtsc is created with. As part of getting there, the way in which ngtsc tracks references and generates import module specifiers is refactored considerably. References are tracked with the Reference class, and previously ngtsc had several different kinds of Reference. An AbsoluteReference represented a declaration which needed to be imported via an absolute module specifier tracked in the AbsoluteReference, and a RelativeReference represented a declaration from the local program, imported via relative path or referred to directly by identifier if possible. Thus, how to refer to a particular declaration was encoded into the Reference type _at the time of creation of the Reference_. This commit refactors that logic and reduces Reference to a single class with no subclasses. A Reference represents a node being referenced, plus context about how the node was located. This context includes a "bestGuessOwningModule", the compiler's best guess at which absolute module specifier has defined this reference. For example, if the compiler arrives at the declaration of CommonModule via an import to @angular/common, then any references obtained from CommonModule (e.g. NgIf) will also be considered to be owned by @angular/common. A ReferenceEmitter class and accompanying ReferenceEmitStrategy interface are introduced. To produce an Expression referring to a given Reference'd node, the ReferenceEmitter consults a sequence of ReferenceEmitStrategy implementations. Several different strategies are defined: - LocalIdentifierStrategy: use local ts.Identifiers if available. - AbsoluteModuleStrategy: if the Reference has a bestGuessOwningModule, import the node via an absolute import from that module specifier. - LogicalProjectStrategy: if the Reference is in the logical project (is under the project rootDirs), import the node via a relative import. - FileToModuleStrategy: use a FileToModuleHost to generate the module specifier by which to import the node. Depending on the availability of fileNameToModuleName in the CompilerHost, then, a different collection of these strategies is used for compilation. PR Close #28523
This commit is contained in:

committed by
Misko Hevery

parent
a529f53031
commit
423b39e216
@ -17,8 +17,10 @@ import {BaseDefDecoratorHandler} from './annotations/src/base_def';
|
||||
import {CycleAnalyzer, ImportGraph} from './cycles';
|
||||
import {ErrorCode, ngErrorCode} from './diagnostics';
|
||||
import {FlatIndexGenerator, ReferenceGraph, checkForPrivateExports, findFlatIndexEntryPoint} from './entry_point';
|
||||
import {ImportRewriter, ModuleResolver, NoopImportRewriter, R3SymbolsImportRewriter, Reference, TsReferenceResolver} from './imports';
|
||||
import {AbsoluteModuleStrategy, FileToModuleHost, ImportRewriter, LocalIdentifierStrategy, LogicalProjectStrategy, ModuleResolver, NoopImportRewriter, R3SymbolsImportRewriter, Reference, ReferenceEmitter} from './imports';
|
||||
import {FileToModuleStrategy} from './imports/src/emitter';
|
||||
import {PartialEvaluator} from './partial_evaluator';
|
||||
import {AbsoluteFsPath, LogicalFileSystem} from './path';
|
||||
import {TypeScriptReflectionHost} from './reflection';
|
||||
import {HostResourceLoader} from './resource_loader';
|
||||
import {NgModuleRouteAnalyzer, entryPointKeyFor} from './routing';
|
||||
@ -27,7 +29,7 @@ import {ivySwitchTransform} from './switch';
|
||||
import {IvyCompilation, declarationTransformFactory, ivyTransformFactory} from './transform';
|
||||
import {TypeCheckContext, TypeCheckProgramHost} from './typecheck';
|
||||
import {normalizeSeparators} from './util/src/path';
|
||||
import {isDtsPath} from './util/src/typescript';
|
||||
import {getRootDirs, isDtsPath} from './util/src/typescript';
|
||||
|
||||
export class NgtscProgram implements api.Program {
|
||||
private tsProgram: ts.Program;
|
||||
@ -40,7 +42,7 @@ export class NgtscProgram implements api.Program {
|
||||
private _importRewriter: ImportRewriter|undefined = undefined;
|
||||
private _reflector: TypeScriptReflectionHost|undefined = undefined;
|
||||
private _isCore: boolean|undefined = undefined;
|
||||
private rootDirs: string[];
|
||||
private rootDirs: AbsoluteFsPath[];
|
||||
private closureCompilerEnabled: boolean;
|
||||
private entryPoint: ts.SourceFile|null;
|
||||
private exportReferenceGraph: ReferenceGraph|null = null;
|
||||
@ -51,22 +53,20 @@ export class NgtscProgram implements api.Program {
|
||||
private moduleResolver: ModuleResolver;
|
||||
private cycleAnalyzer: CycleAnalyzer;
|
||||
|
||||
private refEmitter: ReferenceEmitter|null = null;
|
||||
private fileToModuleHost: FileToModuleHost|null = null;
|
||||
|
||||
constructor(
|
||||
rootNames: ReadonlyArray<string>, private options: api.CompilerOptions,
|
||||
host: api.CompilerHost, oldProgram?: api.Program) {
|
||||
this.rootDirs = [];
|
||||
if (options.rootDirs !== undefined) {
|
||||
this.rootDirs.push(...options.rootDirs);
|
||||
} else if (options.rootDir !== undefined) {
|
||||
this.rootDirs.push(options.rootDir);
|
||||
} else {
|
||||
this.rootDirs.push(host.getCurrentDirectory());
|
||||
}
|
||||
this.rootDirs = getRootDirs(host, options);
|
||||
this.closureCompilerEnabled = !!options.annotateForClosureCompiler;
|
||||
this.resourceManager = new HostResourceLoader(host, options);
|
||||
const shouldGenerateShims = options.allowEmptyCodegenFiles || false;
|
||||
this.host = host;
|
||||
if (host.fileNameToModuleName !== undefined) {
|
||||
this.fileToModuleHost = host as FileToModuleHost;
|
||||
}
|
||||
let rootFiles = [...rootNames];
|
||||
|
||||
const generators: ShimGenerator[] = [];
|
||||
@ -168,7 +168,7 @@ export class NgtscProgram implements api.Program {
|
||||
const compilation = this.ensureAnalyzed();
|
||||
const diagnostics = [...compilation.diagnostics];
|
||||
if (!!this.options.fullTemplateTypeCheck) {
|
||||
const ctx = new TypeCheckContext();
|
||||
const ctx = new TypeCheckContext(this.refEmitter !);
|
||||
compilation.typeCheck(ctx);
|
||||
diagnostics.push(...this.compileTypeCheckProgram(ctx));
|
||||
}
|
||||
@ -315,9 +315,32 @@ export class NgtscProgram implements api.Program {
|
||||
|
||||
private makeCompilation(): IvyCompilation {
|
||||
const checker = this.tsProgram.getTypeChecker();
|
||||
const refResolver = new TsReferenceResolver(this.tsProgram, checker, this.options, this.host);
|
||||
const evaluator = new PartialEvaluator(this.reflector, checker, refResolver);
|
||||
const scopeRegistry = new SelectorScopeRegistry(checker, this.reflector, refResolver);
|
||||
// Construct the ReferenceEmitter.
|
||||
if (this.fileToModuleHost === null || !this.options._useHostForImportGeneration) {
|
||||
// The CompilerHost doesn't have fileNameToModuleName, so build an NPM-centric reference
|
||||
// resolution strategy.
|
||||
this.refEmitter = new ReferenceEmitter([
|
||||
// First, try to use local identifiers if available.
|
||||
new LocalIdentifierStrategy(),
|
||||
// Next, attempt to use an absolute import.
|
||||
new AbsoluteModuleStrategy(this.tsProgram, checker, this.options, this.host),
|
||||
// Finally, check if the reference is being written into a file within the project's logical
|
||||
// file system, and use a relative import if so. If this fails, ReferenceEmitter will throw
|
||||
// an error.
|
||||
new LogicalProjectStrategy(checker, new LogicalFileSystem(this.rootDirs)),
|
||||
]);
|
||||
} else {
|
||||
// The CompilerHost supports fileNameToModuleName, so use that to emit imports.
|
||||
this.refEmitter = new ReferenceEmitter([
|
||||
// First, try to use local identifiers if available.
|
||||
new LocalIdentifierStrategy(),
|
||||
// Then use fileNameToModuleName to emit imports.
|
||||
new FileToModuleStrategy(checker, this.fileToModuleHost),
|
||||
]);
|
||||
}
|
||||
|
||||
const evaluator = new PartialEvaluator(this.reflector, checker);
|
||||
const scopeRegistry = new SelectorScopeRegistry(checker, this.reflector, this.refEmitter);
|
||||
|
||||
// If a flat module entrypoint was specified, then track references via a `ReferenceGraph` in
|
||||
// order to produce proper diagnostics for incorrectly exported directives/pipes/etc. If there
|
||||
@ -344,7 +367,7 @@ export class NgtscProgram implements api.Program {
|
||||
this.reflector, this.isCore, this.options.strictInjectionParameters || false),
|
||||
new NgModuleDecoratorHandler(
|
||||
this.reflector, evaluator, scopeRegistry, referencesRegistry, this.isCore,
|
||||
this.routeAnalyzer),
|
||||
this.routeAnalyzer, this.refEmitter),
|
||||
new PipeDecoratorHandler(this.reflector, evaluator, scopeRegistry, this.isCore),
|
||||
];
|
||||
|
||||
|
Reference in New Issue
Block a user