You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.4 KiB
JavaScript

/**
* 将环境变量去掉VITE_APP_后,转换成小驼峰命名规则的属性的对象
* export: env,useEnv
* env: 转换后的环境变量
* useEnv: vue插件,本质将转换后环境变量绑定:app.config.globalProperties.$env
* @Author : J.L.Zhou
* @EMail : 12020042@qq.com
* @Tel : 151 1104 7708
* @CreateTime : 2023-05-16 10:44:48
* @LastEditos : J.L.Zhou
* @LastEditTime : 2023-05-16 10:44:48
* @Version : 1.0
* Copyright 2023 jlzhou.top Inc. All rights reserved.
* Warning: this content is only for internal circulation of the company.
* It is forbidden to divulge it or use it for other commercial purposes.
*/
console.debug("原始的环境变量",import.meta.env);
console.debug("应用运行的模式",import.meta.env.MODE);
console.debug("生产环境",import.meta.env.PROD);
console.debug("开发环境",import.meta.env.DEV);
const env = {};
let name,value;
for(name in import.meta.env){
if(!name.startsWith("VITE_APP_")){
continue;
}
value = import.meta.env[name];
name = name.substr(9).toLowerCase().split(/_+/);
for (var i = 1; i < name.length; i++) {
name[i]=name[i].substr(0,1).toUpperCase()+name[i].substr(1);
}
name = name.join("");
env[name] = value;
}
console.debug('转换后的环境变量',env);
env.dev = import.meta.env.DEV;
const useEnv = {
install(app){
app.config.globalProperties.$env=env;
}
}
export {env,useEnv};