feat(ivy): enable re-export of the compilation scope of NgModules privately (#33177)
This commit refactors the aliasing system to support multiple different AliasingHost implementations, which control specific aliasing behavior in ngtsc (see the README.md). A new host is introduced, the `PrivateExportAliasingHost`. This solves a longstanding problem in ngtsc regarding support for "monorepo" style private libraries. These are libraries which are compiled separately from the main application, and depended upon through TypeScript path mappings. Such libraries are frequently not in the Angular Package Format and do not have entrypoints, but rather make use of deep import style module specifiers. This can cause issues with ngtsc's ability to import a directive given the module specifier of its NgModule. For example, if the application uses a directive `Foo` from such a library `foo`, the user might write: ```typescript import {FooModule} from 'foo/module'; ``` In this case, foo/module.d.ts is path-mapped into the program. Ordinarily the compiler would see this as an absolute module specifier, and assume that the `Foo` directive can be imported from the same specifier. For such non- APF libraries, this assumption fails. Really `Foo` should be imported from the file which declares it, but there are two problems with this: 1. The compiler would have to reverse the path mapping in order to determine a path-mapped path to the file (maybe foo/dir.d.ts). 2. There is no guarantee that the file containing the directive is path- mapped in the program at all. The compiler would effectively have to "guess" 'foo/dir' as a module specifier, which may or may not be accurate depending on how the library and path mapping are set up. It's strongly desirable that the compiler not break its current invariant that the module specifier given by the user for the NgModule is always the module specifier from which directives/pipes are imported. Thus, for any given NgModule from a particular module specifier, it must always be possible to import any directives/pipes from the same specifier, no matter how it's packaged. To make this possible, when compiling a file containing an NgModule, ngtsc will automatically add re-exports for any directives/pipes not yet exported by the user, with a name of the form: ɵngExportɵModuleNameɵDirectiveName This has several effects: 1. It guarantees anyone depending on the NgModule will be able to import its directives/pipes from the same specifier. 2. It maintains a stable name for the exported symbol that is safe to depend on from code on NPM. Effectively, this private exported name will be a part of the package's .d.ts API, and cannot be changed in a non-breaking fashion. Fixes #29361 FW-1610 #resolve PR Close #33177
This commit is contained in:

committed by
Matias Niemelä

parent
a86a179f45
commit
c4733c15c0
@ -18,7 +18,7 @@ import {CycleAnalyzer, ImportGraph} from './cycles';
|
||||
import {ErrorCode, ngErrorCode} from './diagnostics';
|
||||
import {FlatIndexGenerator, ReferenceGraph, checkForPrivateExports, findFlatIndexEntryPoint} from './entry_point';
|
||||
import {AbsoluteFsPath, LogicalFileSystem, absoluteFrom} from './file_system';
|
||||
import {AbsoluteModuleStrategy, AliasGenerator, AliasStrategy, DefaultImportTracker, FileToModuleHost, FileToModuleStrategy, ImportRewriter, LocalIdentifierStrategy, LogicalProjectStrategy, ModuleResolver, NoopImportRewriter, R3SymbolsImportRewriter, Reference, ReferenceEmitter} from './imports';
|
||||
import {AbsoluteModuleStrategy, AliasStrategy, AliasingHost, DefaultImportTracker, FileToModuleAliasingHost, FileToModuleHost, FileToModuleStrategy, ImportRewriter, LocalIdentifierStrategy, LogicalProjectStrategy, ModuleResolver, NoopImportRewriter, PrivateExportAliasingHost, R3SymbolsImportRewriter, Reference, ReferenceEmitter} from './imports';
|
||||
import {IncrementalState} from './incremental';
|
||||
import {IndexedComponent, IndexingContext} from './indexer';
|
||||
import {generateAnalysis} from './indexer/src/transform';
|
||||
@ -60,6 +60,7 @@ export class NgtscProgram implements api.Program {
|
||||
private cycleAnalyzer: CycleAnalyzer;
|
||||
private metaReader: MetadataReader|null = null;
|
||||
|
||||
private aliasingHost: AliasingHost|null = null;
|
||||
private refEmitter: ReferenceEmitter|null = null;
|
||||
private fileToModuleHost: FileToModuleHost|null = null;
|
||||
private defaultImportTracker: DefaultImportTracker;
|
||||
@ -351,6 +352,10 @@ export class NgtscProgram implements api.Program {
|
||||
declarationTransformFactory(compilation),
|
||||
];
|
||||
|
||||
// Only add aliasing re-exports to the .d.ts output if the `AliasingHost` requests it.
|
||||
if (this.aliasingHost !== null && this.aliasingHost.aliasExportsInDts) {
|
||||
afterDeclarationsTransforms.push(aliasTransformFactory(compilation.exportStatements));
|
||||
}
|
||||
|
||||
if (this.factoryToSourceInfo !== null) {
|
||||
beforeTransforms.push(
|
||||
@ -475,7 +480,6 @@ export class NgtscProgram implements api.Program {
|
||||
private makeCompilation(): IvyCompilation {
|
||||
const checker = this.tsProgram.getTypeChecker();
|
||||
|
||||
let aliasGenerator: AliasGenerator|null = null;
|
||||
// Construct the ReferenceEmitter.
|
||||
if (this.fileToModuleHost === null || !this.options._useHostForImportGeneration) {
|
||||
// The CompilerHost doesn't have fileNameToModuleName, so build an NPM-centric reference
|
||||
@ -491,6 +495,15 @@ export class NgtscProgram implements api.Program {
|
||||
// an error.
|
||||
new LogicalProjectStrategy(this.reflector, new LogicalFileSystem(this.rootDirs)),
|
||||
]);
|
||||
|
||||
// If an entrypoint is present, then all user imports should be directed through the
|
||||
// entrypoint and private exports are not needed. The compiler will validate that all publicly
|
||||
// visible directives/pipes are importable via this entrypoint.
|
||||
if (this.entryPoint === null && this.options.generateDeepReexports === true) {
|
||||
// No entrypoint is present and deep re-exports were requested, so configure the aliasing
|
||||
// system to generate them.
|
||||
this.aliasingHost = new PrivateExportAliasingHost(this.reflector);
|
||||
}
|
||||
} else {
|
||||
// The CompilerHost supports fileNameToModuleName, so use that to emit imports.
|
||||
this.refEmitter = new ReferenceEmitter([
|
||||
@ -501,16 +514,16 @@ export class NgtscProgram implements api.Program {
|
||||
// Then use fileNameToModuleName to emit imports.
|
||||
new FileToModuleStrategy(this.reflector, this.fileToModuleHost),
|
||||
]);
|
||||
aliasGenerator = new AliasGenerator(this.fileToModuleHost);
|
||||
this.aliasingHost = new FileToModuleAliasingHost(this.fileToModuleHost);
|
||||
}
|
||||
|
||||
const evaluator = new PartialEvaluator(this.reflector, checker, this.incrementalState);
|
||||
const dtsReader = new DtsMetadataReader(checker, this.reflector);
|
||||
const localMetaRegistry = new LocalMetadataRegistry();
|
||||
const localMetaReader = new CompoundMetadataReader([localMetaRegistry, this.incrementalState]);
|
||||
const depScopeReader = new MetadataDtsModuleScopeResolver(dtsReader, aliasGenerator);
|
||||
const depScopeReader = new MetadataDtsModuleScopeResolver(dtsReader, this.aliasingHost);
|
||||
const scopeRegistry = new LocalModuleScopeRegistry(
|
||||
localMetaReader, depScopeReader, this.refEmitter, aliasGenerator, this.incrementalState);
|
||||
localMetaReader, depScopeReader, this.refEmitter, this.aliasingHost, this.incrementalState);
|
||||
const scopeReader = new CompoundComponentScopeReader([scopeRegistry, this.incrementalState]);
|
||||
const metaRegistry =
|
||||
new CompoundMetadataRegistry([localMetaRegistry, scopeRegistry, this.incrementalState]);
|
||||
|
Reference in New Issue
Block a user