diff --git a/packages/vite/src/node/env.ts b/packages/vite/src/node/env.ts index 95589b9b1e55b1..338de6784497d8 100644 --- a/packages/vite/src/node/env.ts +++ b/packages/vite/src/node/env.ts @@ -47,19 +47,9 @@ export function loadEnv( process.env.BROWSER_ARGS = parsed.BROWSER_ARGS } - try { - // let environment variables use each other - expand({ parsed }) - } catch (e) { - // custom error handling until https://github.com/motdotla/dotenv-expand/issues/65 is fixed upstream - // check for message "TypeError: Cannot read properties of undefined (reading 'split')" - if (e.message.includes('split')) { - throw new Error( - 'dotenv-expand failed to expand env vars. Maybe you need to escape `$`?', - ) - } - throw e - } + // let environment variables use each other + // `expand` patched in patches/dotenv-expand@9.0.0.patch + expand({ parsed }) // only keys that start with prefix are exposed to client for (const [key, value] of Object.entries(parsed)) { diff --git a/patches/dotenv-expand@9.0.0.patch b/patches/dotenv-expand@9.0.0.patch index fb6859346d75d1..979879e22a602c 100644 --- a/patches/dotenv-expand@9.0.0.patch +++ b/patches/dotenv-expand@9.0.0.patch @@ -1,8 +1,19 @@ diff --git a/lib/main.js b/lib/main.js -index c873cc77229d4cd0cf9de98ae0970b25d89f312f..ddf570558e985760efde52af37a41b56282d30a6 100644 +index c873cc77229d4cd0cf9de98ae0970b25d89f312f..901758c6b665d2935501404fc09c6abd94b7eb1e 100644 --- a/lib/main.js +++ b/lib/main.js -@@ -50,9 +50,10 @@ function expand (config) { +@@ -17,6 +17,10 @@ function _interpolate (envValue, environment, config) { + replacePart = parts[0] + value = replacePart.replace('\\$', '$') + } else { ++ // PATCH: compatible with env variables ended with unescaped $ ++ if(!parts[2]) { ++ return newEnv ++ } + const keyParts = parts[2].split(':-') + const key = keyParts[0] + replacePart = parts[0].substring(prefix.length) +@@ -50,9 +54,10 @@ function expand (config) { config.parsed[configKey] = _interpolate(value, environment, config) } diff --git a/playground/env/.env b/playground/env/.env index 7d2085287f2c39..bca550ecc1f768 100644 --- a/playground/env/.env +++ b/playground/env/.env @@ -2,4 +2,10 @@ VITE_CUSTOM_ENV_VARIABLE=1 CUSTOM_PREFIX_ENV_VARIABLE=1 VITE_EFFECTIVE_MODE_FILE_NAME=.env VITE_BOOL=true -VITE_EXPAND=$EXPAND +VITE_EXPAND_A=$EXPAND +VITE_EXPAND_B=$DEPEND_ENV +VITE_ESCAPE_A=escape\$ +VITE_ESCAPE_B=escape$ +IRRELEVANT_ENV=$DEPEND_ENV +IRRELEVANT_ESCAPE_ENV=irrelevant$ +DEPEND_ENV=depend diff --git a/playground/env/__tests__/env.spec.ts b/playground/env/__tests__/env.spec.ts index dc4a32653efdab..281c055e0041c0 100644 --- a/playground/env/__tests__/env.spec.ts +++ b/playground/env/__tests__/env.spec.ts @@ -46,7 +46,8 @@ test('NODE_ENV', async () => { }) test('expand', async () => { - expect(await page.textContent('.expand')).toBe('expand') + expect(await page.textContent('.expand-a')).toBe('expand') + expect(await page.textContent('.expand-b')).toBe('depend') }) test('ssr', async () => { @@ -54,11 +55,20 @@ test('ssr', async () => { }) test('env object', async () => { - const envText = await page.textContent('.env-object') - expect(JSON.parse(envText)).toMatchObject({ + const env = JSON.parse(await page.textContent('.env-object')) + expect(env).not.toHaveProperty([ + 'DEPEND_ENV', + 'IRRELEVANT_ENV', + 'IRRELEVANT_ESCAPE_ENV', + ]) + expect(env).toMatchObject({ VITE_EFFECTIVE_MODE_FILE_NAME: `.env.${mode}`, CUSTOM_PREFIX_ENV_VARIABLE: '1', VITE_CUSTOM_ENV_VARIABLE: '1', + VITE_EXPAND_A: 'expand', + VITE_EXPAND_B: 'depend', + VITE_ESCAPE_A: 'escape$', + VITE_ESCAPE_B: 'escape$', BASE_URL: '/env/', VITE_BOOL: true, SSR: false, diff --git a/playground/env/index.html b/playground/env/index.html index 9b698ff237e426..3e146fae6184d1 100644 --- a/playground/env/index.html +++ b/playground/env/index.html @@ -14,7 +14,8 @@

Environment Variables

import.meta.env.VITE_INLINE:

typeof import.meta.env.VITE_BOOL:

process.env.NODE_ENV:

-

import.meta.env.VITE_EXPAND:

+

import.meta.env.VITE_EXPAND_A:

+

import.meta.env.VITE_EXPAND_B:

import.meta.env.SSR:

import.meta.env:

import.meta.url:

@@ -32,7 +33,8 @@

Environment Variables

text('.ssr', import.meta.env.SSR) text('.node-env', process.env.NODE_ENV) text('.env-object', JSON.stringify(import.meta.env, null, 2)) - text('.expand', import.meta.env.VITE_EXPAND) + text('.expand-a', import.meta.env.VITE_EXPAND_A) + text('.expand-b', import.meta.env.VITE_EXPAND_B) function text(el, text) { document.querySelector(el).textContent = text diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 62dc61b2240b13..8e48e76538290a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -7,7 +7,7 @@ packageExtensionsChecksum: 2a87e01b470616d3b7def19cc0830231 patchedDependencies: dotenv-expand@9.0.0: - hash: 6vdpvodnfhahap67xag6diqqtu + hash: dccccn23nvejzy75sgiosdt2au path: patches/dotenv-expand@9.0.0.patch sirv@2.0.2: hash: hmoqtj4vy3i7wnpchga2a2mu3y @@ -260,7 +260,7 @@ importers: debug: 4.3.4 dep-types: link:src/types dotenv: 16.0.3 - dotenv-expand: 9.0.0_6vdpvodnfhahap67xag6diqqtu + dotenv-expand: 9.0.0_dccccn23nvejzy75sgiosdt2au es-module-lexer: 1.1.0 estree-walker: 3.0.3 etag: 1.8.1 @@ -4939,7 +4939,7 @@ packages: is-obj: 2.0.0 dev: true - /dotenv-expand/9.0.0_6vdpvodnfhahap67xag6diqqtu: + /dotenv-expand/9.0.0_dccccn23nvejzy75sgiosdt2au: resolution: {integrity: sha512-uW8Hrhp5ammm9x7kBLR6jDfujgaDarNA02tprvZdyrJ7MpdzD1KyrIHG4l+YoC2fJ2UcdFdNWNWIjt+sexBHJw==} engines: {node: '>=12'} dev: true