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_PORT: 8848,
VITE_PUBLIC_PATH: "", VITE_PUBLIC_PATH: "",
VITE_ROUTER_HISTORY: "", VITE_ROUTER_HISTORY: "",
@ -10,22 +10,27 @@ const warpperEnv = (envConf: Recordable): ViteEnv => {
VITE_COMPRESSION: "none" VITE_COMPRESSION: "none"
}; };
for (const envName of Object.keys(envConf)) { for (const configName of Object.keys(envConfigs)) {
let realName = envConf[envName].replace(/\\n/g, "\n"); let realConfigValue = envConfigs[configName].replace(/\\n/g, "\n");
realName = realConfigValue =
realName === "true" ? true : realName === "false" ? false : realName; realConfigValue === "true"
? true
: realConfigValue === "false"
? false
: realConfigValue;
if (envName === "VITE_PORT") { if (configName === "VITE_PORT") {
realName = Number(realName); realConfigValue = Number(realConfigValue);
} }
ret[envName] = realName;
if (typeof realName === "string") { defaultEnvConfigs[configName] = realConfigValue;
process.env[envName] = realName; if (typeof realConfigValue === "string") {
} else if (typeof realName === "object") { process.env[configName] = realConfigValue;
process.env[envName] = JSON.stringify(realName); } 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) => { await formEl.validate((valid, fields) => {
if (valid) { if (valid) {
useUserStoreHook() useUserStoreHook()
.loginByUsername({ username: ruleForm.username, password: "admin123" }) .loginByUsername({
username: ruleForm.username,
password: ruleForm.password
})
.then(res => { .then(res => {
if (res.success) { if (res.success) {
// //

View File

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