From a0d04c628cb1293cdbce0819c223ac0924df6153 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matias=20Niemel=C3=A4?= Date: Tue, 17 Sep 2019 11:03:44 -0700 Subject: [PATCH] fix(ivy): ensure that `window.ng` utilities are published when NgModules are used (#32725) Prior to this patch if any backwards-compatible Angular code was using Ivy then the built-in `window.ng` debug utilies would not be exposed. PR Close #32725 --- packages/core/src/application_ref.ts | 13 +++++++++++++ .../core/test/application_ref_integration_spec.ts | 10 +++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/core/src/application_ref.ts b/packages/core/src/application_ref.ts index 4de809d509..5d5b2cafe4 100644 --- a/packages/core/src/application_ref.ts +++ b/packages/core/src/application_ref.ts @@ -8,6 +8,7 @@ import {Observable, Observer, Subscription, merge} from 'rxjs'; import {share} from 'rxjs/operators'; + import {ApplicationInitStatus} from './application_init'; import {APP_BOOTSTRAP_LISTENER, PLATFORM_INITIALIZER} from './application_tokens'; import {getCompilerFacade} from './compiler/compiler_facade'; @@ -30,6 +31,7 @@ import {assertNgModuleType} from './render3/assert'; import {ComponentFactory as R3ComponentFactory} from './render3/component_ref'; import {setLocaleId} from './render3/i18n'; import {NgModuleFactory as R3NgModuleFactory} from './render3/ng_module_ref'; +import {publishDefaultGlobalUtils as _publishDefaultGlobalUtils} from './render3/util/global_utils'; import {Testability, TestabilityRegistry} from './testability/testability'; import {isDevMode} from './util/is_dev_mode'; import {isPromise} from './util/lang'; @@ -81,6 +83,16 @@ export function compileNgModuleFactory__POST_R3__( .then(() => moduleFactory); } +// the `window.ng` global utilities are only available in non-VE versions of +// Angular. The function switch below will make sure that the code is not +// included into Angular when PRE mode is active. +export function publishDefaultGlobalUtils__PRE_R3__() {} +export function publishDefaultGlobalUtils__POST_R3__() { + ngDevMode && _publishDefaultGlobalUtils(); +} + +let publishDefaultGlobalUtils: () => any = publishDefaultGlobalUtils__PRE_R3__; + let isBoundToModule: (cf: ComponentFactory) => boolean = isBoundToModule__PRE_R3__; export function isBoundToModule__PRE_R3__(cf: ComponentFactory): boolean { @@ -116,6 +128,7 @@ export function createPlatform(injector: Injector): PlatformRef { throw new Error( 'There can be only one platform. Destroy the previous one to create a new one.'); } + publishDefaultGlobalUtils(); _platform = injector.get(PlatformRef); const inits = injector.get(PLATFORM_INITIALIZER, null); if (inits) inits.forEach((init: any) => init()); diff --git a/packages/core/test/application_ref_integration_spec.ts b/packages/core/test/application_ref_integration_spec.ts index bf17e89a56..10cbee4ce2 100644 --- a/packages/core/test/application_ref_integration_spec.ts +++ b/packages/core/test/application_ref_integration_spec.ts @@ -10,7 +10,6 @@ import {ApplicationRef, Component, DoCheck, NgModule, OnInit, TestabilityRegistr import {getTestBed} from '@angular/core/testing'; import {BrowserModule} from '@angular/platform-browser'; import {withBody} from '@angular/private/testing'; - import {NgModuleFactory} from '../src/render3/ng_module_ref'; ivyEnabled && describe('ApplicationRef bootstrap', () => { @@ -58,4 +57,13 @@ ivyEnabled && describe('ApplicationRef bootstrap', () => { registry.unregisterAllApplications(); })); + it('should expose the `window.ng` global utilities', + withBody('', async() => { + const MyAppModuleFactory = new NgModuleFactory(MyAppModule); + const moduleRef = + await getTestBed().platform.bootstrapModuleFactory(MyAppModuleFactory, {ngZone: 'noop'}); + + const ngUtils = (global as any).ng; + expect(ngUtils.getComponent).toBeTruthy(); + })); });