@@ -9,8 +9,7 @@ import { isHTMLRequest } from './html'
9
9
const nonJsRe = / \. j s o n (?: $ | \? ) /
10
10
const isNonJsRequest = ( request : string ) : boolean => nonJsRe . test ( request )
11
11
const importMetaEnvMarker = '__vite_import_meta_env__'
12
- const bareImportMetaEnvRe = new RegExp ( `${ importMetaEnvMarker } (?!\\.)\\b` )
13
- const importMetaEnvKeyRe = new RegExp ( `${ importMetaEnvMarker } \\..+?\\b` , 'g' )
12
+ const importMetaEnvKeyReCache = new Map < string , RegExp > ( )
14
13
15
14
export function definePlugin ( config : ResolvedConfig ) : Plugin {
16
15
const isBuild = config . command === 'build'
@@ -80,7 +79,6 @@ export function definePlugin(config: ResolvedConfig): Plugin {
80
79
SSR : ssr + '' ,
81
80
...userDefineEnv ,
82
81
} )
83
- const banner = `const ${ importMetaEnvMarker } = ${ importMetaEnvVal } ;\n`
84
82
85
83
// Create regex pattern as a fast check before running esbuild
86
84
const patternKeys = Object . keys ( userDefine )
@@ -94,7 +92,7 @@ export function definePlugin(config: ResolvedConfig): Plugin {
94
92
? new RegExp ( patternKeys . map ( escapeRegex ) . join ( '|' ) )
95
93
: null
96
94
97
- return [ define , pattern , banner ] as const
95
+ return [ define , pattern , importMetaEnvVal ] as const
98
96
}
99
97
100
98
const defaultPattern = generatePattern ( false )
@@ -122,28 +120,49 @@ export function definePlugin(config: ResolvedConfig): Plugin {
122
120
return
123
121
}
124
122
125
- const [ define , pattern , banner ] = ssr ? ssrPattern : defaultPattern
123
+ let [ define , pattern , importMetaEnvVal ] = ssr
124
+ ? ssrPattern
125
+ : defaultPattern
126
126
if ( ! pattern ) return
127
127
128
128
// Check if our code needs any replacements before running esbuild
129
129
pattern . lastIndex = 0
130
130
if ( ! pattern . test ( code ) ) return
131
131
132
- const result = await replaceDefine ( code , id , define , config )
132
+ const hasDefineImportMetaEnv = 'import.meta.env' in define
133
+ let marker = importMetaEnvMarker
133
134
134
- // Replace `import.meta.env.*` with undefined
135
- result . code = result . code . replaceAll ( importMetaEnvKeyRe , ( m ) =>
136
- 'undefined' . padEnd ( m . length ) ,
137
- )
135
+ if ( hasDefineImportMetaEnv && code . includes ( marker ) ) {
136
+ // append a number to the marker until it's unique, to avoid if there is a
137
+ // marker already in the code
138
+ let i = 1
139
+ do {
140
+ marker = importMetaEnvMarker + i ++
141
+ } while ( code . includes ( marker ) )
138
142
139
- // If there's bare `import.meta.env` references, prepend the banner
140
- if ( bareImportMetaEnvRe . test ( result . code ) ) {
141
- result . code = banner + result . code
143
+ if ( marker !== importMetaEnvMarker ) {
144
+ define = { ...define , 'import.meta.env' : marker }
145
+ }
146
+ }
142
147
143
- if ( result . map ) {
144
- const map = JSON . parse ( result . map )
145
- map . mappings = ';' + map . mappings
146
- result . map = map
148
+ const result = await replaceDefine ( code , id , define , config )
149
+
150
+ if ( hasDefineImportMetaEnv ) {
151
+ // Replace `import.meta.env.*` with undefined
152
+ result . code = result . code . replaceAll (
153
+ getImportMetaEnvKeyRe ( marker ) ,
154
+ ( m ) => 'undefined' . padEnd ( m . length ) ,
155
+ )
156
+
157
+ // If there's bare `import.meta.env` references, prepend the banner
158
+ if ( result . code . includes ( marker ) ) {
159
+ result . code = `const ${ marker } = ${ importMetaEnvVal } ;\n` + result . code
160
+
161
+ if ( result . map ) {
162
+ const map = JSON . parse ( result . map )
163
+ map . mappings = ';' + map . mappings
164
+ result . map = map
165
+ }
147
166
}
148
167
}
149
168
@@ -219,3 +238,12 @@ function handleDefineValue(value: any): string {
219
238
if ( typeof value === 'string' ) return value
220
239
return JSON . stringify ( value )
221
240
}
241
+
242
+ function getImportMetaEnvKeyRe ( marker : string ) : RegExp {
243
+ let re = importMetaEnvKeyReCache . get ( marker )
244
+ if ( ! re ) {
245
+ re = new RegExp ( `${ marker } \\..+?\\b` , 'g' )
246
+ importMetaEnvKeyReCache . set ( marker , re )
247
+ }
248
+ return re
249
+ }
0 commit comments