import path from "path"; import react from "@vitejs/plugin-react"; import { defineConfig, loadEnv } from "vite"; const watchIgnored = [ "**/node_modules/**", "**/dist/**", "**/output/**", "**/.playwright-cli/**", "**/playwright-report/**", "**/test-results/**", ]; export default defineConfig(({ mode }) => { const env = loadEnv(mode, process.cwd(), ""); // 本地默认优先对齐 Docker 开发栈:backend 暴露在 8001 -> 容器内 8000。 // 若开发者直跑后端,可通过 VITE_DEV_API_PROXY_TARGET 覆盖回 8000。 const proxyTarget = env.VITE_DEV_API_PROXY_TARGET || "http://127.0.0.1:8001"; const usePolling = String(env.CHOKIDAR_USEPOLLING || "false").toLowerCase() === "true"; return { plugins: [react()], build: { rollupOptions: { output: { manualChunks(id) { const normalizedId = id.split(path.sep).join("/"); if ( normalizedId.includes("/node_modules/react/") || normalizedId.includes("/node_modules/react-dom/") || normalizedId.includes("/node_modules/react-router-dom/") || normalizedId.includes("/node_modules/@remix-run/router/") ) { return "framework"; } if ( normalizedId.includes("/node_modules/@tanstack/react-query/") || normalizedId.includes("/node_modules/zustand/") || normalizedId.includes("/node_modules/axios/") ) { return "state-query"; } if (normalizedId.includes("/node_modules/@radix-ui/")) { return "ui-radix"; } if (normalizedId.includes("/node_modules/lucide-react/")) { return "icons"; } if (normalizedId.includes("/node_modules/recharts/")) { return "charts"; } if ( normalizedId.includes("/node_modules/react-markdown/") || normalizedId.includes("/node_modules/remark-gfm/") || normalizedId.includes("/node_modules/remark-breaks/") ) { return "markdown"; } return undefined; }, }, }, }, test: { environment: "jsdom", setupFiles: "./vitest.setup.ts", globals: false, }, resolve: { alias: { "@": path.resolve(__dirname, "./src"), }, }, server: { port: 5174, allowedHosts: true, watch: { ignored: watchIgnored, ...(usePolling ? { usePolling: true, interval: 250, } : {}), }, proxy: { "/api": { target: proxyTarget, changeOrigin: true, rewrite: (path) => path.replace(/^\/api/, ""), }, }, }, }; });