index.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. VITE_PURCHASE_URL: "",
  16. VITE_SECRET_KEY: "",
  17. WECHAT_URL: "",
  18. BACK_URL: ""
  19. };
  20. for (const envName of Object.keys(envConf)) {
  21. let realName = envConf[envName].replace(/\\n/g, "\n");
  22. realName =
  23. realName === "true" ? true : realName === "false" ? false : realName;
  24. if (envName === "VITE_PORT") {
  25. realName = Number(realName);
  26. }
  27. ret[envName] = realName;
  28. if (typeof realName === "string") {
  29. process.env[envName] = realName;
  30. } else if (typeof realName === "object") {
  31. process.env[envName] = JSON.stringify(realName);
  32. }
  33. }
  34. return ret;
  35. };
  36. // 跨域代理重写
  37. const regExps = (value: string, reg: string): string => {
  38. return value.replace(new RegExp(`^${reg}`, "g"), "");
  39. };
  40. // 环境变量
  41. const loadEnv = (): ViteEnv => {
  42. return import.meta.env;
  43. };
  44. export { warpperEnv, regExps, loadEnv };