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

vite.config.ts

50 lines2.9 KB

1import { config as loadEnv } from 'dotenv'
2import { defineConfig } from 'vite'
3import { tanstackStart } from '@tanstack/react-start/plugin/vite'
4import { nitro } from 'nitro/vite'
5import viteReact from '@vitejs/plugin-react'
6import tailwindcss from '@tailwindcss/vite'
7​
8// The `/api/run` route reads ANTHROPIC_API_KEY / HARNESS / SANDBOX from
9// `process.env`. The dev SSR runs in this Vite process, so loading `.env.local`
10// (then `.env`) here makes those available without exporting them in the shell.
11// Already-set vars win, so a shell `export` still takes precedence.
12loadEnv({ path: ['.env.local', '.env'] })
13​
14// Plain Node TanStack Start (Nitro server) — no Cloudflare plugin. The whole app
15// runs as one Node process: the SSR UI, the `/api/run` route that drives the
16// agent, AND the host-side MCP tool-bridge the in-sandbox `claude` calls back on.
17//
18// Unlike the Cloudflare example, the tool-bridge is NOT this Vite/Nitro server —
19// the Claude Code adapter starts its OWN short-lived HTTP bridge per run (via
20// `nodeHttpBridgeProvisioner`) and tells the container to reach it at
21// `host.docker.internal:<bridgePort>` (the Docker provider adds the
22// `host.docker.internal:host-gateway` mapping by default). So this dev server
23// needs no special `host`/`allowedHosts` config.
24//
25// Browser PREVIEWS of the app the agent builds also bypass this server: the
26// Docker provider publishes the container's dev-server port to a random host
27// port, and `exposePreview` hands back the matching `http://localhost:<port>`.
28//
29// `dockerode` is a server-only dependency that pulls in optional native addons
30// (`ssh2` → `cpu-features`, a `.node` binary that this install does not compile).
31// At runtime `ssh2` catches the missing addon, but the bundlers don't:
32// • esbuild's dev-time dep pre-bundler (`optimizeDeps`) hard-errors trying to
33// resolve `cpufeatures.node` → `optimizeDeps.exclude` keeps it out of the scan.
34// • the SSR build would try to inline it → `ssr.external` keeps it a runtime
35// require. (Nitro 3 already externalizes node_modules into the traced server
36// output by default, so the production build needs no extra config.)
37// So `dockerode` is excluded from optimization AND SSR-externalized; it runs only
38// in the `/api/run` server route, never in the client bundle.
39const SERVER_ONLY_NATIVE = ['dockerode', '@ngrok/ngrok']
40​
41export default defineConfig({
42 optimizeDeps: { exclude: SERVER_ONLY_NATIVE },
43 ssr: { external: SERVER_ONLY_NATIVE },
44 // Keep `dockerode` external in every Rollup pass (the Nitro server build has its
45 // own, which `ssr.external` doesn't reach). Nitro still traces it into the
46 // node-server output, so it resolves at runtime; the client build never imports
47 // it, so this is a no-op there.
48 build: { rollupOptions: { external: SERVER_ONLY_NATIVE } },
49 plugins: [nitro(), tailwindcss(), tanstackStart(), viteReact()],
50})