Skip to content

Commit

Permalink
fix: avoid re-define __vite_import_meta_env__
Browse files Browse the repository at this point in the history
Closes #17874
  • Loading branch information
yuzheng14 committed Aug 12, 2024
1 parent 4e5ce3c commit 49b37b4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
19 changes: 19 additions & 0 deletions packages/vite/src/node/__tests__/plugins/define.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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__\$\$;/,
)
})
})
20 changes: 14 additions & 6 deletions packages/vite/src/node/plugins/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 49b37b4

Please sign in to comment.