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
This commit is contained in:
Andrew Kushnir 2019-10-04 14:29:29 -07:00 committed by Alex Rickabaugh
parent 0119f46daf
commit bad3434337
1 changed files with 13 additions and 7 deletions

View File

@ -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;
}
}