Skip to content

Commit f2d7096

Browse files
Restore version replacement
1 parent 234eafc commit f2d7096

File tree

7 files changed

+38
-13
lines changed

7 files changed

+38
-13
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@
219219
"dotenv": "^8.6.0",
220220
"emoji-mart": "^5.5.2",
221221
"esbuild": "^0.23.1",
222+
"esbuild-plugin-replace": "^1.4.0",
222223
"eslint": "^9.16.0",
223224
"eslint-plugin-import": "^2.31.0",
224225
"eslint-plugin-jest": "^28.11.0",

scripts/bundle-cjs.mjs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { dirname, resolve } from 'node:path';
44
import { fileURLToPath } from 'node:url';
55
import * as esbuild from 'esbuild';
6+
import { replace } from 'esbuild-plugin-replace';
67
import getPackageVersion from './get-package-version.mjs';
78
import packageJson from '../package.json' with { type: 'json' };
89

@@ -56,9 +57,11 @@ const bundles = ['browser', 'node'].map((platform) =>
5657
...cjsBundleConfig,
5758
entryNames: `[dir]/[name].${platform}`,
5859
platform,
59-
define: {
60-
'process.env.STREAM_CHAT_REACT_VERSION': JSON.stringify(getPackageVersion()),
61-
},
60+
plugins: [
61+
replace({
62+
__STREAM_CHAT_REACT_VERSION__: getPackageVersion(),
63+
}),
64+
],
6265
}),
6366
);
6467
await Promise.all(bundles);

scripts/bundle-esm.mjs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@ const bundleEsm = async () => {
2121
await Promise.all(
2222
files.map(async (file) => {
2323
const content = await readFile(file, 'utf8');
24-
const newContent = content.replace(
25-
/process.env.STREAM_CHAT_REACT_VERSION/g,
26-
JSON.stringify(version),
27-
);
24+
const newContent = content.replace(/__STREAM_CHAT_REACT_VERSION__/g, version);
2825
await writeFile(file, newContent);
2926
}),
3027
);
3128

3229
console.log('ESM build complete');
3330
};
3431

35-
bundleEsm().catch(console.error);
32+
bundleEsm();

scripts/get-package-version.mjs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import { execSync } from 'node:child_process';
22
import packageJson from '../package.json' with { type: 'json' };
33

4-
// 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
4+
// get the latest version so that "__STREAM_CHAT_REACT_VERSION__" can
5+
// be replaced with it in the source code (used for reporting purposes)
6+
// see bundle-cjs.mjs/bundle-esm.mjs for source
57
export default function getPackageVersion() {
6-
// "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
7-
let version = packageJson.version;
8+
// "build" script ("prepack" hook) gets invoked when semantic-release
9+
// runs "npm publish", at that point package.json#version already
10+
// contains updated next version which we can use (npm populates env
11+
// with npm_package_version which could be used also)
12+
let version = packageJson.version; // process.env.npm_package_version
813

914
// if it fails (loads a default), try pulling version from git
1015
if (version === '0.0.0-development') {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('Chat', () => {
6464
expect(context.openMobileNav).toBeInstanceOf(Function);
6565
expect(context.closeMobileNav).toBeInstanceOf(Function);
6666
expect(context.client.getUserAgent()).toBe(
67-
`stream-chat-react-undefined-${originalUserAgent}`,
67+
`stream-chat-react-__STREAM_CHAT_REACT_VERSION__-${originalUserAgent}`,
6868
);
6969
});
7070
});

src/components/Chat/hooks/useChat.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export const useChat = ({
5555
useEffect(() => {
5656
if (!client) return;
5757

58-
const version = process.env.STREAM_CHAT_REACT_VERSION;
58+
const version = '__STREAM_CHAT_REACT_VERSION__';
5959

6060
const userAgent = client.getUserAgent();
6161
if (!userAgent.includes('stream-chat-react')) {

yarn.lock

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5386,6 +5386,13 @@ esbuild-openbsd-64@0.14.27:
53865386
resolved "https://registry.yarnpkg.com/esbuild-openbsd-64/-/esbuild-openbsd-64-0.14.27.tgz#e99f8cdc63f1628747b63edd124d53cf7796468d"
53875387
integrity sha512-xwSje6qIZaDHXWoPpIgvL+7fC6WeubHHv18tusLYMwL+Z6bEa4Pbfs5IWDtQdHkArtfxEkIZz77944z8MgDxGw==
53885388

5389+
esbuild-plugin-replace@^1.4.0:
5390+
version "1.4.0"
5391+
resolved "https://registry.yarnpkg.com/esbuild-plugin-replace/-/esbuild-plugin-replace-1.4.0.tgz#2051eb35e21699e41dcf75630f613bdaa5039be6"
5392+
integrity sha512-lP3ZAyzyRa5JXoOd59lJbRKNObtK8pJ/RO7o6vdjwLi71GfbL32NR22ZuS7/cLZkr10/L1lutoLma8E4DLngYg==
5393+
dependencies:
5394+
magic-string "^0.25.7"
5395+
53895396
esbuild-sunos-64@0.14.27:
53905397
version "0.14.27"
53915398
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:
88528859
resolved "https://registry.yarnpkg.com/lz-string/-/lz-string-1.5.0.tgz#c1ab50f77887b712621201ba9fd4e3a6ed099941"
88538860
integrity sha512-h5bgJWpxJNswbU7qCrV0tIKQCaS3blPDrqKWx+QxzuzL1zGUzij9XCWLrSLsJPu5t+eWA/ycetzYAO5IOMcWAQ==
88548861

8862+
magic-string@^0.25.7:
8863+
version "0.25.9"
8864+
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.9.tgz#de7f9faf91ef8a1c91d02c2e5314c8277dbcdd1c"
8865+
integrity sha512-RmF0AsMzgt25qzqqLc1+MbHmhdx0ojF2Fvs4XnOqz2ZOBXzzkEwc/dJQZCYHAn7v1jbVOjAZfK8msRn4BxO4VQ==
8866+
dependencies:
8867+
sourcemap-codec "^1.4.8"
8868+
88558869
make-dir@^2.0.0, make-dir@^2.1.0:
88568870
version "2.1.0"
88578871
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:
1203312047
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
1203412048
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
1203512049

12050+
sourcemap-codec@^1.4.8:
12051+
version "1.4.8"
12052+
resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.8.tgz#ea804bd94857402e6992d05a38ef1ae35a9ab4c4"
12053+
integrity sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==
12054+
1203612055
space-separated-tokens@^2.0.0:
1203712056
version "2.0.2"
1203812057
resolved "https://registry.yarnpkg.com/space-separated-tokens/-/space-separated-tokens-2.0.2.tgz#1ecd9d2350a3844572c3f4a312bceb018348859f"

0 commit comments

Comments
 (0)