From fa809758322a2c49aef5fa4dcfd9a512d446fe75 Mon Sep 17 00:00:00 2001 From: Matthieu Riegler Date: Fri, 28 Apr 2023 13:52:59 +0200 Subject: [PATCH] refactor(core): use globalThis for global (#50063) `globalThis` is now available on every runtime supported by Angular PR Close #50063 --- .../side-effects/snapshots/core/esm2022.js | 10 +--------- packages/core/src/util/global.ts | 18 +----------------- packages/core/testing/src/test_hooks.ts | 12 ++---------- 3 files changed, 4 insertions(+), 36 deletions(-) diff --git a/integration/side-effects/snapshots/core/esm2022.js b/integration/side-effects/snapshots/core/esm2022.js index 13a1af39412..dbc3b5fd647 100644 --- a/integration/side-effects/snapshots/core/esm2022.js +++ b/integration/side-effects/snapshots/core/esm2022.js @@ -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.)"); diff --git a/packages/core/src/util/global.ts b/packages/core/src/util/global.ts index 2bc1ce2296e..cb13b1681ca 100644 --- a/packages/core/src/util/global.ts +++ b/packages/core/src/util/global.ts @@ -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 diff --git a/packages/core/testing/src/test_hooks.ts b/packages/core/testing/src/test_hooks.ts index 45ea7d3c14a..ada3632d428 100644 --- a/packages/core/testing/src/test_hooks.ts +++ b/packages/core/testing/src/test_hooks.ts @@ -15,21 +15,13 @@ import {resetFakeAsyncZone} from './fake_async'; import {TestBedImpl} from './test_bed'; -declare var global: any; - -const _global = (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 () => {