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
This commit is contained in:
Matias Niemelä 2019-09-17 11:03:44 -07:00 committed by Andrew Kushnir
parent b741a1c3e7
commit a0d04c628c
2 changed files with 22 additions and 1 deletions

View File

@ -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__<M>(
.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: <C>(cf: ComponentFactory<C>) => boolean = isBoundToModule__PRE_R3__;
export function isBoundToModule__PRE_R3__<C>(cf: ComponentFactory<C>): 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());

View File

@ -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('<hello-world></hello-world>', 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();
}));
});