refactor(core): use globalThis for global (#50063)

`globalThis` is now available on every runtime supported by Angular

PR Close #50063
This commit is contained in:
Matthieu Riegler 2023-04-28 13:52:59 +02:00 committed by Jessica Janiuk
parent 8d636ad6a7
commit fa80975832
3 changed files with 4 additions and 36 deletions

View file

@ -2,15 +2,7 @@ import "rxjs";
import "rxjs/operators";
const __globalThis = "undefined" !== typeof globalThis && globalThis;
const __window = "undefined" !== typeof window && window;
const __self = "undefined" !== typeof self && "undefined" !== typeof WorkerGlobalScope && self instanceof WorkerGlobalScope && self;
const __global = "undefined" !== typeof global && global;
const _global = __globalThis || __global || __window || __self;
const _global = globalThis;
if ("undefined" !== typeof ngDevMode && ngDevMode) _global.$localize = _global.$localize || function() {
throw new Error("It looks like your application or one of its dependencies is using i18n.\n" + "Angular 9 introduced a global `$localize()` function that needs to be loaded.\n" + "Please run `ng add @angular/localize` from the Angular CLI.\n" + "(For non-CLI projects, add `import '@angular/localize/init';` to your `polyfills.ts` file.\n" + "For server-side rendering applications add the import to your `main.server.ts` file.)");

View file

@ -6,23 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
// TODO(jteplitz602): Load WorkerGlobalScope from lib.webworker.d.ts file #3492
declare var WorkerGlobalScope: any;
// CommonJS / Node have global context exposed as "global" variable.
// 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;
// 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. Note: Typeof/Instanceof
// checks are considered side-effects in Terser. We explicitly mark this as side-effect free:
// https://github.com/terser/terser/issues/250.
const _global: any = (/* @__PURE__ */ (
() => (typeof globalThis !== 'undefined' && globalThis) ||
(typeof global !== 'undefined' && global) || (typeof window !== 'undefined' && window) ||
(typeof self !== 'undefined' && typeof WorkerGlobalScope !== 'undefined' &&
self instanceof WorkerGlobalScope && self))());
const _global: any = globalThis;
/**
* Attention: whenever providing a new value, be sure to add an

View file

@ -15,21 +15,13 @@
import {resetFakeAsyncZone} from './fake_async';
import {TestBedImpl} from './test_bed';
declare var global: any;
const _global = <any>(typeof window === 'undefined' ? global : window);
// Reset the test providers and the fake async zone before each test.
if (_global.beforeEach) {
_global.beforeEach(getCleanupHook(false));
}
beforeEach(getCleanupHook(false));
// We provide both a `beforeEach` and `afterEach`, because the updated behavior for
// tearing down the module is supposed to run after the test so that we can associate
// teardown errors with the correct test.
if (_global.afterEach) {
_global.afterEach(getCleanupHook(true));
}
afterEach(getCleanupHook(true));
function getCleanupHook(expectedTeardownValue: boolean) {
return () => {