npm i vite-plugin-version-env -S
"types": [ "vite-plugin-version-env/client",]
declare namespace VersionEnvSpace {
interface GlobalConfig {
DEV?: DevConfig;
[x: string]: any;
}
interface DevConfig {
port?: number;
proxy?: boolean;
proxy_list?: {
name: string;
include?: string;
exclude?: string;
target: string;
}[];
[x: string]: any;
}
interface EnvConfig {
GLOBAL_CONFIG: GlobalConfig;
DEV?: DevConfig;
}
}
declare const GLOBAL_CONFIG: VersionEnvSpace.GlobalConfig;
namespace VersionEnvSpace {
interface GlobalConfig {
cache_store: string[];
api_base: string;
api_idaas: string;
api_file: string;
api_security: string;
api_gis: string;
}
}
build/config
|- development.ts
|- production.ts
|- test.ts
export default {
//全局配置项,打包后会存入 app.config.js文件中。
GLOBAL_CONFIG: {
cache_store: ['oauth', 'global'],
api_base: 'http://fyeb.cnsaas.com/support-gateway/',
api_idaas: '/idaas/',
api_file: '/file-service/',
api_security: '/fy-security/',
api_gis: '/',
},
DEV: {
port: 8080,
proxy: true,
proxy_list: [
{
name: '/_api/',
include: '^/gisLayerGroup',
target: 'http://172.16.105.9:30006',
},
],
},
} as VersionEnvSpace.EnvConfig;
import { ServerPlugin, VersionPlugin } from 'vite-plugin-version-env';
export default defineConfig(async ({ mode, command }: ConfigEnv): Promise<UserConfig> => {
const CustomEnv = (await import(`./build/config/${mode}.ts`)).default as VersionEnvSpace.EnvConfig;
return {
plugins: [ VersionPlugin({ CustomEnv, command, '__GLOBAL_CONFIG__', 'app.config.js' }),],
server: {
cors: true,
open: true,
host: '0.0.0.0',
...ServerPlugin(CustomEnv)
}
}
({ CustomEnv, command, cleanDir, GLOBAL_CONFIG_FILE_NAME, GLOBAL_CONFIG_KEY, GLOBAL_CONFIG_NAME, }: {
command: 'build' | 'serve';
webTitle?: string;
CustomEnv?: VersionEnvSpace.EnvConfig;
cleanDir?: boolean;
GLOBAL_CONFIG_FILE_NAME?: string;
GLOBAL_CONFIG_KEY?: string;
GLOBAL_CONFIG_NAME?: string;
})
属性 | 类型 | 描述 | 默认值 |
---|---|---|---|
webTitle |
String | 网页标题 | - |
CustomEnv |
Object | 配置对象 | - |
command |
'build' |'serve' | 构建命令 | - |
cleanDir |
Boolean | 是否清空输出目录 | true |
GLOBAL_CONFIG_FILE_NAME |
String | 存放全局配置的文件名 | app.config.js |
GLOBAL_CONFIG_KEY |
String | 挂载到 window 对象上的属性名 |
\__GLOBAL_CONFIG__ |
GLOBAL_CONFIG_NAME |
String | 代码可用的全局对象名 | GLOBAL_CONFIG |
import SetDevProxy from 'vite-plugin-version-env/SetDevProxy';
http.interceptors.request.use(async (config) => {
SetDevProxy(config,GLOBAL_CONFIG.DEV);
return config;
}
- 开发环境,依据代理配置,设置axios的baseURLGlobalConfig.api_base
- ProxyConfig为undefined时则不起作用
(AxiosConfig: InternalAxiosRequestConfig<any>, ProxyConfig?: VersionEnvSpace.DevConfig) => void
global.d.ts
declare const CUSTON_GLOBAL_CONFIG: VersionEnvSpace.GlobalConfig;
- GLOBAL_VERSION_CODE 版本号
- GLOBAL_CONFIG 全局配置