Skip to content
Merged
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
3 changes: 2 additions & 1 deletion examples/vite/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
VITE_STREAM_KEY=""
VITE_USER_TOKEN=""
VITE_USER_ID=""
VITE_USER_ID=""
STREAM_CHAT_JS_PATH=""
81 changes: 79 additions & 2 deletions examples/vite/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,76 @@
import { existsSync, readFileSync } from 'node:fs';
import { createRequire } from 'node:module';
import { dirname, resolve } from 'node:path';
import { defineConfig, loadEnv } from 'vite';
import babel from 'vite-plugin-babel';
import react from '@vitejs/plugin-react';

const require = createRequire(import.meta.url);

// Resolve the actual installed package root from the example app's dependency graph.
// This avoids assuming a fixed sibling-repo layout and works with portal/symlinked installs.
const getPackageRoot = (packageName: string, fromDirectory: string) => {
const packageEntry = require.resolve(packageName, { paths: [fromDirectory] });
let currentDirectory = dirname(packageEntry);

while (true) {
const packageJsonPath = resolve(currentDirectory, 'package.json');

if (existsSync(packageJsonPath)) {
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8')) as {
name?: string;
};

if (packageJson.name === packageName) {
return currentDirectory;
}
}

const parentDirectory = dirname(currentDirectory);

if (parentDirectory === currentDirectory) {
throw new Error(`Could not locate package root for "${packageName}"`);
}

currentDirectory = parentDirectory;
}
};

// https://vitejs.dev/config/
export default defineConfig(() => {
export default defineConfig(({ mode }) => {
const rootDir = process.cwd();
const streamChatReactRoot = getPackageRoot('stream-chat-react', rootDir);

// Load shared .env file
const env = loadEnv('', rootDir, '');
const env = loadEnv(mode, rootDir, '');
const streamChatJsRoot = env.STREAM_CHAT_JS_PATH
? resolve(rootDir, env.STREAM_CHAT_JS_PATH)
: undefined;
const localStreamChatEntry = streamChatJsRoot
? resolve(streamChatJsRoot, 'dist/esm/index.mjs')
: undefined;
Comment on lines +46 to +51

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Missing sibling-checkout fallback in stream-chat-js resolution

The config currently resolves streamChatJsRoot only from STREAM_CHAT_JS_PATH. The documented workflow for this PR includes an automatic fallback to ../../../stream-chat-js/src/index.ts when env var is absent, but that branch is missing here.

Suggested patch
-  const streamChatJsRoot = env.STREAM_CHAT_JS_PATH
-    ? resolve(rootDir, env.STREAM_CHAT_JS_PATH)
-    : undefined;
-  const localStreamChatEntry = streamChatJsRoot
-    ? resolve(streamChatJsRoot, 'dist/esm/index.mjs')
-    : undefined;
+  const siblingStreamChatRoot = resolve(rootDir, '../../../stream-chat-js');
+  const streamChatJsRoot = env.STREAM_CHAT_JS_PATH
+    ? resolve(rootDir, env.STREAM_CHAT_JS_PATH)
+    : existsSync(resolve(siblingStreamChatRoot, 'src/index.ts'))
+      ? siblingStreamChatRoot
+      : undefined;
+  const localStreamChatEntry = streamChatJsRoot
+    ? env.STREAM_CHAT_JS_PATH
+      ? resolve(streamChatJsRoot, 'dist/esm/index.mjs')
+      : resolve(streamChatJsRoot, 'src/index.ts')
+    : undefined;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@examples/vite/vite.config.ts` around lines 46 - 51, The code only sets
streamChatJsRoot from env.STREAM_CHAT_JS_PATH; add the documented
sibling-checkout fallback so that when env.STREAM_CHAT_JS_PATH is undefined you
also try resolving '../../../stream-chat-js/src/index.ts' relative to rootDir
(or the containing repo) before leaving it undefined; update the
streamChatJsRoot resolution logic (the variable named streamChatJsRoot used to
compute localStreamChatEntry) to prefer env.STREAM_CHAT_JS_PATH and then fall
back to the sibling checkout path, then compute localStreamChatEntry from that
resolved path as before.


if (localStreamChatEntry && !existsSync(localStreamChatEntry)) {
throw new Error(
`STREAM_CHAT_JS_PATH must point to a built stream-chat-js checkout. Missing ${localStreamChatEntry}`,
);
}

return {
plugins: [
...(localStreamChatEntry
? [
{
enforce: 'pre' as const,
name: 'resolve-local-stream-chat',
resolveId(source: string) {
if (source === 'stream-chat') {
return localStreamChatEntry;
}
},
},
]
: []),
react(),
babel({
babelConfig: {
Expand All @@ -20,5 +81,21 @@ export default defineConfig(() => {
define: {
'process.env': env, // need `process.env` access
},
optimizeDeps: {
// Keep local `stream-chat` out of Vite's prebundle so the browser loads the
// SDK build directly and DevTools can follow its sourcemaps back to source files.
// Its local ESM build still imports a few CommonJS deps that need Vite interop.
include: ['base64-js', 'form-data', 'isomorphic-ws', 'axios'],
exclude: localStreamChatEntry ? ['stream-chat'] : [],
},
server: {
fs: {
allow: [
rootDir,
streamChatReactRoot,
...(streamChatJsRoot ? [streamChatJsRoot] : []),
],
},
},
};
});