瀏覽代碼

fix: app config support `.env.local` (#5012)

Netfan 9 月之前
父節點
當前提交
935df713f3
共有 1 個文件被更改,包括 12 次插入9 次删除
  1. 12 9
      internal/vite-config/src/utils/env.ts

+ 12 - 9
internal/vite-config/src/utils/env.ts

@@ -1,5 +1,6 @@
 import type { ApplicationPluginOptions } from '../typing';
 import type { ApplicationPluginOptions } from '../typing';
 
 
+import { existsSync } from 'node:fs';
 import { join } from 'node:path';
 import { join } from 'node:path';
 
 
 import { fs } from '@vben/node-utils';
 import { fs } from '@vben/node-utils';
@@ -21,12 +22,11 @@ function getConfFiles() {
   const script = process.env.npm_lifecycle_script as string;
   const script = process.env.npm_lifecycle_script as string;
   const reg = /--mode ([\d_a-z]+)/;
   const reg = /--mode ([\d_a-z]+)/;
   const result = reg.exec(script);
   const result = reg.exec(script);
-
+  let mode = 'production';
   if (result) {
   if (result) {
-    const mode = result[1];
-    return ['.env', `.env.${mode}`];
+    mode = result[1] as string;
   }
   }
-  return ['.env', '.env.production'];
+  return ['.env', '.env.local', `.env.${mode}`, `.env.${mode}.local`];
 }
 }
 
 
 /**
 /**
@@ -42,11 +42,14 @@ async function loadEnv<T = Record<string, string>>(
 
 
   for (const confFile of confFiles) {
   for (const confFile of confFiles) {
     try {
     try {
-      const envPath = await fs.readFile(join(process.cwd(), confFile), {
-        encoding: 'utf8',
-      });
-      const env = dotenv.parse(envPath);
-      envConfig = { ...envConfig, ...env };
+      const confFilePath = join(process.cwd(), confFile);
+      if (existsSync(confFilePath)) {
+        const envPath = await fs.readFile(confFilePath, {
+          encoding: 'utf8',
+        });
+        const env = dotenv.parse(envPath);
+        envConfig = { ...envConfig, ...env };
+      }
     } catch (error) {
     } catch (error) {
       console.error(`Error while parsing ${confFile}`, error);
       console.error(`Error while parsing ${confFile}`, error);
     }
     }