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

vite.config.ts

97 lines4.6 KB

1import { defineConfig } from 'vite'
2import { tanstackStart } from '@tanstack/react-start/plugin/vite'
3import viteReact from '@vitejs/plugin-react'
4import tailwindcss from '@tailwindcss/vite'
5import { nitro } from 'nitro/vite'
6import { devtools } from '@tanstack/devtools-vite'
7​
8// `dockerode` is a server-only dependency that pulls in optional native addons
9// (`ssh2` → `cpu-features`, a `.node` binary that this install does not compile).
10// At runtime `ssh2` catches the missing addon, but the bundlers don't:
11// • esbuild's dev-time dep pre-bundler (`optimizeDeps`) hard-errors trying to
12// resolve `cpufeatures.node` → `optimizeDeps.exclude` keeps it out of the scan.
13// • the SSR build would try to inline it → `ssr.external` keeps it a runtime
14// require.
15// This example uses nitro 3. Its server build (rolldown) externalizes
16// node_modules by default, but must resolve each external to a real path.
17// Under pnpm these server-only deps are only reachable via a workspace
18// adapter's nested store dir, so a bare resolve from this example fails —
19// they are therefore declared as direct dependencies here (dockerode,
20// @anthropic-ai/sdk, @elevenlabs/elevenlabs-js) so nitro can resolve and
21// trace them. The vite-level guards below still cover the client/SSR passes.
22//
23// `@anthropic-ai/sdk` (via @tanstack/ai-claude-code) ships a
24// `tools/agent-toolset/fs-util.mjs` sub-module that imports `node:crypto`,
25// `node:fs`, `node:path` — hard build errors if Rollup tries to resolve them
26// in the client bundle. The SDK is only used server-side; mark it external so
27// both the client Rollup pass and the SSR/nitro build resolve it at runtime.
28// `@ngrok/ngrok` (used by src/ngrok-bridge.ts to tunnel the tool bridge to cloud
29// sandboxes) loads a platform-specific `.node` native binary — esbuild's
30// optimizer has no loader for it and the SSR/nitro builds must not inline it.
31const SERVER_ONLY_NATIVE = ['dockerode', '@anthropic-ai/sdk', '@ngrok/ngrok']
32​
33// Dev-only workaround for a nitro dev-middleware heuristic.
34//
35// `nitro/dist/_build/vite.dev.mjs` decides whether a request is a static asset
36// (and so must NOT reach the server) from `Sec-Fetch-Dest`:
37//
38// isAsset = fetchDest && fetchDest !== 'empty'
39// ? !/^(?:document|iframe|frame)$/.test(fetchDest)
40// : isAssetByExt
41//
42// Anything that isn't a document — `image`, `audio`, `video`, `script`, `font`
43// — is treated as an asset and falls through to vite's static middleware,
44// which has no file to serve and 404s with the connect `Cannot GET` page. The
45// extension branch is only consulted when the header is absent/`empty`, so
46// renaming the route doesn't help.
47//
48// It only bites routes nitro sees under Start's catch-all `/**` (an explicitly
49// registered nitro route short-circuits the check), which is every server route
50// here. So `<img src="/api/artifacts?id=…">` 404s in dev while the same URL
51// fetched from JS returns the bytes — and production is unaffected, since this
52// middleware only exists in the vite dev server.
53//
54// Presenting `empty` for our own API paths routes them back to the server
55// without changing what the browser actually sends.
56const nitroServeApiToSubresources = {
57 name: 'nitro-serve-api-to-subresources',
58 enforce: 'pre',
59 apply: 'serve',
60 configureServer(server) {
61 server.middlewares.use((req, _res, next) => {
62 if (req.url?.startsWith('/api/')) req.headers['sec-fetch-dest'] = 'empty'
63 next()
64 })
65 },
66} as const satisfies import('vite').PluginOption
67​
68const config = defineConfig({
69 optimizeDeps: { exclude: SERVER_ONLY_NATIVE },
70 // Server-side only fix. @elevenlabs/elevenlabs-js ships a top-level
71 // `function getHeader(…)` that collides with h3's auto-imported
72 // `getHeader` when vite inlines it into the SSR bundle. The SDK is
73 // only imported by server-side adapter factories (see
74 // `src/lib/server-audio-adapters.ts`), so tree-shaking already keeps
75 // it out of the client bundle — this option only affects the SSR
76 // build, where we want the SDK resolved at runtime via require()
77 // instead of inlined into the rollup chunk.
78 // `dockerode` is also SSR-externalized (see comment above).
79 ssr: {
80 external: ['@elevenlabs/elevenlabs-js', ...SERVER_ONLY_NATIVE],
81 },
82 // Keep `dockerode` external in every Rollup pass (the Nitro server build has its
83 // own, which `ssr.external` doesn't reach). The client build never imports it, so
84 // this is a no-op there.
85 build: { rollupOptions: { external: SERVER_ONLY_NATIVE } },
86 resolve: { tsconfigPaths: true },
87 plugins: [
88 nitroServeApiToSubresources,
89 devtools(),
90 nitro(),
91 tailwindcss(),
92 tanstackStart(),
93 viteReact(),
94 ],
95})
96​
97export default config