Skip to content

Commit

Permalink
fix: env variables override (#10113)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing authored Oct 5, 2022
1 parent 7c4accb commit d619460
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 29 deletions.
53 changes: 53 additions & 0 deletions packages/vite/src/node/__tests__/env.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, expect, test } from 'vitest'
import { loadEnv } from '../env'

const __dirname = fileURLToPath(new URL('.', import.meta.url))

describe('loadEnv', () => {
test('basic', () => {
expect(loadEnv('development', join(__dirname, './env')))
.toMatchInlineSnapshot(`
{
"VITE_APP_BASE_ROUTE": "/",
"VITE_APP_BASE_URL": "/",
"VITE_ENV1": "ENV1",
"VITE_ENV2": "ENV2",
"VITE_ENV3": "ENV3",
}
`)
})

test('specific prefix', () => {
expect(loadEnv('development', join(__dirname, './env'), 'VVITE'))
.toMatchInlineSnapshot(`
{
"VVITE_A": "A",
"VVITE_B": "B",
}
`)
})

test('override', () => {
expect(loadEnv('production', join(__dirname, './env')))
.toMatchInlineSnapshot(`
{
"VITE_APP_BASE_ROUTE": "/app/",
"VITE_APP_BASE_URL": "/app/",
"VITE_USER_NODE_ENV": "production",
}
`)
})

test('VITE_USER_NODE_ENV', () => {
loadEnv('development', join(__dirname, './env'))
expect(process.env.VITE_USER_NODE_ENV).toEqual('production')
})

test('Already exists VITE_USER_NODE_ENV', () => {
process.env.VITE_USER_NODE_ENV = 'test'
loadEnv('development', join(__dirname, './env'))
expect(process.env.VITE_USER_NODE_ENV).toEqual('test')
})
})
5 changes: 5 additions & 0 deletions packages/vite/src/node/__tests__/env/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
VITE_APP_BASE_ROUTE=/
VITE_APP_BASE_URL=$VITE_APP_BASE_ROUTE

VVITE_A=A
VVITE_B=B
4 changes: 4 additions & 0 deletions packages/vite/src/node/__tests__/env/.env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
NODE_ENV=production
VITE_ENV1=ENV1
VITE_ENV2=ENV2
VITE_ENV3=ENV3
1 change: 1 addition & 0 deletions packages/vite/src/node/__tests__/env/.env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_APP_BASE_ROUTE=/app/
61 changes: 32 additions & 29 deletions packages/vite/src/node/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ export function loadEnv(
prefixes = arraify(prefixes)
const env: Record<string, string> = {}
const envFiles = [
/** mode local file */ `.env.${mode}.local`,
/** mode file */ `.env.${mode}`,
/** default file */ `.env`,
/** local file */ `.env.local`,
/** default file */ `.env`
/** mode file */ `.env.${mode}`,
/** mode local file */ `.env.${mode}.local`
]

// check if there are actual env variables starting with VITE_*
Expand All @@ -35,35 +35,38 @@ export function loadEnv(
}
}

for (const file of envFiles) {
const path = lookupFile(envDir, [file], { pathOnly: true, rootDir: envDir })
if (path) {
const parsed = dotenv.parse(fs.readFileSync(path), {
debug: process.env.DEBUG?.includes('vite:dotenv') || undefined
const parsed = Object.fromEntries(
envFiles.flatMap((file) => {
const path = lookupFile(envDir, [file], {
pathOnly: true,
rootDir: envDir
})
if (!path) return []
return Object.entries(
dotenv.parse(fs.readFileSync(path), {
debug: process.env.DEBUG?.includes('vite:dotenv')
})
)
})
)

// let environment variables use each other
dotenvExpand({
parsed,
// prevent process.env mutation
ignoreProcessEnv: true
} as any)
// let environment variables use each other
dotenvExpand({
parsed,
// prevent process.env mutation
ignoreProcessEnv: true
} as any)

// only keys that start with prefix are exposed to client
for (const [key, value] of Object.entries(parsed)) {
if (
prefixes.some((prefix) => key.startsWith(prefix)) &&
env[key] === undefined
) {
env[key] = value
} else if (
key === 'NODE_ENV' &&
process.env.VITE_USER_NODE_ENV === undefined
) {
// NODE_ENV override in .env file
process.env.VITE_USER_NODE_ENV = value
}
}
// only keys that start with prefix are exposed to client
for (const [key, value] of Object.entries(parsed)) {
if (prefixes.some((prefix) => key.startsWith(prefix))) {
env[key] = value
} else if (
key === 'NODE_ENV' &&
process.env.VITE_USER_NODE_ENV === undefined
) {
// NODE_ENV override in .env file
process.env.VITE_USER_NODE_ENV = value
}
}
return env
Expand Down

0 comments on commit d619460

Please sign in to comment.