New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: allow changing import.meta.env #714
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f6eeefa
feat: allow change import.meta.env
sheremet-va 19b3ed2
chore: more tests for "define" import.meta.env
sheremet-va 2591ba5
chore: expose BASE_URL, MODE, PROD, DEV on env
sheremet-va f30c3ca
chore: fix typings
sheremet-va 38a8deb
fix: respect user env if provided
sheremet-va 859e4cd
Merge branch 'main' of https://github.com/antfu-sponsors/vitest into …
sheremet-va File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import MagicString from 'magic-string' | ||
import type { Plugin } from 'vite' | ||
|
||
export const EnvReplacerPlugin = (): Plugin => { | ||
return { | ||
name: 'vitest:env-replacer', | ||
enforce: 'pre', | ||
transform(code) { | ||
let s: MagicString | null = null | ||
|
||
const envs = code.matchAll(/\bimport\.meta\.env\b/g) | ||
|
||
for (const env of envs) { | ||
s ||= new MagicString(code) | ||
|
||
const startIndex = env.index! | ||
const endIndex = startIndex + env[0].length | ||
|
||
s.overwrite(startIndex, endIndex, 'process.env') | ||
} | ||
|
||
if (s) { | ||
return { | ||
code: s.toString(), | ||
map: s.generateMap({ hires: true }), | ||
} | ||
} | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function getAuthToken() { | ||
return import.meta.env.AUTH_TOKEN | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { expect, test } from 'vitest' | ||
import { getAuthToken } from '../src/env' | ||
|
||
test('can reassign env locally', () => { | ||
import.meta.env.VITEST_ENV = 'TEST' | ||
expect(import.meta.env.VITEST_ENV).toBe('TEST') | ||
}) | ||
|
||
test('can reassign env everywhere', () => { | ||
import.meta.env.AUTH_TOKEN = '123' | ||
expect(getAuthToken()).toBe('123') | ||
process.env.AUTH_TOKEN = '321' | ||
expect(getAuthToken()).toBe('321') | ||
}) | ||
|
||
test('can see env in "define"', () => { | ||
expect(import.meta.env.TEST_NAME).toBe('hello world') | ||
expect(process.env.TEST_NAME).toBe('hello world') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
interface ImportMeta { | ||
readonly env: ImportMetaEnv | ||
} | ||
|
||
interface ImportMetaEnv { | ||
[key: string]: string | boolean | undefined | ||
BASE_URL: string | ||
MODE: string | ||
DEV: boolean | ||
PROD: boolean | ||
SSR: boolean | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not so sure about the
MODE
convention here, it feels too general to me. Do we have any references for other tools using it? or we better useVITEST_MODE
orVITE_MODE
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what Vite uses on
import.meta.env
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All these variables are default values on
import.meta.env
that always existThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And we are allowing people to reassign them by making
import.meta.env
=process.env
we can assign them to
process.metaEnv
if you don't want to collaps with other tools, but I know this approach is wildly used now to support testing Vite in jest (usingprocess.env
that is)process.metaEnv
can even allow booleanswe can hide implementation on some helper like
vi.mockEnv(env, value)
, but people may get confused whenprocess.env
will stay the same