File size: 1,680 Bytes
8d3471e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'

const webuiDir = dirname(fileURLToPath(import.meta.url))
const repoRoot = resolve(webuiDir, '..')

export default defineConfig(({ mode }) => ({
    plugins: [
        react(),
    ],
    server: {
        port: 5173,
        fs: {
            allow: [repoRoot],
        },
        proxy: {
            // 代理 /admin 下的 API 请求到后端
            '/admin': {
                target: 'http://localhost:5001',
                changeOrigin: true,
                // 只代理 API 请求,页面请求返回 false 让 Vite 处理
                bypass(req, res, proxyOptions) {
                    const url = req.url
                    // 精确的 /admin 或 /admin/ 是页面请求,不代理
                    if (url === '/admin' || url === '/admin/' || url === '/admin?') {
                        console.log('[Vite Proxy] Bypass (page):', url)
                        return '/index.html'
                    }
                    // 其他 /admin/* 路径都是 API 请求,代理到后端
                    console.log('[Vite Proxy] Proxy to backend:', url)
                    // 返回 undefined 或 null 表示不跳过代理
                },
            },
            '/v1': {
                target: 'http://localhost:5001',
                changeOrigin: true,
            },
        },
    },
    build: {
        outDir: '../static/admin',
        emptyOutDir: true,
    },
    // Use / for dev, /admin/ for production build
    base: mode === 'production' ? '/admin/' : '/',
}))