import { defineConfig } from 'vite' import react from '@vitejs/plugin-react-swc' import { readFileSync } from 'node:fs' import { fileURLToPath } from 'node:url' const repoRoot = fileURLToPath(new URL('..', import.meta.url)) const devApi = process.env.VITE_DEV_API ?? 'http://127.0.0.1:20482' /** * Expose the repository readme to the app as a virtual module. * * The landing page is the readme, so that there is exactly one description of * what this service is: a copy under `web/` would drift from the real one, and * the drift would be invisible until somebody noticed the site disagreeing * with the repository. */ function repoReadme() { const id = 'virtual:repo-readme' const resolved = '\0' + id return { name: 'repo-readme', resolveId: (source: string) => (source === id ? resolved : null), load(this: { addWatchFile?: (f: string) => void }, requested: string) { if (requested !== resolved) return null const path = repoRoot + 'readme.md' this.addWatchFile?.(path) return `export default ${JSON.stringify(readFileSync(path, 'utf8'))}` }, } } export default defineConfig({ plugins: [react(), repoReadme()], build: { outDir: 'dist', sourcemap: false, rollupOptions: { output: { // Split the dependencies that never change from the app that does, so // a redeploy does not invalidate a viewer's cached copy of React and // the markdown stack. manualChunks: { react: ['react', 'react-dom', 'react-router-dom'], markdown: ['react-markdown', 'remark-gfm', 'rehype-raw', 'rehype-sanitize'], }, }, }, }, server: { // Dev server only: proxy to a running rustingface so the app is exercised // against the real thing rather than a fixture. Mirrors what the nginx // vhost does in production — including the resolve paths, which is how // model cards are read. proxy: { '/v1': { target: devApi, changeOrigin: true }, '/api': { target: devApi, changeOrigin: true }, '/healthz': { target: devApi, changeOrigin: true }, // `/{namespace}/{name}/resolve/...` and its bare-name form. '^/[^/]+/(?:[^/]+/)?resolve/': { target: devApi, changeOrigin: true }, }, }, })