import { defineConfig, loadEnv } from 'vite'; import vue from '@vitejs/plugin-vue'; // 开发态默认把 /api 与 /ws/signal 代理到信令服务端,避免本地跨域; // 通过 .env 的 VITE_API_BASE 指向后端(如 http://localhost:8080), // 浏览器侧始终走同源相对路径 /api,无需在代码中写死后端地址。 export default defineConfig(({ mode }) => { const env = loadEnv(mode, process.cwd(), ''); // VITE_API_BASE 为空(开发态推荐)时,回退到本机后端。 const target = env.VITE_API_BASE || 'http://localhost:8080'; const wsTarget = target.replace(/^http/, 'ws'); return { plugins: [vue()], server: { host: true, port: 5173, proxy: { '/api': { target, changeOrigin: true, secure: false }, '/ws/signal': { target: wsTarget, ws: true, changeOrigin: true, secure: false }, }, }, }; });