From 0df719a4614dbe60c3bc6375a63ca95e340babba Mon Sep 17 00:00:00 2001 From: Alex Rickabaugh Date: Thu, 18 Apr 2019 16:22:53 -0700 Subject: [PATCH] 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 --- .../src/ngtsc/annotations/src/ng_module.ts | 21 ++++++++++++++++++- .../compiler-cli/test/ngtsc/ngtsc_spec.ts | 15 +++++++++++++ .../compiler/src/render3/r3_identifiers.ts | 3 +++ .../core/src/core_render3_private_export.ts | 5 ++++- packages/core/src/render3/jit/environment.ts | 5 ++++- 5 files changed, 46 insertions(+), 3 deletions(-) diff --git a/packages/compiler-cli/src/ngtsc/annotations/src/ng_module.ts b/packages/compiler-cli/src/ngtsc/annotations/src/ng_module.ts index f82c2db834..e558ed578a 100644 --- a/packages/compiler-cli/src/ngtsc/annotations/src/ng_module.ts +++ b/packages/compiler-cli/src/ngtsc/annotations/src/ng_module.ts @@ -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[]; exports: Reference[]; + id: string|null; } /** @@ -119,6 +120,17 @@ export class NgModuleDecoratorHandler implements DecoratorHandler { expect(jsContents).not.toContain('\u0275\u0275setNgModuleScope(TestModule,'); }); + it('should emit a \u0275registerNgModuleType call when the module has an id', () => { + env.tsconfig(); + env.write('test.ts', ` + import {NgModule} from '@angular/core'; + + @NgModule({id: 'test'}) + export class TestModule {} + `); + + env.driveMain(); + + const jsContents = env.getContents('test.js'); + expect(jsContents).toContain('i0.\u0275registerNgModuleType("test", TestModule);'); + }); + it('should filter out directives and pipes from module exports in the injector def', () => { env.tsconfig(); env.write('test.ts', ` diff --git a/packages/compiler/src/render3/r3_identifiers.ts b/packages/compiler/src/render3/r3_identifiers.ts index 7b04d69ecc..80f37838c2 100644 --- a/packages/compiler/src/render3/r3_identifiers.ts +++ b/packages/compiler/src/render3/r3_identifiers.ts @@ -229,6 +229,9 @@ export class Identifiers { moduleName: CORE, }; + static registerNgModuleType: + o.ExternalReference = {name: 'ɵregisterNgModuleType', moduleName: CORE}; + // sanitization-related functions static sanitizeHtml: o.ExternalReference = {name: 'ɵɵsanitizeHtml', moduleName: CORE}; static sanitizeStyle: o.ExternalReference = {name: 'ɵɵsanitizeStyle', moduleName: CORE}; diff --git a/packages/core/src/core_render3_private_export.ts b/packages/core/src/core_render3_private_export.ts index 179735a15e..2d9099f8d8 100644 --- a/packages/core/src/core_render3_private_export.ts +++ b/packages/core/src/core_render3_private_export.ts @@ -268,7 +268,10 @@ export { SWITCH_RENDERER2_FACTORY__POST_R3__ as ɵSWITCH_RENDERER2_FACTORY__POST_R3__, } from './render/api'; -export {getModuleFactory__POST_R3__ as ɵgetModuleFactory__POST_R3__} from './linker/ng_module_factory_loader'; +export { + getModuleFactory__POST_R3__ as ɵgetModuleFactory__POST_R3__, + registerNgModuleType as ɵregisterNgModuleType, +} from './linker/ng_module_factory_loader'; export { publishGlobalUtil as ɵpublishGlobalUtil, diff --git a/packages/core/src/render3/jit/environment.ts b/packages/core/src/render3/jit/environment.ts index 18c02dd2ba..fa0f5ad8db 100644 --- a/packages/core/src/render3/jit/environment.ts +++ b/packages/core/src/render3/jit/environment.ts @@ -9,6 +9,7 @@ import {ɵɵdefineInjectable, ɵɵdefineInjector,} from '../../di/interface/defs'; import {ɵɵinject} from '../../di/injector_compatibility'; import * as r3 from '../index'; +import {registerNgModuleType} from '../../linker/ng_module_factory_loader'; import * as sanitization from '../../sanitization/sanitization'; @@ -130,5 +131,7 @@ export const angularCoreEnv: {[name: string]: Function} = { 'ɵɵsanitizeResourceUrl': sanitization.ɵɵsanitizeResourceUrl, 'ɵɵsanitizeScript': sanitization.ɵɵsanitizeScript, 'ɵɵsanitizeUrl': sanitization.ɵɵsanitizeUrl, - 'ɵɵsanitizeUrlOrResourceUrl': sanitization.ɵɵsanitizeUrlOrResourceUrl + 'ɵɵsanitizeUrlOrResourceUrl': sanitization.ɵɵsanitizeUrlOrResourceUrl, + + 'ɵregisterNgModuleType': registerNgModuleType, };