env.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import type { GlobEnvConfig } from '@jeesite/types/config';
  2. import { warn, env } from '@jeesite/core/utils/log';
  3. import { version } from '../package.json';
  4. import { getEnvConfigName } from '@jeesite/vite/config/getEnvConfigName';
  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().replace(/\s/g, '_')}${`__${version}`}__`.toUpperCase();
  12. }
  13. export function getAppEnvConfig() {
  14. const ENV_CONFIG_NAME = getEnvConfigName(env);
  15. const ENV = (env.DEV
  16. ? (env as unknown as GlobEnvConfig)
  17. : window[ENV_CONFIG_NAME as any]) as unknown as GlobEnvConfig;
  18. if (!/^[a-zA-Z_]*$/.test(ENV.VITE_GLOB_APP_SHORT_NAME)) {
  19. warn(
  20. `VITE_GLOB_APP_SHORT_NAME Variables can only be characters/underscores, please modify in the environment variables and re-running.`,
  21. );
  22. }
  23. return ENV;
  24. }
  25. /**
  26. * @description: Development mode
  27. */
  28. export const devMode = 'development';
  29. /**
  30. * @description: Production mode
  31. */
  32. export const prodMode = 'production';
  33. /**
  34. * @description: Get environment mode
  35. */
  36. export function getEnv(): string {
  37. return env.MODE;
  38. }
  39. /**
  40. * @description: Is it a development mode
  41. */
  42. export function isDevMode(): boolean {
  43. return env.DEV;
  44. }
  45. /**
  46. * @description: Is it a production mode
  47. */
  48. export function isProdMode(): boolean {
  49. return env.PROD;
  50. }
  51. /**
  52. * @description: VITE_PUBLIC_PATH
  53. */
  54. function getPublicPath(): string {
  55. const publicPath = env.VITE_PUBLIC_PATH || '';
  56. return publicPath == '/' ? '' : publicPath;
  57. }
  58. export const publicPath = getPublicPath();