Skip to content

Commit 521775c

Browse files
authored
fix: report correct package version in esm build (#2569)
1 parent cafed1f commit 521775c

File tree

5 files changed

+66
-25
lines changed

5 files changed

+66
-25
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@
263263
},
264264
"scripts": {
265265
"build": "rm -rf dist && yarn build-translations && yarn bundle",
266-
"bundle": "concurrently tsc ./scripts/copy-css.sh ./scripts/bundle.mjs",
266+
"bundle": "concurrently ./scripts/bundle-esm.mjs ./scripts/copy-css.sh scripts/bundle-cjs.mjs",
267267
"build-translations": "i18next",
268268
"coverage": "jest --collectCoverage && codecov",
269269
"eslint": "eslint '**/*.{js,md,ts,jsx,tsx}' --max-warnings 0",

scripts/bundle.mjs renamed to scripts/bundle-cjs.mjs

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

33
import { dirname, resolve } from 'node:path';
44
import { fileURLToPath } from 'node:url';
5-
import { execSync } from 'node:child_process';
65
import * as esbuild from 'esbuild';
76
import { replace } from 'esbuild-plugin-replace';
7+
import getPackageVersion from "./getPackageVersion.mjs";
88

99
// import.meta.dirname is not available before Node 20
1010
const __dirname = dirname(fileURLToPath(import.meta.url));
@@ -46,27 +46,6 @@ const cjsBundleConfig = {
4646
sourcemap: 'linked',
4747
};
4848

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-
7049

7150
// We build two CJS bundles: for browser and for node. The latter one can be
7251
// used e.g. during SSR (although it makes little sence to SSR chat, but still
@@ -78,7 +57,7 @@ const bundles = ['browser', 'node'].map((platform) =>
7857
platform,
7958
plugins: [
8059
replace({
81-
'__STREAM_CHAT_REACT_VERSION__': getVersion(),
60+
'__STREAM_CHAT_REACT_VERSION__': getPackageVersion(),
8261
}),
8362
],
8463
}),

scripts/bundle-esm.mjs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env node
2+
3+
import { exec } from 'node:child_process';
4+
import { readFile, writeFile } from 'node:fs/promises';
5+
import glob from 'glob';
6+
import { promisify } from 'node:util';
7+
import getPackageVersion from "./getPackageVersion.mjs";
8+
9+
const execAsync = promisify(exec);
10+
11+
const version = getPackageVersion();
12+
13+
const bundleEsm = async () => {
14+
// Run TypeScript compiler
15+
console.log('Running TypeScript compiler...');
16+
await execAsync('tsc');
17+
18+
// Replace version string in generated files
19+
console.log('Replacing version strings...');
20+
const files = glob.glob.sync('dist/**/*.js');
21+
await Promise.all(
22+
files.map(async (file) => {
23+
const content = await readFile(file, 'utf8');
24+
const newContent = content.replace(/__STREAM_CHAT_REACT_VERSION__/g, version);
25+
await writeFile(file, newContent);
26+
})
27+
);
28+
29+
console.log('ESM build complete');
30+
};
31+
32+
bundleEsm().catch(console.error);

scripts/getPackageVersion.mjs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { dirname, resolve } from 'node:path';
2+
import { fileURLToPath } from 'node:url';
3+
4+
// import.meta.dirname is not available before Node 20
5+
const __dirname = dirname(fileURLToPath(import.meta.url));
6+
7+
const packageJson = await import(resolve(__dirname, '../package.json'), {
8+
assert: { type: 'json' },
9+
});
10+
11+
// 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)
12+
export default function getPackageVersion() {
13+
let version;
14+
// During release, use the version being released
15+
// see .releaserc.json where the `NEXT_VERSION` env variable is set
16+
if (process.env.NEXT_VERSION) {
17+
version = process.env.NEXT_VERSION;
18+
} else {
19+
// Otherwise use the latest git tag
20+
try {
21+
version = execSync('git describe --tags --abbrev=0').toString().trim();
22+
} catch (error) {
23+
console.error(error);
24+
console.warn('Could not get latest version from git tags, falling back to package.json');
25+
version = packageJson.default.version;
26+
}
27+
}
28+
console.log(`Determined the build package version to be ${version}`);
29+
return version;
30+
};

src/components/Chat/hooks/useChat.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export const useChat = <
6161
const userAgent = client.getUserAgent();
6262
if (!userAgent.includes('stream-chat-react')) {
6363
// result looks like: 'stream-chat-react-2.3.2-stream-chat-javascript-client-browser-2.2.2'
64-
// __STREAM_CHAT_REACT_VERSION__ is replaced during the build process with the current version
64+
// the upper-case text between double underscores is replaced with the actual semantic version of the library
6565
client.setUserAgent(`stream-chat-react-__STREAM_CHAT_REACT_VERSION__-${userAgent}`);
6666
}
6767

0 commit comments

Comments
 (0)