From 294e56d529ed92f59cebaca64609f80bbd21b6ad Mon Sep 17 00:00:00 2001 From: Rado Kirov Date: Tue, 20 Aug 2019 18:19:30 -0700 Subject: [PATCH] refactor(platform-browser): Hoist functions to workaround optimization bug. (#32230) Technically, function definitions can live anywhere because they are hoisted. However, in this case Closure optimizations break when exported function definitions are referred in another static object that is exported. The bad pattern is: ``` exports const obj = {f}; export function f() {...} ``` which turns to the following in Closure's module system: ``` goog.module('m'); exports.obj = {f}; function f() {...} exports.f = f; ``` which badly optimizes to (note module objects are collapsed) ``` var b = a; var a = function() {...}; // now b is undefined. ``` This is an optimizer bug and should be fixed in Closure, but in the meantime this change is a noop and will unblock other changes we want to make. PR Close #32230 --- packages/platform-browser/src/browser.ts | 30 ++++++++++++------------ 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/platform-browser/src/browser.ts b/packages/platform-browser/src/browser.ts index 7cdfc85994..44fad7407a 100644 --- a/packages/platform-browser/src/browser.ts +++ b/packages/platform-browser/src/browser.ts @@ -20,6 +20,21 @@ import {KeyEventsPlugin} from './dom/events/key_events'; import {DomSharedStylesHost, SharedStylesHost} from './dom/shared_styles_host'; import {DomSanitizer, DomSanitizerImpl} from './security/dom_sanitization_service'; +export function initDomAdapter() { + BrowserDomAdapter.makeCurrent(); + BrowserGetTestability.init(); +} + +export function errorHandler(): ErrorHandler { + return new ErrorHandler(); +} + +export function _document(): any { + // Tell ivy about the global document + ɵsetDocument(document); + return document; +} + export const INTERNAL_BROWSER_PLATFORM_PROVIDERS: StaticProvider[] = [ {provide: PLATFORM_ID, useValue: PLATFORM_BROWSER_ID}, {provide: PLATFORM_INITIALIZER, useValue: initDomAdapter, multi: true}, @@ -47,21 +62,6 @@ export const BROWSER_SANITIZATION_PROVIDERS = BROWSER_SANITIZATION_PROVIDERS__PR export const platformBrowser: (extraProviders?: StaticProvider[]) => PlatformRef = createPlatformFactory(platformCore, 'browser', INTERNAL_BROWSER_PLATFORM_PROVIDERS); -export function initDomAdapter() { - BrowserDomAdapter.makeCurrent(); - BrowserGetTestability.init(); -} - -export function errorHandler(): ErrorHandler { - return new ErrorHandler(); -} - -export function _document(): any { - // Tell ivy about the global document - ɵsetDocument(document); - return document; -} - export const BROWSER_MODULE_PROVIDERS: StaticProvider[] = [ BROWSER_SANITIZATION_PROVIDERS, {provide: INJECTOR_SCOPE, useValue: 'root'},