Skip to content

Commit

Permalink
refactor: remove globalThis polyfill (#3960)
Browse files Browse the repository at this point in the history
Fixes #3957

---------

Co-authored-by: Nolan Lawson <[email protected]>
  • Loading branch information
SourabhMulay and nolanlawson authored Feb 1, 2024
1 parent e0728d0 commit efb2c52
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import { noop } from '@lwc/shared';
import { globalThis } from '@lwc/shared';

export const instrumentDef = globalThis.__lwc_instrument_cmp_def ?? noop;
export const instrumentInstance = globalThis.__lwc_instrument_cmp_instance ?? noop;
export const instrumentDef = (globalThis as any).__lwc_instrument_cmp_def ?? noop;
export const instrumentInstance = (globalThis as any).__lwc_instrument_cmp_instance ?? noop;
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
import {
assign,
globalThis,
isTrue,
KEY__NATIVE_GET_ELEMENT_BY_ID,
KEY__NATIVE_QUERY_SELECTOR_ALL,
Expand Down Expand Up @@ -34,16 +33,18 @@ import { logWarnOnce } from '../shared/logger';
//

// Use the unpatched native getElementById/querySelectorAll rather than the synthetic one
const getElementById = globalThis[KEY__NATIVE_GET_ELEMENT_BY_ID] as typeof document.getElementById;
const getElementById = (globalThis as any)[
KEY__NATIVE_GET_ELEMENT_BY_ID
] as typeof document.getElementById;

const querySelectorAll = globalThis[
const querySelectorAll = (globalThis as any)[
KEY__NATIVE_QUERY_SELECTOR_ALL
] as typeof document.querySelectorAll;

// This is a "handoff" from synthetic-shadow to engine-core – we want to clean up after ourselves
// so nobody else can misuse these global APIs.
delete globalThis[KEY__NATIVE_GET_ELEMENT_BY_ID];
delete globalThis[KEY__NATIVE_QUERY_SELECTOR_ALL];
delete (globalThis as any)[KEY__NATIVE_GET_ELEMENT_BY_ID];
delete (globalThis as any)[KEY__NATIVE_QUERY_SELECTOR_ALL];

function isSyntheticShadowRootInstance(rootNode: Node): rootNode is ShadowRoot {
return rootNode !== document && isTrue((rootNode as any).synthetic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import {
registerTemplate,
registerDecorators,
} from '@lwc/engine-dom';
import { globalThis, LOWEST_API_VERSION } from '@lwc/shared';
import { LOWEST_API_VERSION } from '@lwc/shared';

// it needs to be imported from the window, otherwise the checks for associated vms is done against "@lwc/engine-core"
const LightningElementFormatter = globalThis['devtoolsFormatters'].find((f: any) => {
const LightningElementFormatter = (globalThis as any)['devtoolsFormatters'].find((f: any) => {
return f.name === 'LightningElementFormatter';
});

Expand Down
6 changes: 3 additions & 3 deletions packages/@lwc/engine-dom/src/formatters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/

import { globalThis, ArrayPush } from '@lwc/shared';
import { ArrayPush } from '@lwc/shared';
import { LightningElementFormatter } from './component';

function init() {
const devtoolsFormatters = globalThis.devtoolsFormatters || [];
const devtoolsFormatters = (globalThis as any).devtoolsFormatters || [];
ArrayPush.call(devtoolsFormatters, LightningElementFormatter);
globalThis.devtoolsFormatters = devtoolsFormatters;
(globalThis as any).devtoolsFormatters = devtoolsFormatters;
}

if (process.env.NODE_ENV !== 'production') {
Expand Down
2 changes: 1 addition & 1 deletion packages/@lwc/engine-server/src/polyfills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/

import { defineProperty, globalThis } from '@lwc/shared';
import { defineProperty } from '@lwc/shared';

/**
* The following constructor might be used in either the constructor or the connectedCallback. In
Expand Down
8 changes: 3 additions & 5 deletions packages/@lwc/features/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import { create, defineProperty, isUndefined, isBoolean, globalThis } from '@lwc/shared';
import { create, defineProperty, isUndefined, isBoolean } from '@lwc/shared';
import { FeatureFlagMap, FeatureFlagName, FeatureFlagValue } from './types';

// When deprecating a feature flag, ensure that it is also no longer set in the application. For
Expand All @@ -20,13 +20,11 @@ const features: FeatureFlagMap = {
ENABLE_FORCE_SHADOW_MIGRATE_MODE: null,
};

// eslint-disable-next-line no-restricted-properties
if (!globalThis.lwcRuntimeFlags) {
if (!(globalThis as any).lwcRuntimeFlags) {
Object.defineProperty(globalThis, 'lwcRuntimeFlags', { value: create(null) });
}

// eslint-disable-next-line no-restricted-properties
const flags: Partial<FeatureFlagMap> = globalThis.lwcRuntimeFlags;
const flags: Partial<FeatureFlagMap> = (globalThis as any).lwcRuntimeFlags;

/**
* Set the value at runtime of a given feature flag. This method only be invoked once per feature
Expand Down
15 changes: 0 additions & 15 deletions packages/@lwc/shared/src/global-this.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/@lwc/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import * as assert from './assert';
export * from './api-version';
export * from './aria';
export * from './language';
export * from './global-this';
export * from './keys';
export * from './void-elements';
export * from './html-attributes';
Expand Down
5 changes: 2 additions & 3 deletions packages/@lwc/synthetic-shadow/src/faux-shadow/shadow-root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
assign,
create,
defineProperty,
globalThis,
isNull,
isTrue,
isUndefined,
Expand Down Expand Up @@ -98,15 +97,15 @@ defineProperty(Node.prototype, KEY__SHADOW_RESOLVER, {

// The isUndefined check is because two copies of synthetic shadow may be loaded on the same page, and this
// would throw an error if we tried to redefine it. Plus the whole point is to expose the native method.
if (isUndefined(globalThis[KEY__NATIVE_GET_ELEMENT_BY_ID])) {
if (isUndefined((globalThis as any)[KEY__NATIVE_GET_ELEMENT_BY_ID])) {
defineProperty(globalThis, KEY__NATIVE_GET_ELEMENT_BY_ID, {
value: getElementById,
configurable: true,
});
}

// See note above.
if (isUndefined(globalThis[KEY__NATIVE_QUERY_SELECTOR_ALL])) {
if (isUndefined((globalThis as any)[KEY__NATIVE_QUERY_SELECTOR_ALL])) {
defineProperty(globalThis, KEY__NATIVE_QUERY_SELECTOR_ALL, {
value: querySelectorAll,
configurable: true,
Expand Down

0 comments on commit efb2c52

Please sign in to comment.