From 735cbfdc1e1ca7f12928393acdb98e295ed6133e Mon Sep 17 00:00:00 2001 From: Oleksandr Fediashov Date: Wed, 7 Dec 2022 12:14:28 +0100 Subject: [PATCH] fix: use global constants (#290) * fix: use global constants * PR review --- ...-cb8022d0-7662-4724-a582-c6536e4413fc.json | 7 ++++ packages/core/src/constants.test.ts | 14 ++++++++ packages/core/src/constants.ts | 32 +++++++++++++++---- 3 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 change/@griffel-core-cb8022d0-7662-4724-a582-c6536e4413fc.json create mode 100644 packages/core/src/constants.test.ts diff --git a/change/@griffel-core-cb8022d0-7662-4724-a582-c6536e4413fc.json b/change/@griffel-core-cb8022d0-7662-4724-a582-c6536e4413fc.json new file mode 100644 index 000000000..8b789f076 --- /dev/null +++ b/change/@griffel-core-cb8022d0-7662-4724-a582-c6536e4413fc.json @@ -0,0 +1,7 @@ +{ + "type": "patch", + "comment": "fix: use global constants", + "packageName": "@griffel/core", + "email": "olfedias@microsoft.com", + "dependentChangeType": "patch" +} diff --git a/packages/core/src/constants.test.ts b/packages/core/src/constants.test.ts new file mode 100644 index 000000000..8f16899fb --- /dev/null +++ b/packages/core/src/constants.test.ts @@ -0,0 +1,14 @@ +it('hosts global constants', async () => { + const importA = await import('./constants'); + + // We are reloading a module, so instances that are created inside a module will be different + // https://jestjs.io/docs/jest-object#jestresetmodules + jest.resetModules(); + const importB = await import('./constants'); + + expect(importA.DEFINITION_LOOKUP_TABLE).toBe(importA.DEFINITION_LOOKUP_TABLE); + expect(importA.DEBUG_RESET_CLASSES).toBe(importA.DEBUG_RESET_CLASSES); + + // This constant is not global, expected to be different + expect(importA.UNSUPPORTED_CSS_PROPERTIES).not.toBe(importB.UNSUPPORTED_CSS_PROPERTIES); +}); diff --git a/packages/core/src/constants.ts b/packages/core/src/constants.ts index 87f6e4af0..c5989304e 100644 --- a/packages/core/src/constants.ts +++ b/packages/core/src/constants.ts @@ -1,5 +1,31 @@ import { GriffelStylesUnsupportedCSSProperties, LookupItem, SequenceHash } from './types'; +// ---- + +// Heads up! +// These constants are global and will be shared between Griffel instances. +// Any change in them should happen only in a MAJOR version. If it happens, +// please change "__NAMESPACE_PREFIX__" to include a version. + +const __GLOBAL__: any = typeof window === 'undefined' ? global : window; +const __NAMESPACE_PREFIX__ = '@griffel/'; + +function getGlobalVar(name: string, defaultValue: T): T { + if (!__GLOBAL__[Symbol.for(__NAMESPACE_PREFIX__ + name)]) { + __GLOBAL__[Symbol.for(__NAMESPACE_PREFIX__ + name)] = defaultValue; + } + + return __GLOBAL__[Symbol.for(__NAMESPACE_PREFIX__ + name)]; +} + +/** @internal */ +export const DEBUG_RESET_CLASSES = getGlobalVar>('DEBUG_RESET_CLASSES', {}); + +/** @internal */ +export const DEFINITION_LOOKUP_TABLE = getGlobalVar>('DEFINITION_LOOKUP_TABLE', {}); + +// ---- + /** @internal */ export const DATA_BUCKET_ATTR = 'data-make-styles-bucket'; @@ -24,12 +50,6 @@ export const SEQUENCE_SIZE = ? SEQUENCE_PREFIX.length + SEQUENCE_HASH_LENGTH : SEQUENCE_PREFIX.length + SEQUENCE_HASH_LENGTH + DEBUG_SEQUENCE_SEPARATOR.length + SEQUENCE_HASH_LENGTH; -/** @internal */ -export const DEBUG_RESET_CLASSES: Record = {}; - -/** @internal */ -export const DEFINITION_LOOKUP_TABLE: Record = {}; - // indexes for values in LookupItem tuple /** @internal */