From a7798f2a9306f68e408844594f68c80fb4c57374 Mon Sep 17 00:00:00 2001 From: Tobias Bosch Date: Tue, 26 Sep 2017 19:41:08 -0700 Subject: [PATCH] =?UTF-8?q?fix:=20don=E2=80=99t=20use=20the=20global=20`ng?= =?UTF-8?q?`=20at=20all=20with=20closure=20enhanced=20optimizations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is needed 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....')`). --- packages/core/src/core.externs.js | 8 ++++---- packages/goog.d.ts | 15 +++++++++++++++ packages/platform-browser/src/dom/util.ts | 12 +++++++----- packages/platform-browser/tsconfig-build.json | 3 ++- packages/types.d.ts | 1 + 5 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 packages/goog.d.ts diff --git a/packages/core/src/core.externs.js b/packages/core/src/core.externs.js index 8ee983fc7e..936424081b 100644 --- a/packages/core/src/core.externs.js +++ b/packages/core/src/core.externs.js @@ -8,10 +8,10 @@ * @externs */ -/** - * @suppress {duplicate} - */ -var ng; +// Note: we can't declare an extern for the global variable `ng` as +// the namespace `ng` is already used within Google +// for typings for angularJS (via `goog.provide('ng....')`). + /** * @suppress {duplicate} */ diff --git a/packages/goog.d.ts b/packages/goog.d.ts new file mode 100644 index 0000000000..063601d738 --- /dev/null +++ b/packages/goog.d.ts @@ -0,0 +1,15 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + + +/** + * Typings for google closure. + */ +declare namespace goog { + export const DEBUG: boolean; +} diff --git a/packages/platform-browser/src/dom/util.ts b/packages/platform-browser/src/dom/util.ts index efbbbdb71a..6a0ea7b7e4 100644 --- a/packages/platform-browser/src/dom/util.ts +++ b/packages/platform-browser/src/dom/util.ts @@ -28,10 +28,12 @@ export function dashCaseToCamelCase(input: string): string { * @param value The value to export. */ export function exportNgVar(name: string, value: any): void { - if (!ng) { - global['ng'] = ng = (global['ng'] as{[key: string]: any} | undefined) || {}; + if (typeof goog === 'undefined' || goog.DEBUG) { + // 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 ng = global['ng'] = (global['ng'] as{[key: string]: any} | undefined) || {}; + ng[name] = value; } - ng[name] = value; } - -let ng: {[key: string]: any}|undefined; diff --git a/packages/platform-browser/tsconfig-build.json b/packages/platform-browser/tsconfig-build.json index cf4b567c7e..f1c58e2ef6 100644 --- a/packages/platform-browser/tsconfig-build.json +++ b/packages/platform-browser/tsconfig-build.json @@ -15,7 +15,8 @@ "files": [ "public_api.ts", "../../node_modules/@types/hammerjs/index.d.ts", - "../../node_modules/zone.js/dist/zone.js.d.ts" + "../../node_modules/zone.js/dist/zone.js.d.ts", + "../goog.d.ts" ], "angularCompilerOptions": { diff --git a/packages/types.d.ts b/packages/types.d.ts index 009398d12f..30d6280539 100644 --- a/packages/types.d.ts +++ b/packages/types.d.ts @@ -15,3 +15,4 @@ /// /// /// +///