v2 refacting

This commit is contained in:
simple321vip
2026-06-28 23:02:32 +08:00
parent 4da6832b44
commit 024f306636
14 changed files with 360 additions and 90 deletions
+68 -5
View File
@@ -7,16 +7,19 @@ import { fileURLToPath, URL } from 'url'
// https://vitejs.dev/guide/env-and-mode.html#env-files
export default defineConfig(({ mode, command }) => {
const env = loadEnv(mode, process.cwd())
// mock 开关:仅 servedev)模式 + .env.local 里 VITE_USE_MOCK=true
// 关掉时直接不注册 viteMockServe 插件(避免 mock routes 被注册)
const useMock = command === 'serve' && env.VITE_USE_MOCK === 'true'
return {
plugins: [
vue(),
// gzip 压缩
viteCompression(),
// 本地 mock 服务(仅 dev 启用,prod 不引入)
viteMockServe({
// 本地 mock 服务(由 VITE_USE_MOCK 控制;prod / 关 mock 时不引入)
...(useMock ? [viteMockServe({
mockPath: 'mock',
localEnabled: command === 'serve',
}),
localEnabled: true,
})] : []),
],
base: env.VITE_RES_URL,
resolve: {
@@ -31,6 +34,66 @@ export default defineConfig(({ mode, command }) => {
}
}
},
// ============================================================
// dev server proxy —— 本地多后端联调
// ============================================================
// ⚠️ 关键:proxy key 必须用 /api/ 后缀,避免拦截 SPA 路由
// 例如 /bookmark/api/v1/... → 8086(后端 API
// 但 /bookmark → 保留在前端(SPA 路由,由 history fallback 处理)
//
// 调试建议:浏览器开 Network,看 Server 响应头确认走了哪个后端
//
// 端口对齐:
// violin-auth: 8080 (context-path: /auth)
// violin-calendar: 8085 (context-path: /calendar)
// violin-bookmark: 8086 (context-path: /bookmark, 待你确认)
// violin-onenote: 8087 (context-path: /onenote, 待你确认)
// ... 新增服务时在这里加一行
server: {
host: '0.0.0.0',
port: 5173,
proxy: {
// auth: 业务 API 在 /api/v1/auth/...,没有 /auth/api 前缀
// 用 rewrite 把 /api/v1/auth/* 转成 /auth/api/v1/auth/*
// 后端 context-path 是 /auth,所以请求路径需要带 /auth 前缀
'^/api/v1/auth/.*': {
target: 'http://localhost:8080',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api\/v1\/auth/, '/auth/api/v1/auth'),
},
// 其他服务:path 自带服务名前缀(/calendar/api/...
'^/calendar/api/.*': {
target: 'http://localhost:8085',
changeOrigin: true,
},
'^/bookmark/api/.*': {
target: 'http://localhost:8086',
changeOrigin: true,
},
'^/onenote/api/.*': {
target: 'http://localhost:8087',
changeOrigin: true,
},
'^/trader/api/.*': {
target: 'http://localhost:8088',
changeOrigin: true,
},
'^/traderCalender/api/.*': {
target: 'http://localhost:8088',
changeOrigin: true,
},
'^/cloud/api/.*': {
target: 'http://localhost:8089',
changeOrigin: true,
},
'^/wiki/api/.*': {
target: 'http://localhost:8090',
changeOrigin: true,
},
},
},
build: {
// 拆 chunk,减少首屏加载
rollupOptions: {
@@ -44,4 +107,4 @@ export default defineConfig(({ mode, command }) => {
}
}
}
})
})