feat(frontend): 重构本地调试代理配置
将 `packages/frontend` 的 Vite 开发代理改为可通过环境变量 切换目标地址,并补充本地联调所需的默认主题与样式更新。 同时同步更新前后端默认 UI 主题定义,便于在本地直接验证 远端接口、WebSocket 与视觉效果。
This commit is contained in:
@@ -1,35 +1,41 @@
|
||||
import { defineConfig } from 'vite';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { dirname } from 'node:path';
|
||||
import { defineConfig, loadEnv } from 'vite';
|
||||
import vue from '@vitejs/plugin-vue';
|
||||
import monacoEditorPlugin from 'vite-plugin-monaco-editor';
|
||||
import tailwindcss from '@tailwindcss/vite'
|
||||
|
||||
const frontendRoot = dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
// https://vitejs.dev/config/
|
||||
export default defineConfig({
|
||||
plugins: [
|
||||
vue(),
|
||||
tailwindcss(),
|
||||
// @ts-ignore because the plugin type might not perfectly match Vite's expected PluginOption type
|
||||
(monacoEditorPlugin as any).default({})
|
||||
],
|
||||
server: {
|
||||
proxy: {
|
||||
// 将所有 /api 开头的请求代理到后端服务器
|
||||
'/api': {
|
||||
target: 'http://localhost:3001', // 后端服务器地址
|
||||
changeOrigin: true, // 需要虚拟主机站点
|
||||
// 可选:如果后端 API 路径没有 /api 前缀,可以在这里重写路径
|
||||
// rewrite: (path) => path.replace(/^\/api/, '')
|
||||
},
|
||||
// 将所有 /uploads 开头的请求也代理到后端服务器
|
||||
'/uploads': {
|
||||
target: 'http://localhost:3001', // 后端服务器地址
|
||||
changeOrigin: true, // 对于静态资源通常也建议开启
|
||||
// 通常不需要重写静态资源的路径
|
||||
},
|
||||
'/ws': {
|
||||
target: 'ws://localhost:3001', // 后端 WebSocket 服务器地址
|
||||
ws: true,
|
||||
changeOrigin: true,
|
||||
export default defineConfig(({ mode }) => {
|
||||
const env = loadEnv(mode, frontendRoot, '');
|
||||
const proxyTarget = env.VITE_DEV_PROXY_TARGET || 'http://localhost:3001';
|
||||
const wsProxyTarget = env.VITE_DEV_WS_PROXY_TARGET || 'ws://localhost:3001';
|
||||
|
||||
return {
|
||||
plugins: [
|
||||
vue(),
|
||||
tailwindcss(),
|
||||
// @ts-ignore because the plugin type might not perfectly match Vite's expected PluginOption type
|
||||
(monacoEditorPlugin as any).default({})
|
||||
],
|
||||
server: {
|
||||
proxy: {
|
||||
'/api': {
|
||||
target: proxyTarget,
|
||||
changeOrigin: true,
|
||||
},
|
||||
'/uploads': {
|
||||
target: proxyTarget,
|
||||
changeOrigin: true,
|
||||
},
|
||||
'/ws': {
|
||||
target: wsProxyTarget,
|
||||
ws: true,
|
||||
changeOrigin: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user