env.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_GLOB_APP_SHORT_NAME,
  23. VITE_GLOB_API_URL_PREFIX,
  24. VITE_GLOB_UPLOAD_URL,
  25. } = ENV;
  26. if (!/^[a-zA-Z\_]*$/.test(VITE_GLOB_APP_SHORT_NAME)) {
  27. warn(
  28. `VITE_GLOB_APP_SHORT_NAME Variables can only be characters/underscores, please modify in the environment variables and re-running.`
  29. );
  30. }
  31. return {
  32. VITE_GLOB_APP_TITLE,
  33. VITE_GLOB_API_URL,
  34. VITE_GLOB_APP_SHORT_NAME,
  35. VITE_GLOB_API_URL_PREFIX,
  36. VITE_GLOB_UPLOAD_URL,
  37. };
  38. }
  39. /**
  40. * @description: Development model
  41. */
  42. export const devMode = 'development';
  43. /**
  44. * @description: Production mode
  45. */
  46. export const prodMode = 'production';
  47. /**
  48. * @description: Get environment variables
  49. * @returns:
  50. * @example:
  51. */
  52. export function getEnv(): string {
  53. return import.meta.env.MODE;
  54. }
  55. /**
  56. * @description: Is it a development mode
  57. * @returns:
  58. * @example:
  59. */
  60. export function isDevMode(): boolean {
  61. return import.meta.env.DEV;
  62. }
  63. /**
  64. * @description: Is it a production mode
  65. * @returns:
  66. * @example:
  67. */
  68. export function isProdMode(): boolean {
  69. return import.meta.env.PROD;
  70. }