fix(ivy): properly compile NgModules with forward referenced types (#29198)
Previously, ngtsc would resolve forward references while evaluating the bootstrap, declaration, imports, and exports fields of NgModule types. However, when generating the resulting ngModuleDef, the forward nature of these references was not taken into consideration, and so the generated JS code would incorrectly reference types not yet declared. This commit fixes this issue by introducing function closures in the NgModuleDef type, similarly to how NgComponentDef uses them for forward declarations of its directives and pipes arrays. ngtsc will then generate closures when required, and the runtime will unwrap them if present. PR Close #29198
This commit is contained in:

committed by
Kara Erickson

parent
1625d86178
commit
73da2792c9
@ -19,7 +19,7 @@ import {getComponentDef, getDirectiveDef, getNgModuleDef, getPipeDef} from '../d
|
||||
import {NG_COMPONENT_DEF, NG_DIRECTIVE_DEF, NG_MODULE_DEF, NG_PIPE_DEF} from '../fields';
|
||||
import {ComponentDef} from '../interfaces/definition';
|
||||
import {NgModuleType} from '../ng_module_ref';
|
||||
import {renderStringify} from '../util/misc_utils';
|
||||
import {maybeUnwrapFn, renderStringify} from '../util/misc_utils';
|
||||
|
||||
import {angularCoreEnv} from './environment';
|
||||
|
||||
@ -156,14 +156,17 @@ function verifySemanticsOfNgModuleDef(moduleType: NgModuleType): void {
|
||||
moduleType = resolveForwardRef(moduleType);
|
||||
const ngModuleDef = getNgModuleDef(moduleType, true);
|
||||
const errors: string[] = [];
|
||||
ngModuleDef.declarations.forEach(verifyDeclarationsHaveDefinitions);
|
||||
const declarations = maybeUnwrapFn(ngModuleDef.declarations);
|
||||
const imports = maybeUnwrapFn(ngModuleDef.imports);
|
||||
const exports = maybeUnwrapFn(ngModuleDef.exports);
|
||||
declarations.forEach(verifyDeclarationsHaveDefinitions);
|
||||
const combinedDeclarations: Type<any>[] = [
|
||||
...ngModuleDef.declarations.map(resolveForwardRef), //
|
||||
...flatten(ngModuleDef.imports.map(computeCombinedExports), resolveForwardRef),
|
||||
...declarations.map(resolveForwardRef), //
|
||||
...flatten(imports.map(computeCombinedExports), resolveForwardRef),
|
||||
];
|
||||
ngModuleDef.exports.forEach(verifyExportsAreDeclaredOrReExported);
|
||||
ngModuleDef.declarations.forEach(verifyDeclarationIsUnique);
|
||||
ngModuleDef.declarations.forEach(verifyComponentEntryComponentsIsPartOfNgModule);
|
||||
exports.forEach(verifyExportsAreDeclaredOrReExported);
|
||||
declarations.forEach(verifyDeclarationIsUnique);
|
||||
declarations.forEach(verifyComponentEntryComponentsIsPartOfNgModule);
|
||||
|
||||
const ngModule = getAnnotation<NgModule>(moduleType, 'NgModule');
|
||||
if (ngModule) {
|
||||
@ -297,15 +300,14 @@ export function resetCompiledComponents(): void {
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the combined declarations of explicit declarations, as well as declarations inherited
|
||||
* by
|
||||
* Computes the combined declarations of explicit declarations, as well as declarations inherited by
|
||||
* traversing the exports of imported modules.
|
||||
* @param type
|
||||
*/
|
||||
function computeCombinedExports(type: Type<any>): Type<any>[] {
|
||||
type = resolveForwardRef(type);
|
||||
const ngModuleDef = getNgModuleDef(type, true);
|
||||
return [...flatten(ngModuleDef.exports.map((type) => {
|
||||
return [...flatten(maybeUnwrapFn(ngModuleDef.exports).map((type) => {
|
||||
const ngModuleDef = getNgModuleDef(type);
|
||||
if (ngModuleDef) {
|
||||
verifySemanticsOfNgModuleDef(type as any as NgModuleType);
|
||||
@ -388,7 +390,7 @@ export function transitiveScopesFor<T>(
|
||||
},
|
||||
};
|
||||
|
||||
def.declarations.forEach(declared => {
|
||||
maybeUnwrapFn(def.declarations).forEach(declared => {
|
||||
const declaredWithDefs = declared as Type<any>& { ngPipeDef?: any; };
|
||||
|
||||
if (getPipeDef(declaredWithDefs)) {
|
||||
@ -401,7 +403,7 @@ export function transitiveScopesFor<T>(
|
||||
}
|
||||
});
|
||||
|
||||
def.imports.forEach(<I>(imported: Type<I>) => {
|
||||
maybeUnwrapFn(def.imports).forEach(<I>(imported: Type<I>) => {
|
||||
const importedType = imported as Type<I>& {
|
||||
// If imported is an @NgModule:
|
||||
ngModuleDef?: NgModuleDef<I>;
|
||||
@ -422,7 +424,7 @@ export function transitiveScopesFor<T>(
|
||||
importedScope.exported.pipes.forEach(entry => scopes.compilation.pipes.add(entry));
|
||||
});
|
||||
|
||||
def.exports.forEach(<E>(exported: Type<E>) => {
|
||||
maybeUnwrapFn(def.exports).forEach(<E>(exported: Type<E>) => {
|
||||
const exportedType = exported as Type<E>& {
|
||||
// Components, Directives, NgModules, and Pipes can all be exported.
|
||||
ngComponentDef?: any;
|
||||
|
Reference in New Issue
Block a user