From f2d7096ea6572d012fdefc5caa2ea2da98821e1c Mon Sep 17 00:00:00 2001 From: Anton Arnautov Date: Wed, 7 May 2025 19:13:16 +0200 Subject: [PATCH] Restore version replacement --- package.json | 1 + scripts/bundle-cjs.mjs | 9 ++++++--- scripts/bundle-esm.mjs | 7 ++----- scripts/get-package-version.mjs | 11 ++++++++--- src/components/Chat/__tests__/Chat.test.js | 2 +- src/components/Chat/hooks/useChat.ts | 2 +- yarn.lock | 19 +++++++++++++++++++ 7 files changed, 38 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index a3b6f49940..1c1d98fc8d 100644 --- a/package.json +++ b/package.json @@ -219,6 +219,7 @@ "dotenv": "^8.6.0", "emoji-mart": "^5.5.2", "esbuild": "^0.23.1", + "esbuild-plugin-replace": "^1.4.0", "eslint": "^9.16.0", "eslint-plugin-import": "^2.31.0", "eslint-plugin-jest": "^28.11.0", diff --git a/scripts/bundle-cjs.mjs b/scripts/bundle-cjs.mjs index 2cbbfadd8d..40cd58d3a6 100755 --- a/scripts/bundle-cjs.mjs +++ b/scripts/bundle-cjs.mjs @@ -3,6 +3,7 @@ import { dirname, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; import * as esbuild from 'esbuild'; +import { replace } from 'esbuild-plugin-replace'; import getPackageVersion from './get-package-version.mjs'; import packageJson from '../package.json' with { type: 'json' }; @@ -56,9 +57,11 @@ const bundles = ['browser', 'node'].map((platform) => ...cjsBundleConfig, entryNames: `[dir]/[name].${platform}`, platform, - define: { - 'process.env.STREAM_CHAT_REACT_VERSION': JSON.stringify(getPackageVersion()), - }, + plugins: [ + replace({ + __STREAM_CHAT_REACT_VERSION__: getPackageVersion(), + }), + ], }), ); await Promise.all(bundles); diff --git a/scripts/bundle-esm.mjs b/scripts/bundle-esm.mjs index bf5a24b6f6..b22f8a7ff4 100755 --- a/scripts/bundle-esm.mjs +++ b/scripts/bundle-esm.mjs @@ -21,10 +21,7 @@ const bundleEsm = async () => { await Promise.all( files.map(async (file) => { const content = await readFile(file, 'utf8'); - const newContent = content.replace( - /process.env.STREAM_CHAT_REACT_VERSION/g, - JSON.stringify(version), - ); + const newContent = content.replace(/__STREAM_CHAT_REACT_VERSION__/g, version); await writeFile(file, newContent); }), ); @@ -32,4 +29,4 @@ const bundleEsm = async () => { console.log('ESM build complete'); }; -bundleEsm().catch(console.error); +bundleEsm(); diff --git a/scripts/get-package-version.mjs b/scripts/get-package-version.mjs index 0e6d224655..596d412a14 100644 --- a/scripts/get-package-version.mjs +++ b/scripts/get-package-version.mjs @@ -1,10 +1,15 @@ import { execSync } from 'node:child_process'; import packageJson from '../package.json' with { type: 'json' }; -// get the latest version so that "process.env.STREAM_CHAT_REACT_VERSION" can be replaced with it in the source code (used for reporting purposes), see bundle-cjs.mjs/bundle-esm.mjs for source +// get the latest version so that "__STREAM_CHAT_REACT_VERSION__" can +// be replaced with it in the source code (used for reporting purposes) +// see bundle-cjs.mjs/bundle-esm.mjs for source export default function getPackageVersion() { - // "build" script ("prepack" hook) gets invoked when semantic-release runs "npm publish", at that point package.json#version already contains updated next version which we can use - let version = packageJson.version; + // "build" script ("prepack" hook) gets invoked when semantic-release + // runs "npm publish", at that point package.json#version already + // contains updated next version which we can use (npm populates env + // with npm_package_version which could be used also) + let version = packageJson.version; // process.env.npm_package_version // if it fails (loads a default), try pulling version from git if (version === '0.0.0-development') { diff --git a/src/components/Chat/__tests__/Chat.test.js b/src/components/Chat/__tests__/Chat.test.js index 9fe2722f69..ddbda72023 100644 --- a/src/components/Chat/__tests__/Chat.test.js +++ b/src/components/Chat/__tests__/Chat.test.js @@ -64,7 +64,7 @@ describe('Chat', () => { expect(context.openMobileNav).toBeInstanceOf(Function); expect(context.closeMobileNav).toBeInstanceOf(Function); expect(context.client.getUserAgent()).toBe( - `stream-chat-react-undefined-${originalUserAgent}`, + `stream-chat-react-__STREAM_CHAT_REACT_VERSION__-${originalUserAgent}`, ); }); }); diff --git a/src/components/Chat/hooks/useChat.ts b/src/components/Chat/hooks/useChat.ts index 21523ecdfc..c8cdc186cc 100644 --- a/src/components/Chat/hooks/useChat.ts +++ b/src/components/Chat/hooks/useChat.ts @@ -55,7 +55,7 @@ export const useChat = ({ useEffect(() => { if (!client) return; - const version = process.env.STREAM_CHAT_REACT_VERSION; + const version = '__STREAM_CHAT_REACT_VERSION__'; const userAgent = client.getUserAgent(); if (!userAgent.includes('stream-chat-react')) { diff --git a/yarn.lock b/yarn.lock index c8676c8e54..cb3f417b4e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5386,6 +5386,13 @@ esbuild-openbsd-64@0.14.27: resolved "https://registry.yarnpkg.com/esbuild-openbsd-64/-/esbuild-openbsd-64-0.14.27.tgz#e99f8cdc63f1628747b63edd124d53cf7796468d" integrity sha512-xwSje6qIZaDHXWoPpIgvL+7fC6WeubHHv18tusLYMwL+Z6bEa4Pbfs5IWDtQdHkArtfxEkIZz77944z8MgDxGw== +esbuild-plugin-replace@^1.4.0: + version "1.4.0" + resolved "https://registry.yarnpkg.com/esbuild-plugin-replace/-/esbuild-plugin-replace-1.4.0.tgz#2051eb35e21699e41dcf75630f613bdaa5039be6" + integrity sha512-lP3ZAyzyRa5JXoOd59lJbRKNObtK8pJ/RO7o6vdjwLi71GfbL32NR22ZuS7/cLZkr10/L1lutoLma8E4DLngYg== + dependencies: + magic-string "^0.25.7" + esbuild-sunos-64@0.14.27: version "0.14.27" resolved "https://registry.yarnpkg.com/esbuild-sunos-64/-/esbuild-sunos-64-0.14.27.tgz#8611d825bcb8239c78d57452e83253a71942f45c" @@ -8852,6 +8859,13 @@ lz-string@^1.5.0: resolved "https://registry.yarnpkg.com/lz-string/-/lz-string-1.5.0.tgz#c1ab50f77887b712621201ba9fd4e3a6ed099941" integrity sha512-h5bgJWpxJNswbU7qCrV0tIKQCaS3blPDrqKWx+QxzuzL1zGUzij9XCWLrSLsJPu5t+eWA/ycetzYAO5IOMcWAQ== +magic-string@^0.25.7: + version "0.25.9" + resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.9.tgz#de7f9faf91ef8a1c91d02c2e5314c8277dbcdd1c" + integrity sha512-RmF0AsMzgt25qzqqLc1+MbHmhdx0ojF2Fvs4XnOqz2ZOBXzzkEwc/dJQZCYHAn7v1jbVOjAZfK8msRn4BxO4VQ== + dependencies: + sourcemap-codec "^1.4.8" + make-dir@^2.0.0, make-dir@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-2.1.0.tgz#5f0310e18b8be898cc07009295a30ae41e91e6f5" @@ -12033,6 +12047,11 @@ source-map@^0.6.0, source-map@^0.6.1, source-map@~0.6.1: resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263" integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g== +sourcemap-codec@^1.4.8: + version "1.4.8" + resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.8.tgz#ea804bd94857402e6992d05a38ef1ae35a9ab4c4" + integrity sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA== + space-separated-tokens@^2.0.0: version "2.0.2" resolved "https://registry.yarnpkg.com/space-separated-tokens/-/space-separated-tokens-2.0.2.tgz#1ecd9d2350a3844572c3f4a312bceb018348859f"