Skip to content
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

fix: ensure define overrides import.meta in build #8892

Merged
merged 2 commits into from Jul 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/vite/src/node/plugins/define.ts
Expand Up @@ -39,6 +39,7 @@ export function definePlugin(config: ResolvedConfig): Plugin {
// during dev, import.meta properties are handled by importAnalysis plugin.
// ignore replace import.meta.env in lib build
const importMetaKeys: Record<string, string> = {}
const importMetaFallbackKeys: Record<string, string> = {}
if (isBuild) {
const env: Record<string, any> = {
...config.env,
Expand All @@ -47,7 +48,7 @@ export function definePlugin(config: ResolvedConfig): Plugin {
for (const key in env) {
importMetaKeys[`import.meta.env.${key}`] = JSON.stringify(env[key])
}
Object.assign(importMetaKeys, {
Object.assign(importMetaFallbackKeys, {
'import.meta.env.': `({}).`,
'import.meta.env': JSON.stringify(config.env),
'import.meta.hot': `false`
Expand All @@ -61,8 +62,9 @@ export function definePlugin(config: ResolvedConfig): Plugin {

const replacements: Record<string, string> = {
...(replaceProcessEnv ? processNodeEnv : {}),
...userDefine,
...importMetaKeys,
...userDefine,
...importMetaFallbackKeys,
...(replaceProcessEnv ? processEnv : {})
}

Expand Down
3 changes: 2 additions & 1 deletion playground/env/.env
@@ -1,3 +1,4 @@
VITE_CUSTOM_ENV_VARIABLE=1
CUSTOM_PREFIX_ENV_VARIABLE=1
VITE_EFFECTIVE_MODE_FILE_NAME=.env
VITE_EFFECTIVE_MODE_FILE_NAME=.env
VITE_BOOL=true
4 changes: 4 additions & 0 deletions playground/env/__tests__/env.spec.ts
Expand Up @@ -36,6 +36,10 @@ test('inline variables', async () => {
)
})

test('bool', async () => {
expect(await page.textContent('.bool')).toBe('boolean')
})

test('NODE_ENV', async () => {
expect(await page.textContent('.node-env')).toBe(process.env.NODE_ENV)
})
Expand Down
2 changes: 2 additions & 0 deletions playground/env/index.html
Expand Up @@ -12,6 +12,7 @@ <h1>Environment Variables</h1>
import.meta.env.VITE_EFFECTIVE_MODE_FILE_NAME: <code class="mode-file"></code>
</p>
<p>import.meta.env.VITE_INLINE: <code class="inline"></code></p>
<p>typeof import.meta.env.VITE_BOOL: <code class="bool"></code></p>
<p>process.env.NODE_ENV: <code class="node-env"></code></p>
<p>import.meta.env: <span class="pre env-object"></span></p>
<p>import.meta.url: <span class="pre url"></span></p>
Expand All @@ -25,6 +26,7 @@ <h1>Environment Variables</h1>
text('.custom-prefix', import.meta.env.CUSTOM_PREFIX_ENV_VARIABLE)
text('.mode-file', import.meta.env.VITE_EFFECTIVE_MODE_FILE_NAME)
text('.inline', import.meta.env.VITE_INLINE)
text('.bool', typeof import.meta.env.VITE_BOOL)
text('.node-env', process.env.NODE_ENV)
text('.env-object', JSON.stringify(import.meta.env, null, 2))

Expand Down
3 changes: 3 additions & 0 deletions playground/env/vite.config.js
Expand Up @@ -5,5 +5,8 @@ module.exports = defineConfig({
envPrefix: ['VITE_', 'CUSTOM_PREFIX_'],
build: {
outDir: 'dist/env'
},
define: {
'import.meta.env.VITE_BOOL': true
}
})