1
0

env.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import type { GlobEnvConfig } from '/#/config';
  2. import { warn } from '/@/utils/log';
  3. import pkg from '../../package.json';
  4. import { getConfigFileName } from '../../build/getConfigFileName';
  5. export function getCommonStoragePrefix() {
  6. const { VITE_GLOB_APP_SHORT_NAME } = getAppEnvConfig();
  7. return `${VITE_GLOB_APP_SHORT_NAME}__${getEnv()}`.toUpperCase();
  8. }
  9. // Generate cache key according to version
  10. export function getStorageShortName() {
  11. return `${getCommonStoragePrefix()}${`__${pkg.version}`}__`.toUpperCase();
  12. }
  13. export function getAppEnvConfig() {
  14. const ENV_NAME = getConfigFileName(import.meta.env);
  15. const ENV = (import.meta.env.DEV
  16. ? // Get the global configuration (the configuration will be extracted independently when packaging)
  17. (import.meta.env as unknown as GlobEnvConfig)
  18. : window[ENV_NAME as any]) as unknown as GlobEnvConfig;
  19. const {
  20. VITE_GLOB_APP_TITLE,
  21. VITE_GLOB_API_URL,
  22. VITE_USE_MOCK,
  23. VITE_GLOB_APP_SHORT_NAME,
  24. VITE_GLOB_API_URL_PREFIX,
  25. VITE_GLOB_UPLOAD_URL,
  26. } = ENV;
  27. if (!/^[a-zA-Z\_]*$/.test(VITE_GLOB_APP_SHORT_NAME)) {
  28. warn(
  29. `VITE_GLOB_APP_SHORT_NAME Variables can only be characters/underscores, please modify in the environment variables and re-running.`
  30. );
  31. }
  32. return {
  33. VITE_GLOB_APP_TITLE,
  34. VITE_GLOB_API_URL,
  35. VITE_USE_MOCK,
  36. VITE_GLOB_APP_SHORT_NAME,
  37. VITE_GLOB_API_URL_PREFIX,
  38. VITE_GLOB_UPLOAD_URL,
  39. };
  40. }
  41. /**
  42. * @description: Development mode
  43. */
  44. export const devMode = 'development';
  45. /**
  46. * @description: Production mode
  47. */
  48. export const prodMode = 'production';
  49. /**
  50. * @description: Get environment variables
  51. * @returns:
  52. * @example:
  53. */
  54. export function getEnv(): string {
  55. return import.meta.env.MODE;
  56. }
  57. /**
  58. * @description: Is it a development mode
  59. * @returns:
  60. * @example:
  61. */
  62. export function isDevMode(): boolean {
  63. return import.meta.env.DEV;
  64. }
  65. /**
  66. * @description: Is it a production mode
  67. * @returns:
  68. * @example:
  69. */
  70. export function isProdMode(): boolean {
  71. return import.meta.env.PROD;
  72. }