refactor(./build/index.ts): 调整加载env的代码

This commit is contained in:
valarchie 2023-05-20 16:50:46 +08:00
parent 0117e581d9
commit aebb7fa802
3 changed files with 26 additions and 18 deletions

View File

@ -1,7 +1,7 @@
/** 处理环境变量 */
const warpperEnv = (envConf: Recordable): ViteEnv => {
const wrapperEnv = (envConfigs: Recordable): ViteEnv => {
/** 此处为默认值 */
const ret: ViteEnv = {
const defaultEnvConfigs: ViteEnv = {
VITE_PORT: 8848,
VITE_PUBLIC_PATH: "",
VITE_ROUTER_HISTORY: "",
@ -10,22 +10,27 @@ const warpperEnv = (envConf: Recordable): ViteEnv => {
VITE_COMPRESSION: "none"
};
for (const envName of Object.keys(envConf)) {
let realName = envConf[envName].replace(/\\n/g, "\n");
realName =
realName === "true" ? true : realName === "false" ? false : realName;
for (const configName of Object.keys(envConfigs)) {
let realConfigValue = envConfigs[configName].replace(/\\n/g, "\n");
realConfigValue =
realConfigValue === "true"
? true
: realConfigValue === "false"
? false
: realConfigValue;
if (envName === "VITE_PORT") {
realName = Number(realName);
if (configName === "VITE_PORT") {
realConfigValue = Number(realConfigValue);
}
ret[envName] = realName;
if (typeof realName === "string") {
process.env[envName] = realName;
} else if (typeof realName === "object") {
process.env[envName] = JSON.stringify(realName);
defaultEnvConfigs[configName] = realConfigValue;
if (typeof realConfigValue === "string") {
process.env[configName] = realConfigValue;
} else if (typeof realConfigValue === "object") {
process.env[configName] = JSON.stringify(realConfigValue);
}
}
return ret;
return defaultEnvConfigs;
};
export { warpperEnv };
export { wrapperEnv };

View File

@ -43,7 +43,10 @@ const onLogin = async (formEl: FormInstance | undefined) => {
await formEl.validate((valid, fields) => {
if (valid) {
useUserStoreHook()
.loginByUsername({ username: ruleForm.username, password: "admin123" })
.loginByUsername({
username: ruleForm.username,
password: ruleForm.password
})
.then(res => {
if (res.success) {
//

View File

@ -1,7 +1,7 @@
import dayjs from "dayjs";
import { resolve } from "path";
import pkg from "./package.json";
import { warpperEnv } from "./build";
import { wrapperEnv } from "./build";
import { getPluginsList } from "./build/plugins";
import { include, exclude } from "./build/optimize";
import { UserConfigExport, ConfigEnv, loadEnv } from "vite";
@ -28,7 +28,7 @@ const __APP_INFO__ = {
export default ({ command, mode }: ConfigEnv): UserConfigExport => {
const { VITE_CDN, VITE_PORT, VITE_COMPRESSION, VITE_PUBLIC_PATH } =
warpperEnv(loadEnv(mode, root));
wrapperEnv(loadEnv(mode, root));
return {
base: VITE_PUBLIC_PATH,
root,