diff --git a/packages/vite/src/node/plugins/define.ts b/packages/vite/src/node/plugins/define.ts index c0c37183b81aaf..c9568ada69799b 100644 --- a/packages/vite/src/node/plugins/define.ts +++ b/packages/vite/src/node/plugins/define.ts @@ -41,13 +41,7 @@ export function definePlugin(config: ResolvedConfig): Plugin { if (isBuild) { const match = key.match(metaEnvRe) if (match) { - userDefineEnv[match[1]] = - // test if value is raw identifier to wrap with __vite__ so when - // stringified for `import.meta.env`, we can remove the quotes and - // retain being an identifier - typeof val === 'string' && /^[\p{L}_$]/u.test(val.trim()) - ? `__vite__define__${val}` - : val + userDefineEnv[match[1]] = `__vite__define__${userDefine[key]}` } } } @@ -67,7 +61,10 @@ export function definePlugin(config: ResolvedConfig): Plugin { ...config.env, SSR: '__vite__ssr__', ...userDefineEnv, - }).replace(/"__vite__define__(.+?)"/g, (_, val) => val), + }).replace( + /"__vite__define__(.+?)"([,}])/g, + (_, val, suffix) => `${val.replace(/(^\\")|(\\"$)/g, '"')}${suffix}`, + ), }) } diff --git a/playground/env/__tests__/env.spec.ts b/playground/env/__tests__/env.spec.ts index 8146eb128df915..91227abe21fb2c 100644 --- a/playground/env/__tests__/env.spec.ts +++ b/playground/env/__tests__/env.spec.ts @@ -37,8 +37,10 @@ test('inline variables', async () => { ) }) -test('bool', async () => { +test('define', async () => { expect(await page.textContent('.bool')).toBe('boolean') + expect(await page.textContent('.number')).toBe('number') + expect(await page.textContent('.string')).toBe('string') }) test('NODE_ENV', async () => { @@ -79,6 +81,8 @@ test('env object', async () => { MODE: mode, DEV: !isBuild, PROD: isBuild, + VITE_NUMBER: 123, + VITE_STRING: '123', }) }) diff --git a/playground/env/index.html b/playground/env/index.html index 07f1cf8f7df79b..547b424ee809e4 100644 --- a/playground/env/index.html +++ b/playground/env/index.html @@ -13,6 +13,8 @@

Environment Variables

import.meta.env.VITE_INLINE:

typeof import.meta.env.VITE_BOOL:

+

typeof import.meta.env.VITE_NUMBER:

+

typeof import.meta.env.VITE_STRING:

process.env.NODE_ENV:

global.process.env.NODE_ENV:

@@ -34,6 +36,8 @@

Environment Variables

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('.number', typeof import.meta.env.VITE_NUMBER) + text('.string', typeof import.meta.env.VITE_STRING) text('.ssr', import.meta.env.SSR) text('.node-env', process.env.NODE_ENV) text('.global-node-env', global.process.env.NODE_ENV) diff --git a/playground/env/vite.config.js b/playground/env/vite.config.js index 58b93b9dd47d0c..1f15f79c6afd3b 100644 --- a/playground/env/vite.config.js +++ b/playground/env/vite.config.js @@ -10,5 +10,7 @@ export default defineConfig({ }, define: { 'import.meta.env.VITE_BOOL': true, + 'import.meta.env.VITE_NUMBER': '123', + 'import.meta.env.VITE_STRING': JSON.stringify('123'), }, })