fix: 修复正式环境地址错误

This commit is contained in:
chansee97 2025-08-20 17:43:28 +08:00
parent 79501a53f0
commit 050ab3e7ed
6 changed files with 14 additions and 24 deletions

View File

@ -56,7 +56,7 @@ export default function createServiceProxyPlugin(options: ServiceProxyPluginOpti
return {
name: 'vite-auto-proxy',
config(config: UserConfig, { command }: { mode: string, command: 'build' | 'serve' }) {
config(config: UserConfig, { mode, command }: { mode: string, command: 'build' | 'serve' }) {
// 只在开发环境serve命令时生成代理配置
const isDev = command === 'serve'
@ -66,9 +66,11 @@ export default function createServiceProxyPlugin(options: ServiceProxyPluginOpti
}
if (!enableProxy || !isDev) {
// 在非开发环境下生成原始地址映射path 和 rawPath 都是原始地址)
// 在非开发环境下,根据构建模式选择正确的环境配置
const targetEnv = isDev ? devEnvName : mode
const rawMapping: ProxyMapping = {}
const envConfig = serviceConfig[devEnvName]
const envConfig = serviceConfig[targetEnv]
if (envConfig) {
Object.entries(envConfig).forEach(([serviceName, serviceUrl]) => {
rawMapping[serviceName] = {
@ -76,7 +78,12 @@ export default function createServiceProxyPlugin(options: ServiceProxyPluginOpti
rawPath: serviceUrl,
}
})
console.warn(`[auto-proxy] 已加载 ${Object.keys(envConfig).length} 个服务地址`)
}
else {
console.warn(`[auto-proxy] 未找到环境 "${targetEnv}" 的配置`)
}
config.define[envName] = JSON.stringify(rawMapping)
// 生成 d.ts 类型文件(如果指定了路径)

View File

@ -1,17 +0,0 @@
[build]
publish = "dist"
command = "vite build --mode prod"
[build.environment]
NODE_VERSION = "20"
[[redirects]]
from = "/*"
to = "/index.html"
status = 200
[[headers]]
for = "/manifest.webmanifest"
[headers.values]
Content-Type = "application/manifest+json"

View File

@ -38,8 +38,8 @@
],
"scripts": {
"dev": "vite --mode dev --port 9980",
"dev:prod": "vite --mode prod",
"build": "vite build --mode prod",
"dev:prod": "vite --mode production",
"build": "vite build",
"build:dev": "vite build --mode dev",
"preview": "vite preview --port 9981",
"lint": "eslint . && vue-tsc --noEmit",

View File

@ -3,7 +3,7 @@ export const serviceConfig: Record<ServiceEnvType, Record<string, string>> = {
dev: {
url: 'http://localhost:3000',
},
prod: {
production: {
url: 'https://mock.apifox.cn/m1/4071143-0-default',
},
}

View File

@ -3,7 +3,7 @@
* - dev: 后台开发环境
* - prod: 后台生产环境
*/
type ServiceEnvType = 'dev' | 'prod'
type ServiceEnvType = 'dev' | 'production'
interface ImportMetaEnv {
/** 项目基本地址 */