Skip to content

Commit

Permalink
feat(env): also expose VITE_ variables from actual env
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 6, 2021
1 parent 9c97638 commit 956cd2c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
6 changes: 6 additions & 0 deletions packages/playground/env/__tests__/env.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ test('mode file override', async () => {
expect(await page.textContent('.mode-file')).toBe(`.env.${mode}`)
})

test('inline variables', async () => {
expect(await page.textContent('.inline')).toBe(
isBuild ? `inline-build` : `inline-serve`
)
})

test('NODE_ENV', async () => {
expect(await page.textContent('.node-env')).toBe(mode)
})
Expand Down
6 changes: 5 additions & 1 deletion packages/playground/env/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ <h1>Environment Variables</h1>
<p >
import.meta.<wbr/>env.VITE_EFFECTIVE_MODE_FILE_NAME: <code class="mode-file"></code>
</p>
<p >
import.meta.<wbr/>env.VITE_INLINE: <code class="inline"></code>
</p>
<p>
process.<wbr/>env.NODE_ENV: <code class="node-env"></code>
</p>
Expand All @@ -31,7 +34,8 @@ <h1>Environment Variables</h1>
text('.prod', import.meta.env.PROD)
text('.custom', import.meta.env.VITE_CUSTOM_ENV_VARIABLE)
text('.mode-file', import.meta.env.VITE_EFFECTIVE_MODE_FILE_NAME)
text('.node-env', process.env.NODE_ENV),
text('.inline', import.meta.env.VITE_INLINE)
text('.node-env', process.env.NODE_ENV)
text('.env-object', JSON.stringify(import.meta.env, null, 2))

function text(el, text) {
Expand Down
7 changes: 5 additions & 2 deletions packages/playground/env/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"dev": "cross-env VITE_INLINE=inline-serve vite",
"build": "cross-env VITE_INLINE=inline-build vite build",
"debug": "node --inspect-brk ../../vite/bin/vite"
},
"devDependencies": {
"cross-env": "^7.0.3"
}
}
8 changes: 8 additions & 0 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,14 @@ export function loadEnv(mode: string, root: string, prefix = 'VITE_') {
/** default file */ `.env`
]

// check if there are actual env variables starting with VITE_*
// these are typically provided inline and should be prioritized
for (const key in process.env) {
if (key.startsWith(prefix) && env[key] === undefined) {
env[key] = process.env[key] as string
}
}

for (const file of envFiles) {
const path = lookupFile(root, [file], true)
if (path) {
Expand Down
2 changes: 2 additions & 0 deletions scripts/jestPerTestSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ beforeAll(async () => {
}

if (!isBuildTest) {
process.env.VITE_INLINE = 'inline-serve'
server = await (await createServer(options)).listen()
// use resolved port from server
const url = ((global as any).viteTestUrl = `http://localhost:${server.config.server.port}`)
await page.goto(url)
} else {
process.env.VITE_INLINE = 'inline-build'
await build(options)
const url = ((global as any).viteTestUrl = await startStaticServer())
await page.goto(url)
Expand Down

0 comments on commit 956cd2c

Please sign in to comment.