1
1
import { describe , expect , test } from 'vitest'
2
+ import { rolldown } from 'rolldown'
2
3
import { definePlugin } from '../../plugins/define'
3
4
import { resolveConfig } from '../../config'
4
5
import { PartialEnvironment } from '../../baseEnvironment'
@@ -16,17 +17,48 @@ async function createDefinePluginTransform(
16
17
const environment = new PartialEnvironment ( ssr ? 'ssr' : 'client' , config )
17
18
18
19
return async ( code : string ) => {
19
- // @ts -expect-error transform.handler should exist
20
- const result = await instance . transform . handler . call (
21
- { environment } ,
22
- code ,
23
- 'foo.ts' ,
24
- )
25
- return result ?. code || result
20
+ if ( process . env . _VITE_TEST_JS_PLUGIN ) {
21
+ // @ts -expect-error transform.handler should exist
22
+ const result = await instance . transform . handler . call (
23
+ { environment } ,
24
+ code ,
25
+ 'foo.ts' ,
26
+ )
27
+ return result ?. code || result
28
+ } else {
29
+ const bundler = await rolldown ( {
30
+ input : 'entry.js' ,
31
+ plugins : [
32
+ {
33
+ name : 'test' ,
34
+ resolveId ( id ) {
35
+ if ( id === 'entry.js' ) {
36
+ return '\0' + id
37
+ }
38
+ } ,
39
+ load ( id ) {
40
+ if ( id === '\0entry.js' ) {
41
+ return code
42
+ }
43
+ } ,
44
+ } ,
45
+ {
46
+ name : 'native:define' ,
47
+ options : ( definePlugin ( config ) . options ! as any ) . bind ( {
48
+ environment,
49
+ } ) ,
50
+ } ,
51
+ ] ,
52
+ experimental : {
53
+ attachDebugInfo : 'none' ,
54
+ } ,
55
+ } )
56
+ return ( await bundler . generate ( ) ) . output [ 0 ] . code
57
+ }
26
58
}
27
59
}
28
60
29
- describe ( 'definePlugin' , ( ) => {
61
+ describe . skipIf ( ! process . env . _VITE_TEST_JS_PLUGIN ) ( 'definePlugin' , ( ) => {
30
62
test ( 'replaces custom define' , async ( ) => {
31
63
const transform = await createDefinePluginTransform ( {
32
64
__APP_VERSION__ : JSON . stringify ( '1.0' ) ,
@@ -146,3 +178,120 @@ describe('definePlugin', () => {
146
178
)
147
179
} )
148
180
} )
181
+
182
+ describe . skipIf ( process . env . _VITE_TEST_JS_PLUGIN ) ( 'native definePlugin' , ( ) => {
183
+ test ( 'replaces custom define' , async ( ) => {
184
+ const transform = await createDefinePluginTransform ( {
185
+ __APP_VERSION__ : JSON . stringify ( '1.0' ) ,
186
+ } )
187
+ expect ( await transform ( 'export const version = __APP_VERSION__;' ) ) . toBe (
188
+ 'const version = "1.0";\n\nexport { version };' ,
189
+ )
190
+ expect ( await transform ( 'export const version = __APP_VERSION__ ;' ) ) . toBe (
191
+ 'const version = "1.0";\n\nexport { version };' ,
192
+ )
193
+ } )
194
+
195
+ test ( 'should not replace if not defined' , async ( ) => {
196
+ const transform = await createDefinePluginTransform ( {
197
+ __APP_VERSION__ : JSON . stringify ( '1.0' ) ,
198
+ } )
199
+ expect ( await transform ( 'export const version = "1.0";' ) ) . toBe (
200
+ 'const version = "1.0";\n\nexport { version };' ,
201
+ )
202
+ expect (
203
+ await transform ( 'export const version = import.meta.SOMETHING' ) ,
204
+ ) . toBe ( 'const version = import.meta.SOMETHING;\n\nexport { version };' )
205
+ } )
206
+
207
+ test ( 'replaces import.meta.env.SSR with false' , async ( ) => {
208
+ const transform = await createDefinePluginTransform ( )
209
+ expect ( await transform ( 'export const isSSR = import.meta.env.SSR;' ) ) . toBe (
210
+ 'const isSSR = false;\n\nexport { isSSR };' ,
211
+ )
212
+ } )
213
+
214
+ test ( 'preserve import.meta.hot with override' , async ( ) => {
215
+ // assert that the default behavior is to replace import.meta.hot with undefined
216
+ const transform = await createDefinePluginTransform ( )
217
+ expect ( await transform ( 'export const hot = import.meta.hot;' ) ) . toBe (
218
+ 'const hot = void 0;\n\nexport { hot };' ,
219
+ )
220
+ // assert that we can specify a user define to preserve import.meta.hot
221
+ const overrideTransform = await createDefinePluginTransform ( {
222
+ 'import.meta.hot' : 'import.meta.hot' ,
223
+ } )
224
+ expect ( await overrideTransform ( 'export const hot = import.meta.hot;' ) ) . toBe (
225
+ 'const hot = import.meta.hot;\n\nexport { hot };' ,
226
+ )
227
+ } )
228
+
229
+ test ( 'replace import.meta.env.UNKNOWN with undefined' , async ( ) => {
230
+ const transform = await createDefinePluginTransform ( )
231
+ expect ( await transform ( 'export const foo = import.meta.env.UNKNOWN;' ) ) . toBe (
232
+ 'const foo = void 0;\n\nexport { foo };' ,
233
+ )
234
+ } )
235
+
236
+ test ( 'leave import.meta.env["UNKNOWN"] to runtime' , async ( ) => {
237
+ const transform = await createDefinePluginTransform ( )
238
+ expect (
239
+ await transform ( 'export const foo = import.meta.env["UNKNOWN"];' ) ,
240
+ ) . toMatch ( / c o n s t f o o = .* \[ " U N K N O W N " \] ; \n \n e x p o r t \{ f o o \} ; / s)
241
+ } )
242
+
243
+ test ( 'preserve import.meta.env.UNKNOWN with override' , async ( ) => {
244
+ const transform = await createDefinePluginTransform ( {
245
+ 'import.meta.env.UNKNOWN' : 'import.meta.env.UNKNOWN' ,
246
+ } )
247
+ expect ( await transform ( 'export const foo = import.meta.env.UNKNOWN;' ) ) . toBe (
248
+ 'const foo = import.meta.env.UNKNOWN;\n\nexport { foo };' ,
249
+ )
250
+ } )
251
+
252
+ test ( 'replace import.meta.env when it is a invalid json' , async ( ) => {
253
+ const transform = await createDefinePluginTransform ( {
254
+ 'import.meta.env.LEGACY' : '__VITE_IS_LEGACY__' ,
255
+ } )
256
+
257
+ expect (
258
+ await transform (
259
+ 'export const isLegacy = import.meta.env.LEGACY;\nimport.meta.env.UNDEFINED && console.log(import.meta.env.UNDEFINED);' ,
260
+ ) ,
261
+ ) . toMatchInlineSnapshot (
262
+ `"const isLegacy = __VITE_IS_LEGACY__;\n\nexport { isLegacy };"` ,
263
+ )
264
+ } )
265
+
266
+ test ( 'replace bare import.meta.env' , async ( ) => {
267
+ const transform = await createDefinePluginTransform ( )
268
+ expect ( await transform ( 'export const env = import.meta.env;' ) ) . toMatch (
269
+ / c o n s t e n v = .* ; \n \n e x p o r t \{ e n v \} ; / s,
270
+ )
271
+ } )
272
+
273
+ test ( 'already has marker' , async ( ) => {
274
+ const transform = await createDefinePluginTransform ( )
275
+ expect (
276
+ await transform (
277
+ 'console.log(__vite_import_meta_env__);\nexport const env = import.meta.env;' ,
278
+ ) ,
279
+ ) . toMatch ( / 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 = .* / )
280
+
281
+ expect (
282
+ await transform (
283
+ 'console.log(__vite_import_meta_env__, __vite_import_meta_env__1);\n export const env = import.meta.env;' ,
284
+ ) ,
285
+ ) . toMatch (
286
+ / 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 = .* / ,
287
+ )
288
+
289
+ expect (
290
+ await transform (
291
+ 'console.log(__vite_import_meta_env__);\nexport const env = import.meta.env;\nconsole.log(import.meta.env.UNDEFINED);' ,
292
+ ) ,
293
+ ) . toMatch (
294
+ / 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 = .* ; \n c o n s o l e .l o g \( v o i d 0 \) ; / s,
295
+ )
296
+ } )
297
+ } )
0 commit comments