Skip to content

Commit d87bef9

Browse files
authored
fix(vite-node): expose all envs from .env file, not just with a prefix VITE_ (#6017)
1 parent cf9d84d commit d87bef9

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

Diff for: packages/vite-node/src/cli.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { resolve } from 'node:path'
22
import cac from 'cac'
33
import c from 'picocolors'
4-
import { createServer } from 'vite'
4+
import { createServer, loadEnv } from 'vite'
55
import { version } from '../package.json'
66
import { ViteNodeServer } from './server'
77
import { ViteNodeRunner } from './client'
@@ -95,6 +95,12 @@ async function run(files: string[], options: CliOptions = {}) {
9595
})
9696
await server.pluginContainer.buildStart({})
9797

98+
const env = loadEnv(server.config.mode, server.config.envDir, '')
99+
100+
for (const key in env) {
101+
process.env[key] ??= env[key]
102+
}
103+
98104
const node = new ViteNodeServer(server, serverOptions)
99105

100106
installSourcemapsSupport({

Diff for: test/vite-node/.env.local

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MY_TEST_ENV=hello

Diff for: test/vite-node/src/cli-print-env.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// eslint-disable-next-line no-console
2+
console.log(JSON.stringify(process.env, null, 2))

Diff for: test/vite-node/test/cli.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ it('script args in -- after', async () => {
3939
expect(parseResult(cli1.stdout)).include('--version').include('--help')
4040
})
4141

42+
it('exposes .env variables', async () => {
43+
const { stdout } = await runViteNodeCli(resolve(__dirname, '../src/cli-print-env.js'))
44+
const env = JSON.parse(stdout)
45+
expect(env.MY_TEST_ENV).toBe('hello')
46+
})
47+
4248
it.each(['index.js', 'index.cjs', 'index.mjs'])('correctly runs --watch %s', async (file) => {
4349
const entryPath = resolve(__dirname, '../src/watch', file)
4450
const { viteNode } = await runViteNodeCli('--watch', entryPath)

0 commit comments

Comments
 (0)