-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
44 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
# 是否开启服务接口代理 Y | N | ||
VITE_HTTP_PROXY=N |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,32 @@ | ||
import type { ProxyOptions } from 'vite' | ||
import { mapEntries } from 'radash' | ||
|
||
/** 不同请求服务的环境配置 */ | ||
export const proxyConfig: Record<ServiceEnvType, ServiceEnvConfig> = { | ||
dev: { | ||
url: 'https://mock.apifox.com/m1/4071143-0-default', | ||
urlPattern: '/url-pattern', | ||
}, | ||
test: { | ||
url: 'http://localhost:8080', | ||
urlPattern: '/url-pattern', | ||
}, | ||
prod: { | ||
url: 'https://mock.apifox.com/m1/4071143-0-default', | ||
urlPattern: '/url-pattern', | ||
}, | ||
export function generateProxyPattern(envConfig: Record<string, string>) { | ||
return mapEntries(envConfig, (key, value) => { | ||
return [ | ||
key, | ||
{ | ||
value, | ||
proxy: `/proxy-${key}`, | ||
}, | ||
] | ||
}) | ||
} | ||
|
||
/** | ||
* @description: 生成vite代理字段 | ||
* @param {*} envConfig - 环境变量配置 | ||
*/ | ||
export function createViteProxy(envConfig: ServiceEnvConfig) { | ||
const proxy: Record<string, string | ProxyOptions> = { | ||
[envConfig.urlPattern]: { | ||
target: envConfig.url, | ||
changeOrigin: true, | ||
rewrite: path => path.replace(new RegExp(`^${envConfig.urlPattern}`), ''), | ||
}, | ||
} | ||
|
||
return proxy | ||
export function createViteProxy(envConfig: Record<string, string>) { | ||
const proxyMap = generateProxyPattern(envConfig) | ||
return mapEntries(proxyMap, (key, value) => { | ||
return [ | ||
value.proxy, | ||
{ | ||
target: value.value, | ||
changeOrigin: true, | ||
rewrite: (path: string) => path.replace(new RegExp(`^${value.proxy}`), ''), | ||
}, | ||
] | ||
}) as Record<string, string | ProxyOptions> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** 不同请求服务的环境配置 */ | ||
export const serviceConfig: Record<ServiceEnvType, Record<string, string>> = { | ||
dev: { | ||
url: 'https://mock.apifox.com/m1/4071143-0-default', | ||
}, | ||
test: { | ||
url: 'https://mock.apifox.com/m1/4071143-0-default', | ||
}, | ||
prod: { | ||
url: 'https://mock.apifox.com/m1/4071143-0-default', | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters