import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import { readFileSync, existsSync } from 'fs' import { resolve } from 'path' // 从项目根目录的 .env 读取端口配置 function loadEnv() { const env = {} const envPath = resolve(__dirname, '../.env') if (existsSync(envPath)) { const content = readFileSync(envPath, 'utf-8') for (const line of content.split('\n')) { const trimmed = line.trim() if (trimmed && !trimmed.startsWith('#') && trimmed.includes('=')) { const idx = trimmed.indexOf('=') const key = trimmed.slice(0, idx).trim() const value = trimmed.slice(idx + 1).trim() env[key] = value } } } return env } const env = loadEnv() const backendPort = env.BACKEND_PORT || '8000' const frontendPort = env.FRONTEND_PORT || '3000' export default defineConfig({ plugins: [vue()], server: { port: parseInt(frontendPort), proxy: { '/api': `http://localhost:${backendPort}`, '/ws': { target: `ws://localhost:${backendPort}`, ws: true, }, }, }, build: { outDir: '../backend/static', }, })