mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 06:47:20 +08:00
feat: 开发服务器代理支持多目标
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
24
web/settings/proxy-config.example.ts
Normal file
24
web/settings/proxy-config.example.ts
Normal 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]
|
||||
}
|
||||
@@ -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]
|
||||
}
|
||||
Reference in New Issue
Block a user