Skip to content

Commit

Permalink
fix(vite-node): expose all envs from .env file, not just with a prefi…
Browse files Browse the repository at this point in the history
…x `VITE_` (#6017)
  • Loading branch information
sheremet-va committed Jul 2, 2024
1 parent cf9d84d commit d87bef9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/vite-node/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { resolve } from 'node:path'
import cac from 'cac'
import c from 'picocolors'
import { createServer } from 'vite'
import { createServer, loadEnv } from 'vite'
import { version } from '../package.json'
import { ViteNodeServer } from './server'
import { ViteNodeRunner } from './client'
Expand Down Expand Up @@ -95,6 +95,12 @@ async function run(files: string[], options: CliOptions = {}) {
})
await server.pluginContainer.buildStart({})

const env = loadEnv(server.config.mode, server.config.envDir, '')

for (const key in env) {
process.env[key] ??= env[key]
}

const node = new ViteNodeServer(server, serverOptions)

installSourcemapsSupport({
Expand Down
1 change: 1 addition & 0 deletions test/vite-node/.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MY_TEST_ENV=hello
2 changes: 2 additions & 0 deletions test/vite-node/src/cli-print-env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line no-console
console.log(JSON.stringify(process.env, null, 2))
6 changes: 6 additions & 0 deletions test/vite-node/test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ it('script args in -- after', async () => {
expect(parseResult(cli1.stdout)).include('--version').include('--help')
})

it('exposes .env variables', async () => {
const { stdout } = await runViteNodeCli(resolve(__dirname, '../src/cli-print-env.js'))
const env = JSON.parse(stdout)
expect(env.MY_TEST_ENV).toBe('hello')
})

it.each(['index.js', 'index.cjs', 'index.mjs'])('correctly runs --watch %s', async (file) => {
const entryPath = resolve(__dirname, '../src/watch', file)
const { viteNode } = await runViteNodeCli('--watch', entryPath)
Expand Down

0 comments on commit d87bef9

Please sign in to comment.