Skip to content

build: restore version replacement #2698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 6 additions & 3 deletions scripts/bundle-cjs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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' };

Expand Down Expand Up @@ -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);
7 changes: 2 additions & 5 deletions scripts/bundle-esm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@ 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);
}),
);

console.log('ESM build complete');
};

bundleEsm().catch(console.error);
bundleEsm();
11 changes: 8 additions & 3 deletions scripts/get-package-version.mjs
Original file line number Diff line number Diff line change
@@ -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') {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Chat/__tests__/Chat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
);
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/components/Chat/hooks/useChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down
19 changes: 19 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5386,6 +5386,13 @@ [email protected]:
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"

[email protected]:
version "0.14.27"
resolved "https://registry.yarnpkg.com/esbuild-sunos-64/-/esbuild-sunos-64-0.14.27.tgz#8611d825bcb8239c78d57452e83253a71942f45c"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
Loading