diff --git a/packages/core/src/util/global.ts b/packages/core/src/util/global.ts index 15674cc123..49d5b9034d 100644 --- a/packages/core/src/util/global.ts +++ b/packages/core/src/util/global.ts @@ -12,14 +12,23 @@ declare var WorkerGlobalScope: any /** TODO #9100 */; // We don't want to include the whole node.d.ts this this compilation unit so we'll just fake // the global "global" var for now. declare var global: any /** TODO #9100 */; -const __window = typeof window !== 'undefined' && window; -const __self = typeof self !== 'undefined' && typeof WorkerGlobalScope !== 'undefined' && - self instanceof WorkerGlobalScope && self; -const __global = typeof global !== 'undefined' && global; +// Not yet available in TypeScript: https://github.com/Microsoft/TypeScript/pull/29332 +declare var globalThis: any /** TODO #9100 */; -// Check __global first, because in Node tests both __global and __window may be defined and _global -// should be __global in that case. -const _global: {[name: string]: any} = __global || __window || __self; +function getGlobal(): any { + const __globalThis = typeof globalThis !== 'undefined' && globalThis; + const __window = typeof window !== 'undefined' && window; + const __self = typeof self !== 'undefined' && typeof WorkerGlobalScope !== 'undefined' && + self instanceof WorkerGlobalScope && self; + const __global = typeof global !== 'undefined' && global; + + // Always use __globalThis if available, which is the spec-defined global variable across all + // environments, then fallback to __global first, because in Node tests both __global and + // __window may be defined and _global should be __global in that case. + return __globalThis || __global || __window || __self; +} + +const _global = getGlobal(); /** * Attention: whenever providing a new value, be sure to add an diff --git a/packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json b/packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json index 2d81d2aa30..ecaa77c17b 100644 --- a/packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json +++ b/packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json @@ -143,15 +143,9 @@ { "name": "ViewEncapsulation" }, - { - "name": "__self" - }, { "name": "__values" }, - { - "name": "__window" - }, { "name": "_currentNamespace" }, @@ -329,6 +323,9 @@ { "name": "getElementDepthCount" }, + { + "name": "getGlobal" + }, { "name": "getHighestElementOrICUContainer" }, diff --git a/packages/core/test/bundling/hello_world/bundle.golden_symbols.json b/packages/core/test/bundling/hello_world/bundle.golden_symbols.json index 896de5c568..dfa14e80ed 100644 --- a/packages/core/test/bundling/hello_world/bundle.golden_symbols.json +++ b/packages/core/test/bundling/hello_world/bundle.golden_symbols.json @@ -116,15 +116,9 @@ { "name": "ViewEncapsulation" }, - { - "name": "__self" - }, { "name": "__values" }, - { - "name": "__window" - }, { "name": "_global" }, @@ -239,6 +233,9 @@ { "name": "getDirectiveDef" }, + { + "name": "getGlobal" + }, { "name": "getHighestElementOrICUContainer" }, diff --git a/packages/core/test/bundling/todo/bundle.golden_symbols.json b/packages/core/test/bundling/todo/bundle.golden_symbols.json index a4db8b2a37..8b58277077 100644 --- a/packages/core/test/bundling/todo/bundle.golden_symbols.json +++ b/packages/core/test/bundling/todo/bundle.golden_symbols.json @@ -278,18 +278,12 @@ { "name": "__read" }, - { - "name": "__self" - }, { "name": "__spread" }, { "name": "__values" }, - { - "name": "__window" - }, { "name": "_c0" }, @@ -680,6 +674,9 @@ { "name": "getErrorLogger" }, + { + "name": "getGlobal" + }, { "name": "getHighestElementOrICUContainer" }, diff --git a/packages/core/test/util/global_spec.ts b/packages/core/test/util/global_spec.ts new file mode 100644 index 0000000000..c74e33ea69 --- /dev/null +++ b/packages/core/test/util/global_spec.ts @@ -0,0 +1,25 @@ +/** + * @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 + */ + +import {global} from '../../src/util/global'; + +// Not yet available in TypeScript: https://github.com/Microsoft/TypeScript/pull/29332 +declare var globalThis: any /** TODO #9100 */; + +{ + describe('global', () => { + it('should be global this value', () => { + const _global = new Function('return this')(); + expect(global).toBe(_global); + }); + + if (typeof globalThis !== 'undefined') { + it('should use globalThis as global reference', () => { expect(global).toBe(globalThis); }); + } + }); +}