From 08fe0faa605944b910249f32851a45d1f649f911 Mon Sep 17 00:00:00 2001 From: bluwy Date: Sun, 3 Jul 2022 10:10:22 +0800 Subject: [PATCH] fix: ensure define overrides import.meta in build --- packages/vite/src/node/plugins/define.ts | 2 +- playground/env/.env | 3 ++- playground/env/__tests__/env.spec.ts | 4 ++++ playground/env/index.html | 2 ++ playground/env/vite.config.js | 3 +++ 5 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/plugins/define.ts b/packages/vite/src/node/plugins/define.ts index 1baff98c9b5ad0..16c930294da59f 100644 --- a/packages/vite/src/node/plugins/define.ts +++ b/packages/vite/src/node/plugins/define.ts @@ -61,8 +61,8 @@ export function definePlugin(config: ResolvedConfig): Plugin { const replacements: Record = { ...(replaceProcessEnv ? processNodeEnv : {}), - ...userDefine, ...importMetaKeys, + ...userDefine, ...(replaceProcessEnv ? processEnv : {}) } diff --git a/playground/env/.env b/playground/env/.env index ad0e78511b8662..ade94e520ffbcf 100644 --- a/playground/env/.env +++ b/playground/env/.env @@ -1,3 +1,4 @@ VITE_CUSTOM_ENV_VARIABLE=1 CUSTOM_PREFIX_ENV_VARIABLE=1 -VITE_EFFECTIVE_MODE_FILE_NAME=.env \ No newline at end of file +VITE_EFFECTIVE_MODE_FILE_NAME=.env +VITE_BOOL=true \ No newline at end of file diff --git a/playground/env/__tests__/env.spec.ts b/playground/env/__tests__/env.spec.ts index 4583c9039116c8..e093d4bc414353 100644 --- a/playground/env/__tests__/env.spec.ts +++ b/playground/env/__tests__/env.spec.ts @@ -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) }) diff --git a/playground/env/index.html b/playground/env/index.html index a7553eeb181f73..46752dbc2c5ab1 100644 --- a/playground/env/index.html +++ b/playground/env/index.html @@ -12,6 +12,7 @@

Environment Variables

import.meta.env.VITE_EFFECTIVE_MODE_FILE_NAME:

import.meta.env.VITE_INLINE:

+

typeof import.meta.env.VITE_BOOL:

process.env.NODE_ENV:

import.meta.env:

import.meta.url:

@@ -25,6 +26,7 @@

Environment Variables

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)) diff --git a/playground/env/vite.config.js b/playground/env/vite.config.js index c96c8f84a321ef..7d3f15e41a6136 100644 --- a/playground/env/vite.config.js +++ b/playground/env/vite.config.js @@ -5,5 +5,8 @@ module.exports = defineConfig({ envPrefix: ['VITE_', 'CUSTOM_PREFIX_'], build: { outDir: 'dist/env' + }, + define: { + 'import.meta.env.VITE_BOOL': true } })