优化vite配置文件

This commit is contained in:
jingrow 2025-11-03 23:36:36 +08:00
parent d27cb3d4ca
commit 1cbedb3420
2 changed files with 11 additions and 10 deletions

View File

@ -1,5 +0,0 @@
## 前端环境变量 (Vite会读取VITE_前缀的变量)
# 开发环境前端使用相对路径通过Vite代理转发
# 生产环境:前端直接请求后端服务器
VITE_JINGROW_SERVER_URL=
VITE_BACKEND_SERVER_URL=

View File

@ -1,4 +1,4 @@
import { defineConfig } from 'vite'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import { fileURLToPath, URL } from 'node:url'
@ -27,7 +27,12 @@ const currentDir = fileURLToPath(new URL('.', import.meta.url))
const appsDir = path.resolve(currentDir, '..', '..')
const APPS_ORDER = loadAppsOrder(appsDir)
export default defineConfig({
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '')
const JINGROW_SERVER_URL = env.VITE_JINGROW_SERVER_URL
const BACKEND_URL = env.VITE_BACKEND_SERVER_URL
return {
plugins: [
vue(),
Icons({
@ -62,17 +67,17 @@ export default defineConfig({
},
proxy: {
'/api/action': {
target: process.env.VITE_JINGROW_SERVER_URL || 'https://simon.c1.site.jingrow.com',
target: JINGROW_SERVER_URL,
changeOrigin: true,
secure: false
},
'/api/data': {
target: process.env.VITE_BACKEND_SERVER_URL || 'http://localhost:9001',
target: BACKEND_URL,
changeOrigin: true,
secure: false
},
'/jingrow': {
target: process.env.VITE_BACKEND_SERVER_URL || 'http://localhost:9001',
target: BACKEND_URL,
changeOrigin: true,
secure: false
}
@ -88,4 +93,5 @@ export default defineConfig({
// 注入 apps.txt 的应用顺序到前端
__APPS_ORDER__: JSON.stringify(APPS_ORDER)
}
}
})