@@ -31,66 +31,68 @@ describe('definePlugin', () => {
31
31
const transform = await createDefinePluginTransform ( {
32
32
__APP_VERSION__ : JSON . stringify ( '1.0' ) ,
33
33
} )
34
- expect ( await transform ( 'const version = __APP_VERSION__ ;' ) ) . toBe (
35
- 'const version = "1.0";\n' ,
34
+ expect ( await transform ( 'export const version = __APP_VERSION__ ;' ) ) . toBe (
35
+ 'export const version = "1.0";\n' ,
36
36
)
37
- expect ( await transform ( 'const version = __APP_VERSION__;' ) ) . toBe (
38
- 'const version = "1.0";\n' ,
37
+ expect ( await transform ( 'export const version = __APP_VERSION__;' ) ) . toBe (
38
+ 'export const version = "1.0";\n' ,
39
39
)
40
40
} )
41
41
42
42
test ( 'should not replace if not defined' , async ( ) => {
43
43
const transform = await createDefinePluginTransform ( {
44
44
__APP_VERSION__ : JSON . stringify ( '1.0' ) ,
45
45
} )
46
- expect ( await transform ( 'const version = "1.0";' ) ) . toBe ( undefined )
47
- expect ( await transform ( 'const version = import.meta.SOMETHING' ) ) . toBe (
48
- undefined ,
49
- )
46
+ expect ( await transform ( 'export const version = "1.0";' ) ) . toBe ( undefined )
47
+ expect (
48
+ await transform ( 'export const version = import.meta.SOMETHING' ) ,
49
+ ) . toBe ( undefined )
50
50
} )
51
51
52
52
test ( 'replaces import.meta.env.SSR with false' , async ( ) => {
53
53
const transform = await createDefinePluginTransform ( )
54
- expect ( await transform ( 'const isSSR = import.meta.env.SSR;' ) ) . toBe (
55
- 'const isSSR = false;\n' ,
54
+ expect ( await transform ( 'export const isSSR = import.meta.env.SSR;' ) ) . toBe (
55
+ 'export const isSSR = false;\n' ,
56
56
)
57
57
} )
58
58
59
59
test ( 'preserve import.meta.hot with override' , async ( ) => {
60
60
// assert that the default behavior is to replace import.meta.hot with undefined
61
61
const transform = await createDefinePluginTransform ( )
62
- expect ( await transform ( 'const hot = import.meta.hot;' ) ) . toBe (
63
- 'const hot = void 0;\n' ,
62
+ expect ( await transform ( 'export const hot = import.meta.hot;' ) ) . toBe (
63
+ 'export const hot = void 0;\n' ,
64
64
)
65
65
// assert that we can specify a user define to preserve import.meta.hot
66
66
const overrideTransform = await createDefinePluginTransform ( {
67
67
'import.meta.hot' : 'import.meta.hot' ,
68
68
} )
69
- expect ( await overrideTransform ( 'const hot = import.meta.hot;' ) ) . toBe (
70
- 'const hot = import.meta.hot;\n' ,
69
+ expect ( await overrideTransform ( 'export const hot = import.meta.hot;' ) ) . toBe (
70
+ 'export const hot = import.meta.hot;\n' ,
71
71
)
72
72
} )
73
73
74
74
test ( 'replace import.meta.env.UNKNOWN with undefined' , async ( ) => {
75
75
const transform = await createDefinePluginTransform ( )
76
- expect ( await transform ( 'const foo = import.meta.env.UNKNOWN;' ) ) . toBe (
77
- 'const foo = undefined ;\n' ,
76
+ expect ( await transform ( 'export const foo = import.meta.env.UNKNOWN;' ) ) . toBe (
77
+ 'export const foo = undefined ;\n' ,
78
78
)
79
79
} )
80
80
81
81
test ( 'leave import.meta.env["UNKNOWN"] to runtime' , async ( ) => {
82
82
const transform = await createDefinePluginTransform ( )
83
- expect ( await transform ( 'const foo = import.meta.env["UNKNOWN"];' ) ) . toMatch (
84
- / c o n s t _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ = .* ; \n c o n s t f o o = _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ \[ " U N K N O W N " \] ; / ,
83
+ expect (
84
+ await transform ( 'export const foo = import.meta.env["UNKNOWN"];' ) ,
85
+ ) . toMatch (
86
+ / c o n s t _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ = .* ; \n e x p o r t c o n s t f o o = _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ \[ " U N K N O W N " \] ; / ,
85
87
)
86
88
} )
87
89
88
90
test ( 'preserve import.meta.env.UNKNOWN with override' , async ( ) => {
89
91
const transform = await createDefinePluginTransform ( {
90
92
'import.meta.env.UNKNOWN' : 'import.meta.env.UNKNOWN' ,
91
93
} )
92
- expect ( await transform ( 'const foo = import.meta.env.UNKNOWN;' ) ) . toBe (
93
- 'const foo = import.meta.env.UNKNOWN;\n' ,
94
+ expect ( await transform ( 'export const foo = import.meta.env.UNKNOWN;' ) ) . toBe (
95
+ 'export const foo = import.meta.env.UNKNOWN;\n' ,
94
96
)
95
97
} )
96
98
@@ -101,46 +103,46 @@ describe('definePlugin', () => {
101
103
102
104
expect (
103
105
await transform (
104
- 'const isLegacy = import.meta.env.LEGACY;\nimport.meta.env.UNDEFINED && console.log(import.meta.env.UNDEFINED);' ,
106
+ 'export const isLegacy = import.meta.env.LEGACY;\nimport.meta.env.UNDEFINED && console.log(import.meta.env.UNDEFINED);' ,
105
107
) ,
106
108
) . toMatchInlineSnapshot ( `
107
- "const isLegacy = __VITE_IS_LEGACY__;
109
+ "export const isLegacy = __VITE_IS_LEGACY__;
108
110
undefined && console.log(undefined );
109
111
"
110
112
` )
111
113
} )
112
114
113
115
test ( 'replace bare import.meta.env' , async ( ) => {
114
116
const transform = await createDefinePluginTransform ( )
115
- expect ( await transform ( 'const env = import.meta.env;' ) ) . toMatch (
116
- / c o n s t _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ = .* ; \n c o n s t e n v = _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ ; / ,
117
+ expect ( await transform ( 'export const env = import.meta.env;' ) ) . toMatch (
118
+ / c o n s t _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ = .* ; \n e x p o r t c o n s t e n v = _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ ; / ,
117
119
)
118
120
} )
119
121
120
122
test ( 'already has marker' , async ( ) => {
121
123
const transform = await createDefinePluginTransform ( )
122
124
expect (
123
125
await transform (
124
- 'console.log(__vite_import_meta_env__);\nconst env = import.meta.env;' ,
126
+ 'console.log(__vite_import_meta_env__);\nexport const env = import.meta.env;' ,
125
127
) ,
126
128
) . toMatch (
127
- / c o n s t _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ 1 = .* ; \n c o n s o l e .l o g \( _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ \) ; \n c o n s t e n v = _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ 1 ; / ,
129
+ / c o n s t _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ 1 = .* ; \n c o n s o l e .l o g \( _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ \) ; \n e x p o r t c o n s t e n v = _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ 1 ; / ,
128
130
)
129
131
130
132
expect (
131
133
await transform (
132
- 'console.log(__vite_import_meta_env__, __vite_import_meta_env__1);\n const env = import.meta.env;' ,
134
+ 'console.log(__vite_import_meta_env__, __vite_import_meta_env__1);\n export const env = import.meta.env;' ,
133
135
) ,
134
136
) . toMatch (
135
- / c o n s t _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ 2 = .* ; \n c o n s o l e .l o g \( _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ , _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ 1 \) ; \n c o n s t e n v = _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ 2 ; / ,
137
+ / c o n s t _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ 2 = .* ; \n c o n s o l e .l o g \( _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ , _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ 1 \) ; \n e x p o r t c o n s t e n v = _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ 2 ; / ,
136
138
)
137
139
138
140
expect (
139
141
await transform (
140
- 'console.log(__vite_import_meta_env__);\nconst env = import.meta.env;\nconsole.log(import.meta.env.UNDEFINED);' ,
142
+ 'console.log(__vite_import_meta_env__);\nexport const env = import.meta.env;\nconsole.log(import.meta.env.UNDEFINED);' ,
141
143
) ,
142
144
) . toMatch (
143
- / c o n s t _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ 1 = .* ; \n c o n s o l e .l o g \( _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ \) ; \n c o n s t e n v = _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ 1 ; \n c o n s o l e .l o g \( u n d e f i n e d { 26 } \) ; / ,
145
+ / c o n s t _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ 1 = .* ; \n c o n s o l e .l o g \( _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ \) ; \n e x p o r t c o n s t e n v = _ _ v i t e _ i m p o r t _ m e t a _ e n v _ _ 1 ; \n c o n s o l e .l o g \( u n d e f i n e d { 26 } \) ; / ,
144
146
)
145
147
} )
146
148
} )
0 commit comments