import { sveltekit } from '@sveltejs/kit/vite'; import { defineConfig } from 'vitest/config'; import { loadEnv } from 'vite'; export default defineConfig(({ mode }) => { // Load env file based on `mode` in the current working directory. // Set the third parameter to '' to load all env regardless of the `VITE_` prefix. const env = loadEnv(mode, process.cwd(), ''); const apiBaseUrl = env.VITE_API_BASE_URL || 'http://localhost:8000'; console.log('[Vite Config] API Proxy target:', apiBaseUrl); return { plugins: [sveltekit()], test: { include: ['src/**/*.{test,spec}.{js,ts}'], globals: true, environment: 'jsdom', setupFiles: ['./src/tests/setup.ts'], coverage: { provider: 'v8', reporter: ['text', 'json', 'html'], exclude: [ 'node_modules/', 'src/tests/', '**/*.d.ts', '**/*.config.*', '**/mockData', '**/.svelte-kit' ] } }, server: { port: 5173, strictPort: false, proxy: { // Proxy API requests to backend to avoid CORS issues in development // All requests to /api/* are forwarded to the backend '/api': { target: apiBaseUrl, changeOrigin: true, // Updates the Origin header to match the target rewrite: (path) => path.replace(/^\/api/, ''), // Remove /api prefix secure: false, // Allow self-signed certificates ws: true, // Proxy websockets configure: (proxy, options) => { proxy.on('error', (err, _req, _res) => { console.error('[Proxy Error]', err.message); console.error('[Proxy Error] Make sure backend is running at:', apiBaseUrl); }); proxy.on('proxyReq', (proxyReq, req, _res) => { console.log(`[Proxy] ${req.method} ${req.url} -> ${apiBaseUrl}${proxyReq.path}`); }); proxy.on('proxyRes', (proxyRes, req, _res) => { console.log(`[Proxy ✓] ${req.method} ${req.url} -> ${proxyRes.statusCode}`); }); } } } }, preview: { port: 4173, strictPort: false } }; });