Commit 95c16a5d26f9fd9a1d11894afe1146ca495eee93

Authored by 周旭
Committed by GitHub
1 parent cdb10cc4

fix: support various vite modes of build, not just production (#832)

(cherry picked from commit bcc4773fbfc3dd1b7a24655bc731be830ad6d72d)
Showing 1 changed file with 19 additions and 4 deletions
build/utils.ts
... ... @@ -40,21 +40,36 @@ export function wrapperEnv(envConf: Recordable): ViteEnv {
40 40 }
41 41  
42 42 /**
  43 + * 获取当前环境下生效的配置文件名
  44 + */
  45 +function getConfFiles() {
  46 + const script = process.env.npm_lifecycle_script;
  47 + const reg = new RegExp('--mode ([a-z]+) ');
  48 + const result = reg.exec(script as string) as any;
  49 + if (result) {
  50 + const mode = result[1] as string;
  51 + return ['.env', `.env.${mode}`];
  52 + }
  53 + return ['.env', '.env.production'];
  54 +}
  55 +
  56 +/**
43 57 * Get the environment variables starting with the specified prefix
44 58 * @param match prefix
45 59 * @param confFiles ext
46 60 */
47   -export function getEnvConfig(match = 'VITE_GLOB_', confFiles = ['.env', '.env.production']) {
  61 +export function getEnvConfig(match = 'VITE_GLOB_', confFiles = getConfFiles()) {
48 62 let envConfig = {};
49 63 confFiles.forEach((item) => {
50 64 try {
51 65 const env = dotenv.parse(fs.readFileSync(path.resolve(process.cwd(), item)));
52 66 envConfig = { ...envConfig, ...env };
53   - } catch (error) {}
  67 + } catch (e) {
  68 + console.error(`Error in parsing ${item}`, e);
  69 + }
54 70 });
55   -
  71 + const reg = new RegExp(`^(${match})`);
56 72 Object.keys(envConfig).forEach((key) => {
57   - const reg = new RegExp(`^(${match})`);
58 73 if (!reg.test(key)) {
59 74 Reflect.deleteProperty(envConfig, key);
60 75 }
... ...