2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 06:47:20 +08:00

feat: 开发服务器代理支持多目标

This commit is contained in:
耗子
2024-10-20 19:55:14 +08:00
parent d51d11f460
commit 892f91be3e
3 changed files with 32 additions and 23 deletions

View File

@@ -1,17 +1,20 @@
import type { ProxyOptions } from 'vite'
import { getProxyConfig } from '../../settings/proxy-config'
import { getProxyConfigs } from '../../settings/proxy-config'
export function createViteProxy(isUseProxy = true, proxyType: ProxyType) {
if (!isUseProxy) return undefined
const proxyConfig = getProxyConfig(proxyType)
const proxy: Record<string, string | ProxyOptions> = {
[proxyConfig.prefix]: {
const proxyConfigs = getProxyConfigs(proxyType)
const proxy: Record<string, string | ProxyOptions> = {}
proxyConfigs.forEach((proxyConfig) => {
proxy[proxyConfig.prefix] = {
target: proxyConfig.target,
secure: proxyConfig.secure,
changeOrigin: true,
rewrite: (path: string) => path.replace(new RegExp(`^${proxyConfig.prefix}`), '')
}
}
})
return proxy
}

View File

@@ -0,0 +1,24 @@
const proxyConfigMappings: Record<ProxyType, ProxyConfig> = {
dev: [
{
prefix: '/api',
target: 'http://localhost:8080'
}
],
test: [
{
prefix: '/api',
target: 'http://localhost:8080'
}
],
prod: [
{
prefix: '/api',
target: 'http://localhost:8080'
}
]
}
export function getProxyConfig(envType: ProxyType = 'dev'): ProxyConfig {
return proxyConfigMappings[envType]
}

View File

@@ -1,18 +0,0 @@
const proxyConfigMappings: Record<ProxyType, ProxyConfig> = {
dev: {
prefix: '/api',
target: 'http://localhost:8080'
},
test: {
prefix: '/api',
target: 'http://localhost:8080'
},
prod: {
prefix: '/api',
target: 'http://localhost:8080'
}
}
export function getProxyConfig(envType: ProxyType = 'dev'): ProxyConfig {
return proxyConfigMappings[envType]
}