Compare commits

..

No commits in common. "ac9d7e84e5d84099280eb8ca2f2c6665cfbb52fd" and "79501a53f09bbb257c9886a7245efa16f2c29a4a" have entirely different histories.

7 changed files with 27 additions and 17 deletions

View File

@ -38,7 +38,7 @@ export interface ServiceProxyPluginOptions {
devEnvName?: ServiceEnvType
/** 是否启用代理配置 */
enableProxy?: boolean
/** 环境变量名(可选,默认为 '__URL_MAP__' */
/** 环境变量名(可选,默认为 '__PROXY_MAPPING__' */
envName?: string
/** d.ts 类型文件生成路径(可选,如果传入路径则在该路径生成 d.ts 类型文件) */
dts?: string
@ -50,13 +50,13 @@ export default function createServiceProxyPlugin(options: ServiceProxyPluginOpti
devEnvName = 'development',
proxyPrefix = 'proxy-',
enableProxy = true,
envName = '__URL_MAP__',
envName = '__PROXY_MAPPING__',
dts,
} = options
return {
name: 'vite-auto-proxy',
config(config: UserConfig, { mode, command }: { mode: string, command: 'build' | 'serve' }) {
config(config: UserConfig, { command }: { mode: string, command: 'build' | 'serve' }) {
// 只在开发环境serve命令时生成代理配置
const isDev = command === 'serve'
@ -66,11 +66,9 @@ export default function createServiceProxyPlugin(options: ServiceProxyPluginOpti
}
if (!enableProxy || !isDev) {
// 在非开发环境下,根据构建模式选择正确的环境配置
const targetEnv = isDev ? devEnvName : mode
// 在非开发环境下生成原始地址映射path 和 rawPath 都是原始地址)
const rawMapping: ProxyMapping = {}
const envConfig = serviceConfig[targetEnv]
const envConfig = serviceConfig[devEnvName]
if (envConfig) {
Object.entries(envConfig).forEach(([serviceName, serviceUrl]) => {
rawMapping[serviceName] = {
@ -78,12 +76,7 @@ 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 类型文件(如果指定了路径)

17
netlify.toml Normal file
View File

@ -0,0 +1,17 @@
[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 production",
"build": "vite build",
"dev:prod": "vite --mode prod",
"build": "vite build --mode prod",
"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',
},
production: {
prod: {
url: 'https://mock.apifox.cn/m1/4071143-0-default',
},
}

View File

@ -1,7 +1,7 @@
import { createAlovaInstance } from './alova'
export const request = createAlovaInstance({
baseURL: __URL_MAP__.url.path,
baseURL: __PROXY_MAPPING__.url.path,
})
export const blankInstance = createAlovaInstance({

View File

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