Skip to content

Commit d619460

Browse files
authoredOct 5, 2022
fix: env variables override (#10113)
1 parent 7c4accb commit d619460

File tree

5 files changed

+95
-29
lines changed

5 files changed

+95
-29
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { join } from 'node:path'
2+
import { fileURLToPath } from 'node:url'
3+
import { describe, expect, test } from 'vitest'
4+
import { loadEnv } from '../env'
5+
6+
const __dirname = fileURLToPath(new URL('.', import.meta.url))
7+
8+
describe('loadEnv', () => {
9+
test('basic', () => {
10+
expect(loadEnv('development', join(__dirname, './env')))
11+
.toMatchInlineSnapshot(`
12+
{
13+
"VITE_APP_BASE_ROUTE": "/",
14+
"VITE_APP_BASE_URL": "/",
15+
"VITE_ENV1": "ENV1",
16+
"VITE_ENV2": "ENV2",
17+
"VITE_ENV3": "ENV3",
18+
}
19+
`)
20+
})
21+
22+
test('specific prefix', () => {
23+
expect(loadEnv('development', join(__dirname, './env'), 'VVITE'))
24+
.toMatchInlineSnapshot(`
25+
{
26+
"VVITE_A": "A",
27+
"VVITE_B": "B",
28+
}
29+
`)
30+
})
31+
32+
test('override', () => {
33+
expect(loadEnv('production', join(__dirname, './env')))
34+
.toMatchInlineSnapshot(`
35+
{
36+
"VITE_APP_BASE_ROUTE": "/app/",
37+
"VITE_APP_BASE_URL": "/app/",
38+
"VITE_USER_NODE_ENV": "production",
39+
}
40+
`)
41+
})
42+
43+
test('VITE_USER_NODE_ENV', () => {
44+
loadEnv('development', join(__dirname, './env'))
45+
expect(process.env.VITE_USER_NODE_ENV).toEqual('production')
46+
})
47+
48+
test('Already exists VITE_USER_NODE_ENV', () => {
49+
process.env.VITE_USER_NODE_ENV = 'test'
50+
loadEnv('development', join(__dirname, './env'))
51+
expect(process.env.VITE_USER_NODE_ENV).toEqual('test')
52+
})
53+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
VITE_APP_BASE_ROUTE=/
2+
VITE_APP_BASE_URL=$VITE_APP_BASE_ROUTE
3+
4+
VVITE_A=A
5+
VVITE_B=B
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
NODE_ENV=production
2+
VITE_ENV1=ENV1
3+
VITE_ENV2=ENV2
4+
VITE_ENV3=ENV3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
VITE_APP_BASE_ROUTE=/app/

‎packages/vite/src/node/env.ts

+32-29
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ export function loadEnv(
1818
prefixes = arraify(prefixes)
1919
const env: Record<string, string> = {}
2020
const envFiles = [
21-
/** mode local file */ `.env.${mode}.local`,
22-
/** mode file */ `.env.${mode}`,
21+
/** default file */ `.env`,
2322
/** local file */ `.env.local`,
24-
/** default file */ `.env`
23+
/** mode file */ `.env.${mode}`,
24+
/** mode local file */ `.env.${mode}.local`
2525
]
2626

2727
// check if there are actual env variables starting with VITE_*
@@ -35,35 +35,38 @@ export function loadEnv(
3535
}
3636
}
3737

38-
for (const file of envFiles) {
39-
const path = lookupFile(envDir, [file], { pathOnly: true, rootDir: envDir })
40-
if (path) {
41-
const parsed = dotenv.parse(fs.readFileSync(path), {
42-
debug: process.env.DEBUG?.includes('vite:dotenv') || undefined
38+
const parsed = Object.fromEntries(
39+
envFiles.flatMap((file) => {
40+
const path = lookupFile(envDir, [file], {
41+
pathOnly: true,
42+
rootDir: envDir
4343
})
44+
if (!path) return []
45+
return Object.entries(
46+
dotenv.parse(fs.readFileSync(path), {
47+
debug: process.env.DEBUG?.includes('vite:dotenv')
48+
})
49+
)
50+
})
51+
)
4452

45-
// let environment variables use each other
46-
dotenvExpand({
47-
parsed,
48-
// prevent process.env mutation
49-
ignoreProcessEnv: true
50-
} as any)
53+
// let environment variables use each other
54+
dotenvExpand({
55+
parsed,
56+
// prevent process.env mutation
57+
ignoreProcessEnv: true
58+
} as any)
5159

52-
// only keys that start with prefix are exposed to client
53-
for (const [key, value] of Object.entries(parsed)) {
54-
if (
55-
prefixes.some((prefix) => key.startsWith(prefix)) &&
56-
env[key] === undefined
57-
) {
58-
env[key] = value
59-
} else if (
60-
key === 'NODE_ENV' &&
61-
process.env.VITE_USER_NODE_ENV === undefined
62-
) {
63-
// NODE_ENV override in .env file
64-
process.env.VITE_USER_NODE_ENV = value
65-
}
66-
}
60+
// only keys that start with prefix are exposed to client
61+
for (const [key, value] of Object.entries(parsed)) {
62+
if (prefixes.some((prefix) => key.startsWith(prefix))) {
63+
env[key] = value
64+
} else if (
65+
key === 'NODE_ENV' &&
66+
process.env.VITE_USER_NODE_ENV === undefined
67+
) {
68+
// NODE_ENV override in .env file
69+
process.env.VITE_USER_NODE_ENV = value
6770
}
6871
}
6972
return env

0 commit comments

Comments
 (0)
Please sign in to comment.