fix(core): improve global variable detection (#28679)

Closes #16545

PR Close #28679
This commit is contained in:
Trotyl Yu
2019-02-13 18:07:54 +08:00
committed by Miško Hevery
parent 553f80ff46
commit 77eee42963
5 changed files with 50 additions and 25 deletions

View File

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