Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

vite.config.ts

81 lines2.6 KB

1import { defineConfig } from 'vite'
2import { tanstackStart } from '@tanstack/react-start/plugin/vite'
3import { nitro } from 'nitro/vite'
4import viteReact from '@vitejs/plugin-react'
5import tailwindcss from '@tailwindcss/vite'
6import { devtools } from '@tanstack/devtools-vite'
7​
8// Native / wasm / binary server-only modules that can't be bundled by esbuild
9// or rolldown. They stay external so the host runtime loads them.
10// Under pnpm, declare them as direct deps of this example so nitro can resolve
11// them (see package.json).
12const SERVER_ONLY_NATIVE = [
13 'isolated-vm',
14 'esbuild',
15 'puppeteer',
16 'quickjs-emscripten',
17 'quickjs-emscripten-core',
18 '@jitl/quickjs-wasmfile-release-asyncify',
19 '@jitl/quickjs-wasmfile-release-sync',
20 '@jitl/quickjs-wasmfile-debug-asyncify',
21 '@jitl/quickjs-wasmfile-debug-sync',
22 // Bun-native QuickJS (exports.bun only) — host Bun loads it, not Vite.
23 'quickjs-bun',
24 '@tanstack/ai-isolate-quickjs-bun',
25 'bun:ffi',
27​
28/** `CODE_MODE_BUN=1` or running the Vite CLI under Bun enables Bun isolate defaults. */
29const codeModeBun =
30 process.env.CODE_MODE_BUN === '1' ||
31 typeof (process.versions as { bun?: string }).bun === 'string'
32​
33/**
34 * Same lessons as the Bun Code Mode path:
35 *
36 * - Do **not** put `bun` in top-level `resolve.conditions`. router-core's
37 * `isServer` maps `bun` → server build; the browser would get isServer=true,
38 * createRouter never builds a store, hydrateStart crashes on `router.state`.
39 * - Put `bun` only on `ssr.resolve.conditions` so `quickjs-bun` resolves under Bun.
40 * - Externalize quickjs-bun + the isolate driver so Vite never inlines them.
41 *
42 * `pnpm dev:bun` → CODE_MODE_BUN=1 bun --bun vite dev
43 */
44export default defineConfig({
45 define: {
46 'import.meta.env.VITE_CODE_MODE_BUN': JSON.stringify(
47 codeModeBun ? '1' : '',
48 ),
49 'import.meta.env.VITE_CODE_MODE_DEFAULT_VM': JSON.stringify(
50 process.env.CODE_MODE_DEFAULT_VM ?? '',
51 ),
52 },
53 resolve: {
54 tsconfigPaths: true,
55 // Client: browser/import only — never prefer `bun` here.
56 conditions: ['import', 'module', 'browser', 'default'],
57 },
58 plugins: [
59 devtools(),
60 // https://bun.com/docs/guides/ecosystem/tanstack-start
61 nitro(codeModeBun ? { preset: 'bun' } : {}),
62 tailwindcss(),
63 tanstackStart(),
64 viteReact(),
65 ],
66 ssr: {
67 external: SERVER_ONLY_NATIVE,
68 resolve: {
69 // Server under Bun: need `bun` for quickjs-bun's export map.
70 conditions: ['bun', 'node', 'import', 'module', 'default'],
71 },
72 },
73 optimizeDeps: {
74 exclude: [
75 'isolated-vm',
76 'quickjs-emscripten',
77 'quickjs-bun',
78 '@tanstack/ai-isolate-quickjs-bun',
79 ],
80 },
81})