diff --git a/.changeset/stale-coats-perform.md b/.changeset/stale-coats-perform.md new file mode 100644 index 000000000..6e8208fee --- /dev/null +++ b/.changeset/stale-coats-perform.md @@ -0,0 +1,5 @@ +--- +'@cube-dev/ui-kit': minor +--- + +Declare uikit version globally. diff --git a/package.json b/package.json index d8a44a85b..e78363cdf 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ ], "scripts": { "start": "pnpm storybook", - "build": "npm-run-all clear:dist -p build:* && node ./scripts/copy-files.js && node scripts/add-banner.js", + "build": "npm-run-all clear:dist -p build:* && node ./scripts/copy-files.js && node scripts/add-banner.js && node scripts/replace-version.js", "build:esm": "tsc -p tsconfig.es.json", "build:cjs": "tsc -p tsconfig.cjs.json", "watch": "pnpm build:esm --watch", diff --git a/scripts/replace-version.js b/scripts/replace-version.js new file mode 100644 index 000000000..dfe610a13 --- /dev/null +++ b/scripts/replace-version.js @@ -0,0 +1,34 @@ +const fs = require('fs'); +const path = require('path'); + +console.log('Replacing version in compiled files...'); + +// Read package.json and extract the version +const packageJsonPath = path.resolve(__dirname, '../package.json'); +const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); +const version = packageJson.version; + +// Define the directory where compiled files are located +const distDir = path.resolve(__dirname, '../dist'); + +// Function to replace version in all .js files in the dist directory +function replaceVersionInFiles(dir) { + const files = fs.readdirSync(dir); + + files.forEach((file) => { + const filePath = path.join(dir, file); + const stat = fs.statSync(filePath); + + if (stat.isDirectory()) { + replaceVersionInFiles(filePath); // Recurse into subdirectories + } else if (file.endsWith('.js')) { + let content = fs.readFileSync(filePath, 'utf8'); + // Replace placeholder with version, wrapped in quotes + content = content.replace(/__UIKIT_VERSION__/g, `${version}`); + fs.writeFileSync(filePath, content); + } + }); +} + +// Execute the replacement +replaceVersionInFiles(distDir); diff --git a/src/index.ts b/src/index.ts index 02e45e597..ad051ebf1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,5 @@ +import './version'; + import { CubeTextProps, Text } from './components/content/Text'; import { CubeTitleProps, Title } from './components/content/Title'; import { CubeParagraphProps, Paragraph } from './components/content/Paragraph'; diff --git a/src/version.ts b/src/version.ts new file mode 100644 index 000000000..d510b0518 --- /dev/null +++ b/src/version.ts @@ -0,0 +1,20 @@ +// eslint-disable-next-line @typescript-eslint/no-unused-vars +interface Window { + CubeUIKit: { + version: string; + }; +} + +if (window.CubeUIKit?.version) { + console.error('More than one version of CubeUIKit is loaded', { + loadedVersions: [window.CubeUIKit.version, '__UIKIT_VERSION'], + }); +} else { + if (!window.CubeUIKit || !Array.isArray(window.CubeUIKit)) { + window.CubeUIKit = { + version: '__UIKIT_VERSION__', + }; + } else { + window.CubeUIKit.version = '__UIKIT_VERSION__'; + } +}