From bad34343371c577e76b78e49ca817804c778e2dc Mon Sep 17 00:00:00 2001 From: Andrew Kushnir Date: Fri, 4 Oct 2019 14:29:29 -0700 Subject: [PATCH] fix(ivy): avoid exposing `ng` with Closure Compiler enhanced optimizations (#33010) Prior to this commit, the `ng` was exposed in global namespace, which turned out to be problematic when minifying code with Closure, since it sometimes clobber our `ng` global. This commit aligns Ivy debugging tools behavior with existing logic in "platform-browser" package (packages/platform-browser/src/dom/util.ts#L31) by avoiding `ng` in global namespace when Closure Compiler is used. PR Close #33010 --- .../core/src/render3/util/global_utils.ts | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/packages/core/src/render3/util/global_utils.ts b/packages/core/src/render3/util/global_utils.ts index cbad2b6ac5..215d2fc23d 100644 --- a/packages/core/src/render3/util/global_utils.ts +++ b/packages/core/src/render3/util/global_utils.ts @@ -61,13 +61,19 @@ export declare type GlobalDevModeContainer = { * used from the browser console when an application is not in production. */ export function publishGlobalUtil(name: string, fn: Function): void { - const w = global as any as GlobalDevModeContainer; - ngDevMode && assertDefined(fn, 'function not defined'); - if (w) { - let container = w[GLOBAL_PUBLISH_EXPANDO_KEY]; - if (!container) { - container = w[GLOBAL_PUBLISH_EXPANDO_KEY] = {}; + if (typeof COMPILED === 'undefined' || !COMPILED) { + // Note: we can't export `ng` when using closure enhanced optimization as: + // - closure declares globals itself for minified names, which sometimes clobber our `ng` global + // - we can't declare a closure extern as the namespace `ng` is already used within Google + // for typings for AngularJS (via `goog.provide('ng....')`). + const w = global as any as GlobalDevModeContainer; + ngDevMode && assertDefined(fn, 'function not defined'); + if (w) { + let container = w[GLOBAL_PUBLISH_EXPANDO_KEY]; + if (!container) { + container = w[GLOBAL_PUBLISH_EXPANDO_KEY] = {}; + } + container[name] = fn; } - container[name] = fn; } }