feat(ivy): register NgModules with ids when compiled with AOT (#29980)

This commit adds registration of AOT compiled NgModules that have 'id'
properties set in their metadata. Such modules have a call to
registerNgModuleType() emitted as part of compilation.

The JIT behavior of this code is already in place.

This is required for module loading systems (such as g3) which rely on
getModuleFactory().

PR Close #29980
This commit is contained in:
Alex Rickabaugh
2019-04-18 16:22:53 -07:00
committed by Ben Lesh
parent 4229b41057
commit 0df719a461
5 changed files with 46 additions and 3 deletions

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {Expression, ExternalExpr, InvokeFunctionExpr, LiteralArrayExpr, R3Identifiers, R3InjectorMetadata, R3NgModuleMetadata, R3Reference, Statement, WrappedNodeExpr, compileInjector, compileNgModule} from '@angular/compiler';
import {Expression, ExternalExpr, InvokeFunctionExpr, LiteralArrayExpr, LiteralExpr, R3Identifiers, R3InjectorMetadata, R3NgModuleMetadata, R3Reference, Statement, WrappedNodeExpr, compileInjector, compileNgModule} from '@angular/compiler';
import * as ts from 'typescript';
import {ErrorCode, FatalDiagnosticError} from '../../diagnostics';
@ -28,6 +28,7 @@ export interface NgModuleAnalysis {
metadataStmt: Statement|null;
declarations: Reference<ClassDeclaration>[];
exports: Reference<ClassDeclaration>[];
id: string|null;
}
/**
@ -119,6 +120,17 @@ export class NgModuleDecoratorHandler implements DecoratorHandler<NgModuleAnalys
bootstrapRefs = this.resolveTypeList(expr, bootstrapMeta, name, 'bootstrap');
}
let id: string|null = null;
if (ngModule.has('id')) {
const expr = ngModule.get('id') !;
const value = this.evaluator.evaluate(expr);
if (typeof value !== 'string') {
throw new FatalDiagnosticError(
ErrorCode.VALUE_HAS_WRONG_TYPE, expr, `NgModule.id must be a string`);
}
id = value;
}
// Register this module's information with the LocalModuleScopeRegistry. This ensures that
// during the compile() phase, the module's metadata is available for selector scope
// computation.
@ -184,6 +196,7 @@ export class NgModuleDecoratorHandler implements DecoratorHandler<NgModuleAnalys
return {
analysis: {
id,
ngModuleDef,
ngInjectorDef,
declarations: declarationRefs,
@ -247,6 +260,12 @@ export class NgModuleDecoratorHandler implements DecoratorHandler<NgModuleAnalys
ngModuleStatements.push(callExpr.toStmt());
}
}
if (analysis.id !== null) {
const registerNgModuleType = new ExternalExpr(R3Identifiers.registerNgModuleType);
const callExpr = registerNgModuleType.callFn(
[new LiteralExpr(analysis.id), new WrappedNodeExpr(node.name)]);
ngModuleStatements.push(callExpr.toStmt());
}
return [
{
name: 'ngModuleDef',