Skip to content

Commit

Permalink
feat: register current runtime version in window (#6222)
Browse files Browse the repository at this point in the history
Closes #6210
  • Loading branch information
MarcusNotheis authored Aug 19, 2024
1 parent 28b14d9 commit 367628c
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 7 deletions.
2 changes: 2 additions & 0 deletions packages/base/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pkgJson from '../package.json';
import * as Device from './Device/index.js';
import * as hooks from './hooks/index.js';
import { I18nStore } from './stores/I18nStore.js';
Expand All @@ -9,3 +10,4 @@ export * from './utils/index.js';
export * from './hooks/index.js';

export { I18nStore, StyleStore, ThemingParameters, Device, hooks };
export const version = pkgJson.version;
2 changes: 2 additions & 0 deletions packages/charts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pkgJson from '../package.json';
import { BarChart } from './components/BarChart/BarChart.js';
import { BarChartPlaceholder } from './components/BarChart/Placeholder.js';
import { BulletChart } from './components/BulletChart/BulletChart.js';
Expand Down Expand Up @@ -47,3 +48,4 @@ export {
ColumnChartWithTrendPlaceholder,
TimelineChartPlaceholder
};
export const version = pkgJson.version;
2 changes: 2 additions & 0 deletions packages/compat/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pkgJson from '../package.json';
export * from './components/Loader/index.js';
export * from './components/OverflowToolbarButton/index.js';
export * from './components/OverflowToolbarToggleButton/index.js';
Expand All @@ -13,3 +14,4 @@ export * from './components/ToolbarSpacer/index.js';
export { LoaderType } from './enums/LoaderType.js';
export { ToolbarDesign } from './enums/ToolbarDesign.js';
export { ToolbarStyle } from './enums/ToolbarStyle.js';
export const version = pkgJson.version;
32 changes: 31 additions & 1 deletion packages/main/src/components/ThemeProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,21 @@ import type { StyleDataCSP } from '@ui5/webcomponents-base/dist/ManagedStyles.js
import { attachThemeLoaded, detachThemeLoaded } from '@ui5/webcomponents-base/dist/theming/ThemeLoaded.js';
import { I18nStore, StyleStore, useIsomorphicLayoutEffect, useStylesheet } from '@ui5/webcomponents-react-base';
import type { FC, ReactNode } from 'react';
import { useId } from 'react';
import { useEffect, useId } from 'react';
import pkgJson from '../../../package.json';
import { parseSemVer } from '../../internal/utils.js';
import { styleData } from './ThemeProvider.css.js';

let _versionInfo = null;
let _versionInfoInjected = false;

function getVersionInfo() {
if (!_versionInfo) {
_versionInfo = parseSemVer(pkgJson.version);
}
return _versionInfo;
}

function ThemeProviderStyles() {
const uniqueId = useId();
useStylesheet(styleData, `${ThemeProvider.displayName}-${uniqueId}`);
Expand Down Expand Up @@ -79,6 +91,24 @@ const ThemeProvider: FC<ThemeProviderPropTypes> = (props: ThemeProviderPropTypes
};
}, []);

useEffect(() => {
if (_versionInfoInjected) {
return;
}
const versionInfo = getVersionInfo();
globalThis['@ui5/webcomponents-react'] ??= {};
globalThis['@ui5/webcomponents-react'].Runtimes ??= [];

globalThis['@ui5/webcomponents-react'].Runtimes.push(versionInfo);
_versionInfoInjected = true;
return () => {
globalThis['@ui5/webcomponents-react'].Runtimes = globalThis['@ui5/webcomponents-react'].Runtimes.filter(
(info) => info !== versionInfo
);
_versionInfoInjected = false;
};
}, []);

return (
<>
<ThemeProviderStyles />
Expand Down
2 changes: 2 additions & 0 deletions packages/main/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pkgJson from '../package.json';
import * as AnalyticalTableHooks from './components/AnalyticalTable/pluginHooks/AnalyticalTableHooks.js';

export * from './components/ActionSheet/index.js';
Expand Down Expand Up @@ -37,3 +38,4 @@ export type { CommonProps, Ui5CustomEvent, Ui5DomRef, UI5WCSlotsNode, Nullable }
export * from './webComponents/index.js';

export { AnalyticalTableHooks };
export const version = pkgJson.version;
15 changes: 15 additions & 0 deletions packages/main/src/internal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,18 @@ export function getTagNameWithoutScopingSuffix(tagName) {
const tagNameSuffix = getCustomElementsScopingSuffix();
return tagNameSuffix ? tagName.replace(`-${tagNameSuffix.toUpperCase()}`, '') : tagName;
}

const SEMVER_REGEX =
/^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-(?<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;

export function parseSemVer(version: string) {
const parsed = SEMVER_REGEX.exec(version).groups;
return {
version,
major: parseInt(parsed.major),
minor: parseInt(parsed.minor),
patch: parseInt(parsed.patch),
prerelease: parsed.prerelease,
buildMetadata: parsed.buildmetadata
};
}
8 changes: 2 additions & 6 deletions packages/main/src/internal/withWebComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import type { ComponentType, ReactElement, ReactNode, Ref } from 'react';
import { cloneElement, forwardRef, Fragment, isValidElement, useEffect, useState, version } from 'react';
import type { CommonProps, Ui5DomRef } from '../types/index.js';
import { useServerSideEffect } from './ssr.js';
import { camelToKebabCase, capitalizeFirstLetter, kebabToCamelCase } from './utils.js';

const SEMVER_REGEX =
/^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-(?<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
import { camelToKebabCase, capitalizeFirstLetter, kebabToCamelCase, parseSemVer } from './utils.js';

const createEventPropName = (eventName: string) => `on${capitalizeFirstLetter(kebabToCamelCase(eventName))}`;

Expand Down Expand Up @@ -38,8 +35,7 @@ export const withWebComponent = <Props extends Record<string, any>, RefType = Ui
eventProperties: string[],
loader: () => Promise<unknown>
) => {
const reactMajorVersion = SEMVER_REGEX.exec(version)?.groups?.major;
const webComponentsSupported = parseInt(reactMajorVersion) >= 19;
const webComponentsSupported = parseSemVer(version).major >= 19;
// displayName will be assigned in the individual files
// eslint-disable-next-line react/display-name
return forwardRef<RefType, Props & WithWebComponentPropTypes>((props, wcRef) => {
Expand Down

0 comments on commit 367628c

Please sign in to comment.