import { defineConfig } from "vite"; import react from "@vitejs/plugin-react"; // Vignette web — Vite + React 19. // 개발 시 /api, /auth 프록시로 FastAPI(8000) 백엔드에 붙는다. // 쿠키 인증(__Host-vignette_sid)을 위해 changeOrigin + 쿠키 전달. const nodeEnv = ( globalThis as unknown as { process?: { env?: { VITE_ALLOWED_HOSTS?: string; VITE_API_PROXY_TARGET?: string } }; } ).process?.env; // 공개 배포 호스트(cloudflared 터널 대상)도 기본 허용에 포함한다. // vite 6 preview 서버는 Host 헤더를 allowedHosts로 검증하므로, 누락 시 // "Blocked request. This host is not allowed." 403이 반환되어 배포 도메인이 // 통째로 블랭크가 된다. server(dev)와 preview(배포) 모두 같은 목록을 쓴다. const defaultAllowedHosts = [ "alpaca-home.taile93291.ts.net", "vnet.18ka.net", "api-vnet.18ka.net", "vignette.chanpaca.net", ]; const allowedHosts = Array.from( new Set([ ...defaultAllowedHosts, ...(nodeEnv?.VITE_ALLOWED_HOSTS ?? "") .split(",") .map((host: string) => host.trim()) .filter(Boolean), ]), ); const apiProxyTarget = nodeEnv?.VITE_API_PROXY_TARGET?.trim() || "http://127.0.0.1:8000"; export default defineConfig({ plugins: [react()], server: { port: 5173, ...(allowedHosts.length ? { allowedHosts } : {}), proxy: { "/api": { target: apiProxyTarget, changeOrigin: true, ws: true, configure(proxy) { proxy.on("proxyReq", (proxyReq, req) => { const forwardedHost = req.headers.host; if (forwardedHost) { proxyReq.setHeader("x-forwarded-host", forwardedHost); const hostName = String(forwardedHost).split(":")[0]; const forwardedProto = allowedHosts.includes(hostName) ? "https" : "http"; proxyReq.setHeader("x-forwarded-proto", forwardedProto); } }); }, // 백엔드 라우트는 /auth, /sessions, /health 로 노출되므로 /api 프리픽스 제거. rewrite: (path) => path.replace(/^\/api/, ""), }, }, }, preview: { port: 5174, // 배포는 `vite preview`를 cloudflared 터널로 공개 도메인에 연결한다. // allowedHosts가 없으면 공개 Host가 차단되어 전체 화면이 블랭크가 된다. ...(allowedHosts.length ? { allowedHosts } : {}), }, build: { outDir: "dist", sourcemap: false, }, });