You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.1 KiB

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