diff --git a/packages/vite/src/node/__tests__/plugins/define.spec.ts b/packages/vite/src/node/__tests__/plugins/define.spec.ts index 2165461c77acaa..0742853947cec2 100644 --- a/packages/vite/src/node/__tests__/plugins/define.spec.ts +++ b/packages/vite/src/node/__tests__/plugins/define.spec.ts @@ -109,4 +109,23 @@ describe('definePlugin', () => { /const __vite_import_meta_env__ = .*;\nconst env = __vite_import_meta_env__;/, ) }) + + test('already has marker', async () => { + const transform = await createDefinePluginTransform() + expect( + await transform( + 'console.log(__vite_import_meta_env__);\nconst env = import.meta.env;', + ), + ).toMatch( + /const __vite_import_meta_env__\$ = .*;\nconsole.log\(__vite_import_meta_env__\);\nconst env = __vite_import_meta_env__\$;/, + ) + + expect( + await transform( + 'console.log(__vite_import_meta_env__, __vite_import_meta_env__$);\n const env = import.meta.env;', + ), + ).toMatch( + /const __vite_import_meta_env__\$\$ = .*;\nconsole.log\(__vite_import_meta_env__, __vite_import_meta_env__\$\);\nconst env = __vite_import_meta_env__\$\$;/, + ) + }) }) diff --git a/packages/vite/src/node/plugins/define.ts b/packages/vite/src/node/plugins/define.ts index 585bc0154fa263..87e6dbab5c8e2d 100644 --- a/packages/vite/src/node/plugins/define.ts +++ b/packages/vite/src/node/plugins/define.ts @@ -9,7 +9,6 @@ import { isHTMLRequest } from './html' const nonJsRe = /\.json(?:$|\?)/ const isNonJsRequest = (request: string): boolean => nonJsRe.test(request) const importMetaEnvMarker = '__vite_import_meta_env__' -const bareImportMetaEnvRe = new RegExp(`${importMetaEnvMarker}(?!\\.)\\b`) const importMetaEnvKeyRe = new RegExp(`${importMetaEnvMarker}\\..+?\\b`, 'g') export function definePlugin(config: ResolvedConfig): Plugin { @@ -80,7 +79,6 @@ export function definePlugin(config: ResolvedConfig): Plugin { SSR: ssr + '', ...userDefineEnv, }) - const banner = `const ${importMetaEnvMarker} = ${importMetaEnvVal};\n` // Create regex pattern as a fast check before running esbuild const patternKeys = Object.keys(userDefine) @@ -94,7 +92,7 @@ export function definePlugin(config: ResolvedConfig): Plugin { ? new RegExp(patternKeys.map(escapeRegex).join('|')) : null - return [define, pattern, banner] as const + return [define, pattern, importMetaEnvVal] as const } const defaultPattern = generatePattern(false) @@ -122,13 +120,23 @@ export function definePlugin(config: ResolvedConfig): Plugin { return } - const [define, pattern, banner] = ssr ? ssrPattern : defaultPattern + const [define, pattern, importMetaEnvVal] = ssr + ? ssrPattern + : defaultPattern if (!pattern) return // Check if our code needs any replacements before running esbuild pattern.lastIndex = 0 if (!pattern.test(code)) return + let marker = importMetaEnvMarker + while (new RegExp(escapeRegex(marker)).test(code)) { + marker += '$' + } + if (marker !== importMetaEnvMarker && 'import.meta.env' in define) { + define['import.meta.env'] = marker + } + const result = await replaceDefine(code, id, define, config) // Replace `import.meta.env.*` with undefined @@ -137,8 +145,8 @@ export function definePlugin(config: ResolvedConfig): Plugin { ) // If there's bare `import.meta.env` references, prepend the banner - if (bareImportMetaEnvRe.test(result.code)) { - result.code = banner + result.code + if (new RegExp(escapeRegex(marker)).test(result.code)) { + result.code = `const ${marker} = ${importMetaEnvVal};\n` + result.code if (result.map) { const map = JSON.parse(result.map)