Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow changing import.meta.env (#714)
- Loading branch information
1 parent
d02db1b
commit 5f00788
Showing
6 changed files
with
86 additions
and
0 deletions.
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