index.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // 处理环境变量
  2. const warpperEnv = (envConf: Recordable): ViteEnv => {
  3. // 此处为默认值,无需修改
  4. const ret: ViteEnv = {
  5. VITE_PORT: 8848,
  6. VITE_PUBLIC_PATH: "",
  7. VITE_PROXY_DOMAIN: "",
  8. VITE_PROXY_DOMAIN_REAL: "",
  9. VITE_PROXY_USER_REAL: "",
  10. VITE_ROUTER_HISTORY: "",
  11. VITE_CORP_ID: "",
  12. VITE_APP_ID: "",
  13. REDIRECT_BASE_URL: "",
  14. VITE_LEGACY: false
  15. };
  16. for (const envName of Object.keys(envConf)) {
  17. let realName = envConf[envName].replace(/\\n/g, "\n");
  18. realName =
  19. realName === "true" ? true : realName === "false" ? false : realName;
  20. if (envName === "VITE_PORT") {
  21. realName = Number(realName);
  22. }
  23. ret[envName] = realName;
  24. if (typeof realName === "string") {
  25. process.env[envName] = realName;
  26. } else if (typeof realName === "object") {
  27. process.env[envName] = JSON.stringify(realName);
  28. }
  29. }
  30. return ret;
  31. };
  32. // 跨域代理重写
  33. const regExps = (value: string, reg: string): string => {
  34. return value.replace(new RegExp(`^${reg}`, "g"), "");
  35. };
  36. // 环境变量
  37. const loadEnv = (): ViteEnv => {
  38. return import.meta.env;
  39. };
  40. export { warpperEnv, regExps, loadEnv };