Skip to content

Commit 2157d2f

Browse files
authored
fix: report correct package version in the source code (#2566)
1 parent db95e34 commit 2157d2f

File tree

8 files changed

+70
-13
lines changed

8 files changed

+70
-13
lines changed

.releaserc.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@
7171
}
7272
}
7373
],
74+
[
75+
"@semantic-release/exec", {
76+
"prepareCmd": "NEXT_VERSION=${nextRelease.version} npm run build"
77+
}],
7478
[
7579
"@semantic-release/changelog",
7680
{

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@
184184
"@ladle/react": "^0.16.0",
185185
"@playwright/test": "^1.42.1",
186186
"@semantic-release/changelog": "^6.0.2",
187+
"@semantic-release/exec": "^6.0.3",
187188
"@semantic-release/git": "^10.0.1",
188189
"@stream-io/rollup-plugin-node-builtins": "^2.1.5",
189190
"@stream-io/stream-chat-css": "^5.4.0",
@@ -221,6 +222,7 @@
221222
"dotenv": "^8.6.0",
222223
"emoji-mart": "^5.5.2",
223224
"esbuild": "^0.23.1",
225+
"esbuild-plugin-replace": "^1.4.0",
224226
"eslint": "7.14.0",
225227
"eslint-config-airbnb": "^18.2.1",
226228
"eslint-config-prettier": "^6.15.0",
@@ -260,7 +262,7 @@
260262
"typescript": "^5.4.5"
261263
},
262264
"scripts": {
263-
"build": "rm -rf dist && ./scripts/copy-version.sh && yarn build-translations && yarn bundle",
265+
"build": "rm -rf dist && yarn build-translations && yarn bundle",
264266
"bundle": "concurrently tsc ./scripts/copy-css.sh ./scripts/bundle.mjs",
265267
"build-translations": "i18next",
266268
"coverage": "jest --collectCoverage && codecov",

scripts/bundle.mjs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import { dirname, resolve } from 'node:path';
44
import { fileURLToPath } from 'node:url';
5+
import { execSync } from 'node:child_process';
56
import * as esbuild from 'esbuild';
7+
import { replace } from 'esbuild-plugin-replace';
68

79
// import.meta.dirname is not available before Node 20
810
const __dirname = dirname(fileURLToPath(import.meta.url));
@@ -44,6 +46,28 @@ const cjsBundleConfig = {
4446
sourcemap: 'linked',
4547
};
4648

49+
// Get the latest version so that magic string __STREAM_CHAT_REACT_VERSION__ can be replaced with it in the source code (used for reporting purposes)
50+
const getVersion = () => {
51+
let version;
52+
// During release, use the version being released
53+
// see .releaserc.json where the `NEXT_VERSION` env variable is set
54+
if (process.env.NEXT_VERSION) {
55+
version = process.env.NEXT_VERSION;
56+
} else {
57+
// Otherwise use the latest git tag
58+
try {
59+
version = execSync('git describe --tags --abbrev=0').toString().trim();
60+
} catch (error) {
61+
console.error(error);
62+
console.warn('Could not get latest version from git tags, falling back to package.json');
63+
version = packageJson.default.version;
64+
}
65+
}
66+
console.log(`Determined the build package version to be ${version}`);
67+
return version;
68+
};
69+
70+
4771
// We build two CJS bundles: for browser and for node. The latter one can be
4872
// used e.g. during SSR (although it makes little sence to SSR chat, but still
4973
// nice for import not to break on server).
@@ -52,6 +76,11 @@ const bundles = ['browser', 'node'].map((platform) =>
5276
...cjsBundleConfig,
5377
entryNames: `[dir]/[name].${platform}`,
5478
platform,
79+
plugins: [
80+
replace({
81+
'__STREAM_CHAT_REACT_VERSION__': getVersion(),
82+
}),
83+
],
5584
}),
5685
);
5786
await Promise.all(bundles);

scripts/copy-version.sh

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/components/Chat/__tests__/Chat.test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import {
1212
getTestClientWithUser,
1313
} from '../../../mock-builders';
1414

15-
import { version } from '../../../../package.json';
16-
1715
const ChatContextConsumer = ({ fn }) => {
1816
fn(useContext(ChatContext));
1917
return <div data-testid='children' />;
@@ -66,7 +64,7 @@ describe('Chat', () => {
6664
expect(context.openMobileNav).toBeInstanceOf(Function);
6765
expect(context.closeMobileNav).toBeInstanceOf(Function);
6866
expect(context.client.getUserAgent()).toBe(
69-
`stream-chat-react-${version}-${originalUserAgent}`,
67+
`stream-chat-react-__STREAM_CHAT_REACT_VERSION__-${originalUserAgent}`,
7068
);
7169
});
7270
});

src/components/Chat/hooks/useChat.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
Streami18n,
88
SupportedTranslations,
99
} from '../../../i18n';
10-
import { version } from '../../../version';
1110

1211
import type { AppSettingsAPIResponse, Channel, Event, Mute, StreamChat } from 'stream-chat';
1312

@@ -62,7 +61,8 @@ export const useChat = <
6261
const userAgent = client.getUserAgent();
6362
if (!userAgent.includes('stream-chat-react')) {
6463
// result looks like: 'stream-chat-react-2.3.2-stream-chat-javascript-client-browser-2.2.2'
65-
client.setUserAgent(`stream-chat-react-${version}-${userAgent}`);
64+
// __STREAM_CHAT_REACT_VERSION__ is replaced during the build process with the current version
65+
client.setUserAgent(`stream-chat-react-__STREAM_CHAT_REACT_VERSION__-${userAgent}`);
6666
}
6767

6868
client.threads.registerSubscriptions();

src/version.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

yarn.lock

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2233,6 +2233,18 @@
22332233
resolved "https://registry.yarnpkg.com/@semantic-release/error/-/error-3.0.0.tgz#30a3b97bbb5844d695eb22f9d3aa40f6a92770c2"
22342234
integrity sha512-5hiM4Un+tpl4cKw3lV4UgzJj+SmfNIDCLLw0TepzQxz9ZGV5ixnqkzIVF+3tp0ZHgcMKE+VNGHJjEeyFG2dcSw==
22352235

2236+
"@semantic-release/exec@^6.0.3":
2237+
version "6.0.3"
2238+
resolved "https://registry.yarnpkg.com/@semantic-release/exec/-/exec-6.0.3.tgz#d212fdf19633bdfb553de6cb6c7f8781933224db"
2239+
integrity sha512-bxAq8vLOw76aV89vxxICecEa8jfaWwYITw6X74zzlO0mc/Bgieqx9kBRz9z96pHectiTAtsCwsQcUyLYWnp3VQ==
2240+
dependencies:
2241+
"@semantic-release/error" "^3.0.0"
2242+
aggregate-error "^3.0.0"
2243+
debug "^4.0.0"
2244+
execa "^5.0.0"
2245+
lodash "^4.17.4"
2246+
parse-json "^5.0.0"
2247+
22362248
"@semantic-release/git@^10.0.1":
22372249
version "10.0.1"
22382250
resolved "https://registry.yarnpkg.com/@semantic-release/git/-/git-10.0.1.tgz#c646e55d67fae623875bf3a06a634dd434904498"
@@ -5499,6 +5511,13 @@ [email protected]:
54995511
resolved "https://registry.yarnpkg.com/esbuild-openbsd-64/-/esbuild-openbsd-64-0.14.27.tgz#e99f8cdc63f1628747b63edd124d53cf7796468d"
55005512
integrity sha512-xwSje6qIZaDHXWoPpIgvL+7fC6WeubHHv18tusLYMwL+Z6bEa4Pbfs5IWDtQdHkArtfxEkIZz77944z8MgDxGw==
55015513

5514+
esbuild-plugin-replace@^1.4.0:
5515+
version "1.4.0"
5516+
resolved "https://registry.yarnpkg.com/esbuild-plugin-replace/-/esbuild-plugin-replace-1.4.0.tgz#2051eb35e21699e41dcf75630f613bdaa5039be6"
5517+
integrity sha512-lP3ZAyzyRa5JXoOd59lJbRKNObtK8pJ/RO7o6vdjwLi71GfbL32NR22ZuS7/cLZkr10/L1lutoLma8E4DLngYg==
5518+
dependencies:
5519+
magic-string "^0.25.7"
5520+
55025521
55035522
version "0.14.27"
55045523
resolved "https://registry.yarnpkg.com/esbuild-sunos-64/-/esbuild-sunos-64-0.14.27.tgz#8611d825bcb8239c78d57452e83253a71942f45c"
@@ -8927,6 +8946,13 @@ lz-string@^1.4.4:
89278946
resolved "https://registry.yarnpkg.com/lz-string/-/lz-string-1.4.4.tgz#c0d8eaf36059f705796e1e344811cf4c498d3a26"
89288947
integrity sha1-wNjq82BZ9wV5bh40SBHPTEmNOiY=
89298948

8949+
magic-string@^0.25.7:
8950+
version "0.25.9"
8951+
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.9.tgz#de7f9faf91ef8a1c91d02c2e5314c8277dbcdd1c"
8952+
integrity sha512-RmF0AsMzgt25qzqqLc1+MbHmhdx0ojF2Fvs4XnOqz2ZOBXzzkEwc/dJQZCYHAn7v1jbVOjAZfK8msRn4BxO4VQ==
8953+
dependencies:
8954+
sourcemap-codec "^1.4.8"
8955+
89308956
make-dir@^2.0.0, make-dir@^2.1.0:
89318957
version "2.1.0"
89328958
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-2.1.0.tgz#5f0310e18b8be898cc07009295a30ae41e91e6f5"
@@ -12084,6 +12110,11 @@ source-map@^0.6.0, source-map@^0.6.1, source-map@~0.6.1:
1208412110
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
1208512111
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
1208612112

12113+
sourcemap-codec@^1.4.8:
12114+
version "1.4.8"
12115+
resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.8.tgz#ea804bd94857402e6992d05a38ef1ae35a9ab4c4"
12116+
integrity sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==
12117+
1208712118
space-separated-tokens@^2.0.0:
1208812119
version "2.0.2"
1208912120
resolved "https://registry.yarnpkg.com/space-separated-tokens/-/space-separated-tokens-2.0.2.tgz#1ecd9d2350a3844572c3f4a312bceb018348859f"

0 commit comments

Comments
 (0)