Files
bj_power/bj_power_wms/frontend/vite.config.js
T

45 lines
1.5 KiB
JavaScript
Raw Normal View History

2026-08-28 15:06:01 +08:00
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 apiPort = backendPort('../etc/bj_power_wms-api.yaml', 8890)
const apiTarget = `http://localhost:${apiPort}`
2026-08-28 15:06:01 +08:00
// 构建产物直接写入 Go 网关的嵌入静态目录(go:embed all:web/static)
export default defineConfig({
plugins: [vue()],
build: {
outDir: '../web/static',
// outDir 在 root 外,vite 默认不清空会堆积旧 chunk;设 true 让每次 build 先清空再写,避免嵌入 exe 携带冗余体积。
emptyOutDir: true
2026-08-28 15:06:01 +08:00
},
server: {
port: 5173,
proxy: {
'/api': {
target: apiTarget,
2026-09-18 18:54:54 +08:00
changeOrigin: true
},
// 附件静态文件(PDF/图片预览):与 API 同源,避免开发态下 /uploads 打到 Vite 而 404
'/uploads': {
target: apiTarget,
2026-08-28 15:06:01 +08:00
changeOrigin: true
}
}
}
})