Commit 95c16a5d26f9fd9a1d11894afe1146ca495eee93
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,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 | * Get the environment variables starting with the specified prefix | 57 | * Get the environment variables starting with the specified prefix |
44 | * @param match prefix | 58 | * @param match prefix |
45 | * @param confFiles ext | 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 | let envConfig = {}; | 62 | let envConfig = {}; |
49 | confFiles.forEach((item) => { | 63 | confFiles.forEach((item) => { |
50 | try { | 64 | try { |
51 | const env = dotenv.parse(fs.readFileSync(path.resolve(process.cwd(), item))); | 65 | const env = dotenv.parse(fs.readFileSync(path.resolve(process.cwd(), item))); |
52 | envConfig = { ...envConfig, ...env }; | 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 | Object.keys(envConfig).forEach((key) => { | 72 | Object.keys(envConfig).forEach((key) => { |
57 | - const reg = new RegExp(`^(${match})`); | ||
58 | if (!reg.test(key)) { | 73 | if (!reg.test(key)) { |
59 | Reflect.deleteProperty(envConfig, key); | 74 | Reflect.deleteProperty(envConfig, key); |
60 | } | 75 | } |