在src/utils/env.ts文件中,我们可以尝试一种更健壮的获取环境变量的方法。具体地,我们可以先检查window对象中是否存在ENV属性,如果不存在,则回退到使用import.meta.env来获取环境变量。import.meta.env是Vite等现代构建工具提供的一种在模块内部访问环境变量的方式。
// 修改前代码
const ENV = (import.meta.env.DEV
? // Get the global configuration (the configuration will be extracted independently when packaging)
(import.meta.env as unknown as GlobEnvConfig)
: window[ENV_NAME as any]) as unknown as GlobEnvConfig;
// 修改后代码
const ENV = (import.meta.env.DEV
? // Get the global configuration (the configuration will be extracted independently when packaging)
(import.meta.env as unknown as GlobEnvConfig)
:(window[ENV_NAME as any] ? window[ENV_NAME as any] : import.meta.env as unknown as GlobEnvConfig)) as unknown as GlobEnvConfig;