index.ts 1.2 KB

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