File tree 7 files changed +34
-19
lines changed
7 files changed +34
-19
lines changed Original file line number Diff line number Diff line change 1
1
test ( 'string' , async ( ) => {
2
2
const defines = require ( '../vite.config.js' ) . define
3
3
4
- expect ( await page . textContent ( '.string' ) ) . toBe ( String ( defines . __STRING__ ) )
4
+ expect ( await page . textContent ( '.exp' ) ) . toBe ( String ( eval ( defines . __EXP__ ) ) )
5
+ expect ( await page . textContent ( '.string' ) ) . toBe ( JSON . parse ( defines . __STRING__ ) )
5
6
expect ( await page . textContent ( '.number' ) ) . toBe ( String ( defines . __NUMBER__ ) )
6
7
expect ( await page . textContent ( '.boolean' ) ) . toBe ( String ( defines . __BOOLEAN__ ) )
7
8
expect ( await page . textContent ( '.object' ) ) . toBe (
Original file line number Diff line number Diff line change 1
1
< h1 > Define</ h1 >
2
2
3
+ < p > Raw Expression < code class ="exp "> </ code > </ p >
3
4
< p > String < code class ="string "> </ code > </ p >
4
5
< p > Number < code class ="number "> </ code > </ p >
5
6
< p > Boolean < code class ="boolean "> </ code > </ p >
6
7
< p > Object < span class ="pre object "> </ span > </ p >
7
8
8
9
< script type ="module ">
10
+ text ( '.exp' , __EXP__ )
9
11
text ( '.string' , __STRING__ )
10
12
text ( '.number' , __NUMBER__ )
11
13
text ( '.boolean' , __BOOLEAN__ )
Original file line number Diff line number Diff line change 1
1
module . exports = {
2
2
define : {
3
- __STRING__ : 'hello' ,
3
+ __EXP__ : '1 + 1' ,
4
+ __STRING__ : '"hello"' ,
4
5
__NUMBER__ : 123 ,
5
6
__BOOLEAN__ : true ,
6
7
__OBJ__ : {
Original file line number Diff line number Diff line change @@ -269,15 +269,6 @@ export async function resolveConfig(
269
269
270
270
// load .env files
271
271
const userEnv = loadEnv ( mode , resolvedRoot )
272
- // check if user defined any import.meta.env variables
273
- if ( config . define ) {
274
- const prefix = `import.meta.env.`
275
- for ( const key in config . define ) {
276
- if ( key . startsWith ( prefix ) ) {
277
- userEnv [ key . slice ( prefix . length ) ] = config . define [ key ]
278
- }
279
- }
280
- }
281
272
282
273
// Note it is possible for user to have a custom mode, e.g. `staging` where
283
274
// production-like behavior is expected. This is indicated by NODE_ENV=production
Original file line number Diff line number Diff line change @@ -44,7 +44,7 @@ export function clientInjectionsPlugin(config: ResolvedConfig): Plugin {
44
44
. replace ( `__MODE__` , JSON . stringify ( config . mode ) )
45
45
. replace ( `__BASE__` , JSON . stringify ( config . base ) )
46
46
. replace ( `__ROOT__` , JSON . stringify ( config . root ) )
47
- . replace ( `__DEFINES__` , JSON . stringify ( config . define || { } ) )
47
+ . replace ( `__DEFINES__` , serializeDefine ( config . define || { } ) )
48
48
. replace ( `__HMR_PROTOCOL__` , JSON . stringify ( protocol ) )
49
49
. replace ( `__HMR_HOSTNAME__` , JSON . stringify ( host ) )
50
50
. replace ( `__HMR_PORT__` , JSON . stringify ( port ) )
@@ -60,3 +60,14 @@ export function clientInjectionsPlugin(config: ResolvedConfig): Plugin {
60
60
}
61
61
}
62
62
}
63
+
64
+ function serializeDefine ( define : Record < string , any > ) : string {
65
+ let res = `{`
66
+ for ( const key in define ) {
67
+ const val = define [ key ]
68
+ res += `${ JSON . stringify ( key ) } : ${
69
+ typeof val === 'string' ? `(${ val } )` : JSON . stringify ( val )
70
+ } , `
71
+ }
72
+ return res + `}`
73
+ }
Original file line number Diff line number Diff line change @@ -9,7 +9,8 @@ export function definePlugin(config: ResolvedConfig): Plugin {
9
9
10
10
const userDefine : Record < string , string > = { }
11
11
for ( const key in config . define ) {
12
- userDefine [ key ] = JSON . stringify ( config . define [ key ] )
12
+ const val = config . define [ key ]
13
+ userDefine [ key ] = typeof val === 'string' ? val : JSON . stringify ( val )
13
14
}
14
15
15
16
// during dev, import.meta properties are handled by importAnalysis plugin
Original file line number Diff line number Diff line change @@ -411,12 +411,20 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
411
411
412
412
if ( hasEnv ) {
413
413
// inject import.meta.env
414
- str ( ) . prepend (
415
- `import.meta.env = ${ JSON . stringify ( {
416
- ...config . env ,
417
- SSR : ! ! ssr
418
- } ) } ;`
419
- )
414
+ let env = `import.meta.env = ${ JSON . stringify ( {
415
+ ...config . env ,
416
+ SSR : ! ! ssr
417
+ } ) } ;`
418
+ // account for user env defines
419
+ for ( const key in config . define ) {
420
+ if ( key . startsWith ( `import.meta.env.` ) ) {
421
+ const val = config . define [ key ]
422
+ env += `${ key } = ${
423
+ typeof val === 'string' ? val : JSON . stringify ( val )
424
+ } ;`
425
+ }
426
+ }
427
+ str ( ) . prepend ( env )
420
428
}
421
429
422
430
if ( hasHMR && ! ssr ) {
You can’t perform that action at this time.
0 commit comments