import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import fs from 'node:fs' import path from 'node:path' // 后端端口单一真源:读后端 yaml 顶层 Port(改端口只改 etc/*.yaml 一处,开发态 proxy 自动联动)。 // 仅 npm run dev 时生效;npm run build 产物与 Go 网关同源,proxy 不参与。读不到时回退 fallback(行为不变)。 function backendPort(yamlRel, fallback) { try { const yamlPath = path.resolve(process.cwd(), yamlRel) const m = fs.readFileSync(yamlPath, 'utf-8').match(/^Port:\s*(\d+)\s*$/m) if (m) return Number(m[1]) } catch (e) { console.warn(`[vite.config] 未读到 ${yamlRel} 的 Port,proxy 回退 ${fallback}`) } return fallback } const apiTarget = `http://localhost:${backendPort('../etc/bj_power_mes-api.yaml', 8888)}` // 构建产物直接写入 Go 嵌入静态目录(main.go go:embed public) export default defineConfig({ plugins: [vue()], // 绝对路径:访问 /board 或 /board/ 时资源都从 /assets/ 加载, // 避免相对路径 ./assets 在子路径下被解析成 /board/assets 而回退成 index.html(MIME 错、白屏)。 base: '/', build: { outDir: '../public', // outDir 在 root 外,vite 默认不清空会堆积旧 chunk;设 true 让每次 build 先清空再写,避免嵌入 exe 携带冗余体积。 emptyOutDir: true }, server: { port: 7070, host: true, proxy: { '/api': { target: apiTarget, changeOrigin: true } } } })