-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
ssrTransform.spec.ts
1032 lines (901 loc) · 28.3 KB
/
ssrTransform.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { expect, test } from 'vitest'
import { transformWithEsbuild } from '../../plugins/esbuild'
import { ssrTransform } from '../ssrTransform'
const ssrTransformSimple = async (code: string, url = '') =>
ssrTransform(code, null, url, code)
const ssrTransformSimpleCode = async (code: string, url?: string) =>
(await ssrTransformSimple(code, url))?.code
test('default import', async () => {
expect(
await ssrTransformSimpleCode(`import foo from 'vue';console.log(foo.bar)`),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["default"]});
console.log(__vite_ssr_import_0__.default.bar)"
`)
})
test('named import', async () => {
expect(
await ssrTransformSimpleCode(
`import { ref } from 'vue';function foo() { return ref(0) }`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["ref"]});
function foo() { return __vite_ssr_import_0__.ref(0) }"
`)
})
test('namespace import', async () => {
expect(
await ssrTransformSimpleCode(
`import * as vue from 'vue';function foo() { return vue.ref(0) }`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue");
function foo() { return __vite_ssr_import_0__.ref(0) }"
`)
})
test('export function declaration', async () => {
expect(await ssrTransformSimpleCode(`export function foo() {}`))
.toMatchInlineSnapshot(`
"function foo() {}
Object.defineProperty(__vite_ssr_exports__, "foo", { enumerable: true, configurable: true, get(){ return foo }});"
`)
})
test('export class declaration', async () => {
expect(await ssrTransformSimpleCode(`export class foo {}`))
.toMatchInlineSnapshot(`
"class foo {}
Object.defineProperty(__vite_ssr_exports__, "foo", { enumerable: true, configurable: true, get(){ return foo }});"
`)
})
test('export var declaration', async () => {
expect(await ssrTransformSimpleCode(`export const a = 1, b = 2`))
.toMatchInlineSnapshot(`
"const a = 1, b = 2
Object.defineProperty(__vite_ssr_exports__, "a", { enumerable: true, configurable: true, get(){ return a }});
Object.defineProperty(__vite_ssr_exports__, "b", { enumerable: true, configurable: true, get(){ return b }});"
`)
})
test('export named', async () => {
expect(
await ssrTransformSimpleCode(`const a = 1, b = 2; export { a, b as c }`),
).toMatchInlineSnapshot(`
"const a = 1, b = 2;
Object.defineProperty(__vite_ssr_exports__, "a", { enumerable: true, configurable: true, get(){ return a }});
Object.defineProperty(__vite_ssr_exports__, "c", { enumerable: true, configurable: true, get(){ return b }});"
`)
})
test('export named from', async () => {
expect(
await ssrTransformSimpleCode(`export { ref, computed as c } from 'vue'`),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["ref","computed"]});
Object.defineProperty(__vite_ssr_exports__, "ref", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_0__.ref }});
Object.defineProperty(__vite_ssr_exports__, "c", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_0__.computed }});"
`)
})
test('named exports of imported binding', async () => {
expect(
await ssrTransformSimpleCode(
`import {createApp} from 'vue';export {createApp}`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["createApp"]});
Object.defineProperty(__vite_ssr_exports__, "createApp", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_0__.createApp }});"
`)
})
test('export * from', async () => {
expect(
await ssrTransformSimpleCode(
`export * from 'vue'\n` + `export * from 'react'`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue");
__vite_ssr_exportAll__(__vite_ssr_import_0__);
const __vite_ssr_import_1__ = await __vite_ssr_import__("react");
__vite_ssr_exportAll__(__vite_ssr_import_1__);
"
`)
})
test('export * as from', async () => {
expect(await ssrTransformSimpleCode(`export * as foo from 'vue'`))
.toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue");
Object.defineProperty(__vite_ssr_exports__, "foo", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_0__ }});"
`)
})
test('export default', async () => {
expect(
await ssrTransformSimpleCode(`export default {}`),
).toMatchInlineSnapshot(`"__vite_ssr_exports__.default = {}"`)
})
test('export then import minified', async () => {
expect(
await ssrTransformSimpleCode(
`export * from 'vue';import {createApp} from 'vue';`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["createApp"]});
const __vite_ssr_import_1__ = await __vite_ssr_import__("vue");
__vite_ssr_exportAll__(__vite_ssr_import_1__);
"
`)
})
test('hoist import to top', async () => {
expect(
await ssrTransformSimpleCode(
`path.resolve('server.js');import path from 'node:path';`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("node:path", {"importedNames":["default"]});
__vite_ssr_import_0__.default.resolve('server.js');"
`)
})
test('import.meta', async () => {
expect(
await ssrTransformSimpleCode(`console.log(import.meta.url)`),
).toMatchInlineSnapshot(`"console.log(__vite_ssr_import_meta__.url)"`)
})
test('dynamic import', async () => {
const result = await ssrTransformSimple(
`export const i = () => import('./foo')`,
)
expect(result?.code).toMatchInlineSnapshot(`
"const i = () => __vite_ssr_dynamic_import__('./foo')
Object.defineProperty(__vite_ssr_exports__, "i", { enumerable: true, configurable: true, get(){ return i }});"
`)
expect(result?.deps).toEqual([])
expect(result?.dynamicDeps).toEqual(['./foo'])
})
test('do not rewrite method definition', async () => {
const result = await ssrTransformSimple(
`import { fn } from 'vue';class A { fn() { fn() } }`,
)
expect(result?.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["fn"]});
class A { fn() { __vite_ssr_import_0__.fn() } }"
`)
expect(result?.deps).toEqual(['vue'])
})
test('do not rewrite when variable is in scope', async () => {
const result = await ssrTransformSimple(
`import { fn } from 'vue';function A(){ const fn = () => {}; return { fn }; }`,
)
expect(result?.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["fn"]});
function A(){ const fn = () => {}; return { fn }; }"
`)
expect(result?.deps).toEqual(['vue'])
})
// #5472
test('do not rewrite when variable is in scope with object destructuring', async () => {
const result = await ssrTransformSimple(
`import { fn } from 'vue';function A(){ let {fn, test} = {fn: 'foo', test: 'bar'}; return { fn }; }`,
)
expect(result?.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["fn"]});
function A(){ let {fn, test} = {fn: 'foo', test: 'bar'}; return { fn }; }"
`)
expect(result?.deps).toEqual(['vue'])
})
// #5472
test('do not rewrite when variable is in scope with array destructuring', async () => {
const result = await ssrTransformSimple(
`import { fn } from 'vue';function A(){ let [fn, test] = ['foo', 'bar']; return { fn }; }`,
)
expect(result?.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["fn"]});
function A(){ let [fn, test] = ['foo', 'bar']; return { fn }; }"
`)
expect(result?.deps).toEqual(['vue'])
})
// #5727
test('rewrite variable in string interpolation in function nested arguments', async () => {
const result = await ssrTransformSimple(
`import { fn } from 'vue';function A({foo = \`test\${fn}\`} = {}){ return {}; }`,
)
expect(result?.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["fn"]});
function A({foo = \`test\${__vite_ssr_import_0__.fn}\`} = {}){ return {}; }"
`)
expect(result?.deps).toEqual(['vue'])
})
// #6520
test('rewrite variables in default value of destructuring params', async () => {
const result = await ssrTransformSimple(
`import { fn } from 'vue';function A({foo = fn}){ return {}; }`,
)
expect(result?.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["fn"]});
function A({foo = __vite_ssr_import_0__.fn}){ return {}; }"
`)
expect(result?.deps).toEqual(['vue'])
})
test('do not rewrite when function declaration is in scope', async () => {
const result = await ssrTransformSimple(
`import { fn } from 'vue';function A(){ function fn() {}; return { fn }; }`,
)
expect(result?.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["fn"]});
function A(){ function fn() {}; return { fn }; }"
`)
expect(result?.deps).toEqual(['vue'])
})
// #16452
test('do not rewrite when function expression is in scope', async () => {
const result = await ssrTransformSimple(
`import {fn} from './vue';var a = function() { return function fn() { console.log(fn) } }`,
)
expect(result?.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("./vue", {"importedNames":["fn"]});
var a = function() { return function fn() { console.log(fn) } }"
`)
})
// #16452
test('do not rewrite when function expression is in global scope', async () => {
const result = await ssrTransformSimple(
`import {fn} from './vue';foo(function fn(a = fn) { console.log(fn) })`,
)
expect(result?.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("./vue", {"importedNames":["fn"]});
foo(function fn(a = fn) { console.log(fn) })"
`)
})
test('do not rewrite when class declaration is in scope', async () => {
const result = await ssrTransformSimple(
`import { cls } from 'vue';function A(){ class cls {} return { cls }; }`,
)
expect(result?.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["cls"]});
function A(){ class cls {} return { cls }; }"
`)
expect(result?.deps).toEqual(['vue'])
})
test('do not rewrite when class expression is in scope', async () => {
const result = await ssrTransformSimple(
`import { cls } from './vue';var a = function() { return class cls { constructor() { console.log(cls) } } }`,
)
expect(result?.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("./vue", {"importedNames":["cls"]});
var a = function() { return class cls { constructor() { console.log(cls) } } }"
`)
})
test('do not rewrite when class expression is in global scope', async () => {
const result = await ssrTransformSimple(
`import { cls } from './vue';foo(class cls { constructor() { console.log(cls) } })`,
)
expect(result?.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("./vue", {"importedNames":["cls"]});
foo(class cls { constructor() { console.log(cls) } })"
`)
})
test('do not rewrite catch clause', async () => {
const result = await ssrTransformSimple(
`import {error} from './dependency';try {} catch(error) {}`,
)
expect(result?.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("./dependency", {"importedNames":["error"]});
try {} catch(error) {}"
`)
expect(result?.deps).toEqual(['./dependency'])
})
// #2221
test('should declare variable for imported super class', async () => {
expect(
await ssrTransformSimpleCode(
`import { Foo } from './dependency';` + `class A extends Foo {}`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("./dependency", {"importedNames":["Foo"]});
const Foo = __vite_ssr_import_0__.Foo;
class A extends Foo {}"
`)
// exported classes: should prepend the declaration at root level, before the
// first class that uses the binding
expect(
await ssrTransformSimpleCode(
`import { Foo } from './dependency';` +
`export default class A extends Foo {}\n` +
`export class B extends Foo {}`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("./dependency", {"importedNames":["Foo"]});
const Foo = __vite_ssr_import_0__.Foo;
class A extends Foo {}
class B extends Foo {}
Object.defineProperty(__vite_ssr_exports__, "B", { enumerable: true, configurable: true, get(){ return B }});
Object.defineProperty(__vite_ssr_exports__, "default", { enumerable: true, configurable: true, value: A });"
`)
})
// #4049
test('should handle default export variants', async () => {
// default anonymous functions
expect(await ssrTransformSimpleCode(`export default function() {}\n`))
.toMatchInlineSnapshot(`
"__vite_ssr_exports__.default = function() {}
"
`)
// default anonymous class
expect(await ssrTransformSimpleCode(`export default class {}\n`))
.toMatchInlineSnapshot(`
"__vite_ssr_exports__.default = class {}
"
`)
// default named functions
expect(
await ssrTransformSimpleCode(
`export default function foo() {}\n` +
`foo.prototype = Object.prototype;`,
),
).toMatchInlineSnapshot(`
"function foo() {}
foo.prototype = Object.prototype;
Object.defineProperty(__vite_ssr_exports__, "default", { enumerable: true, configurable: true, value: foo });"
`)
// default named classes
expect(
await ssrTransformSimpleCode(
`export default class A {}\n` + `export class B extends A {}`,
),
).toMatchInlineSnapshot(`
"class A {}
class B extends A {}
Object.defineProperty(__vite_ssr_exports__, "B", { enumerable: true, configurable: true, get(){ return B }});
Object.defineProperty(__vite_ssr_exports__, "default", { enumerable: true, configurable: true, value: A });"
`)
})
test('sourcemap source', async () => {
const map = (
await ssrTransform(
`export const a = 1`,
null,
'input.js',
'export const a = 1 /* */',
)
)?.map
expect(map?.sources).toStrictEqual(['input.js'])
expect(map?.sourcesContent).toStrictEqual(['export const a = 1 /* */'])
})
test('overwrite bindings', async () => {
expect(
await ssrTransformSimpleCode(
`import { inject } from 'vue';` +
`const a = { inject }\n` +
`const b = { test: inject }\n` +
`function c() { const { test: inject } = { test: true }; console.log(inject) }\n` +
`const d = inject\n` +
`function f() { console.log(inject) }\n` +
`function e() { const { inject } = { inject: true } }\n` +
`function g() { const f = () => { const inject = true }; console.log(inject) }\n`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["inject"]});
const a = { inject: __vite_ssr_import_0__.inject }
const b = { test: __vite_ssr_import_0__.inject }
function c() { const { test: inject } = { test: true }; console.log(inject) }
const d = __vite_ssr_import_0__.inject
function f() { console.log(__vite_ssr_import_0__.inject) }
function e() { const { inject } = { inject: true } }
function g() { const f = () => { const inject = true }; console.log(__vite_ssr_import_0__.inject) }
"
`)
})
test('Empty array pattern', async () => {
expect(
await ssrTransformSimpleCode(`const [, LHS, RHS] = inMatch;`),
).toMatchInlineSnapshot(`"const [, LHS, RHS] = inMatch;"`)
})
test('function argument destructure', async () => {
expect(
await ssrTransformSimpleCode(
`
import { foo, bar } from 'foo'
const a = ({ _ = foo() }) => {}
function b({ _ = bar() }) {}
function c({ _ = bar() + foo() }) {}
`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["foo","bar"]});
const a = ({ _ = __vite_ssr_import_0__.foo() }) => {}
function b({ _ = __vite_ssr_import_0__.bar() }) {}
function c({ _ = __vite_ssr_import_0__.bar() + __vite_ssr_import_0__.foo() }) {}
"
`)
})
test('object destructure alias', async () => {
expect(
await ssrTransformSimpleCode(
`
import { n } from 'foo'
const a = () => {
const { type: n = 'bar' } = {}
console.log(n)
}
`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["n"]});
const a = () => {
const { type: n = 'bar' } = {}
console.log(n)
}
"
`)
// #9585
expect(
await ssrTransformSimpleCode(
`
import { n, m } from 'foo'
const foo = {}
{
const { [n]: m } = foo
}
`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["n","m"]});
const foo = {}
{
const { [__vite_ssr_import_0__.n]: m } = foo
}
"
`)
})
test('nested object destructure alias', async () => {
expect(
await ssrTransformSimpleCode(
`
import { remove, add, get, set, rest, objRest } from 'vue'
function a() {
const {
o: { remove },
a: { b: { c: [ add ] }},
d: [{ get }, set, ...rest],
...objRest
} = foo
remove()
add()
get()
set()
rest()
objRest()
}
remove()
add()
get()
set()
rest()
objRest()
`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["remove","add","get","set","rest","objRest"]});
function a() {
const {
o: { remove },
a: { b: { c: [ add ] }},
d: [{ get }, set, ...rest],
...objRest
} = foo
remove()
add()
get()
set()
rest()
objRest()
}
__vite_ssr_import_0__.remove()
__vite_ssr_import_0__.add()
__vite_ssr_import_0__.get()
__vite_ssr_import_0__.set()
__vite_ssr_import_0__.rest()
__vite_ssr_import_0__.objRest()
"
`)
})
test('object props and methods', async () => {
expect(
await ssrTransformSimpleCode(
`
import foo from 'foo'
const bar = 'bar'
const obj = {
foo() {},
[foo]() {},
[bar]() {},
foo: () => {},
[foo]: () => {},
[bar]: () => {},
bar(foo) {}
}
`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["default"]});
const bar = 'bar'
const obj = {
foo() {},
[__vite_ssr_import_0__.default]() {},
[bar]() {},
foo: () => {},
[__vite_ssr_import_0__.default]: () => {},
[bar]: () => {},
bar(foo) {}
}
"
`)
})
test('class props', async () => {
expect(
await ssrTransformSimpleCode(
`
import { remove, add } from 'vue'
class A {
remove = 1
add = null
}
`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["remove","add"]});
const add = __vite_ssr_import_0__.add;
const remove = __vite_ssr_import_0__.remove;
class A {
remove = 1
add = null
}
"
`)
})
test('class methods', async () => {
expect(
await ssrTransformSimpleCode(
`
import foo from 'foo'
const bar = 'bar'
class A {
foo() {}
[foo]() {}
[bar]() {}
#foo() {}
bar(foo) {}
}
`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["default"]});
const bar = 'bar'
class A {
foo() {}
[__vite_ssr_import_0__.default]() {}
[bar]() {}
#foo() {}
bar(foo) {}
}
"
`)
})
test('declare scope', async () => {
expect(
await ssrTransformSimpleCode(
`
import { aaa, bbb, ccc, ddd } from 'vue'
function foobar() {
ddd()
const aaa = () => {
bbb(ccc)
ddd()
}
const bbb = () => {
console.log('hi')
}
const ccc = 1
function ddd() {}
aaa()
bbb()
ccc()
}
aaa()
bbb()
`,
),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["aaa","bbb","ccc","ddd"]});
function foobar() {
ddd()
const aaa = () => {
bbb(ccc)
ddd()
}
const bbb = () => {
console.log('hi')
}
const ccc = 1
function ddd() {}
aaa()
bbb()
ccc()
}
__vite_ssr_import_0__.aaa()
__vite_ssr_import_0__.bbb()
"
`)
})
test('jsx', async () => {
const code = `
import React from 'react'
import { Foo, Slot } from 'foo'
function Bar({ Slot = <Foo /> }) {
return (
<>
<Slot />
</>
)
}
`
const id = '/foo.jsx'
const result = await transformWithEsbuild(code, id)
expect(await ssrTransformSimpleCode(result.code, '/foo.jsx'))
.toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("react", {"importedNames":["default"]});
const __vite_ssr_import_1__ = await __vite_ssr_import__("foo", {"importedNames":["Foo","Slot"]});
function Bar({ Slot: Slot2 = /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(__vite_ssr_import_1__.Foo, null) }) {
return /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(__vite_ssr_import_0__.default.Fragment, null, /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(Slot2, null));
}
"
`)
})
test('continuous exports', async () => {
expect(
await ssrTransformSimpleCode(
`
export function fn1() {
}export function fn2() {
}
`,
),
).toMatchInlineSnapshot(`
"
function fn1() {
}
Object.defineProperty(__vite_ssr_exports__, "fn1", { enumerable: true, configurable: true, get(){ return fn1 }});function fn2() {
}
Object.defineProperty(__vite_ssr_exports__, "fn2", { enumerable: true, configurable: true, get(){ return fn2 }});
"
`)
})
// https://github.com/vitest-dev/vitest/issues/1141
test('export default expression', async () => {
// esbuild transform result of following TS code
// export default <MyFn> function getRandom() {
// return Math.random()
// }
const code = `
export default (function getRandom() {
return Math.random();
});
`.trim()
expect(await ssrTransformSimpleCode(code)).toMatchInlineSnapshot(`
"__vite_ssr_exports__.default = (function getRandom() {
return Math.random();
});"
`)
expect(
await ssrTransformSimpleCode(`export default (class A {});`),
).toMatchInlineSnapshot(`"__vite_ssr_exports__.default = (class A {});"`)
})
// #8002
test('with hashbang', async () => {
expect(
await ssrTransformSimpleCode(
`#!/usr/bin/env node
console.log("it can parse the hashbang")`,
),
).toMatchInlineSnapshot(`
"#!/usr/bin/env node
console.log("it can parse the hashbang")"
`)
})
test('import hoisted after hashbang', async () => {
expect(
await ssrTransformSimpleCode(
`#!/usr/bin/env node
console.log(foo);
import foo from "foo"`,
),
).toMatchInlineSnapshot(`
"#!/usr/bin/env node
const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["default"]});
console.log(__vite_ssr_import_0__.default);
"
`)
})
// #10289
test('track scope by class, function, condition blocks', async () => {
const code = `
import { foo, bar } from 'foobar'
if (false) {
const foo = 'foo'
console.log(foo)
} else if (false) {
const [bar] = ['bar']
console.log(bar)
} else {
console.log(foo)
console.log(bar)
}
export class Test {
constructor() {
if (false) {
const foo = 'foo'
console.log(foo)
} else if (false) {
const [bar] = ['bar']
console.log(bar)
} else {
console.log(foo)
console.log(bar)
}
}
};`.trim()
expect(await ssrTransformSimpleCode(code)).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("foobar", {"importedNames":["foo","bar"]});
if (false) {
const foo = 'foo'
console.log(foo)
} else if (false) {
const [bar] = ['bar']
console.log(bar)
} else {
console.log(__vite_ssr_import_0__.foo)
console.log(__vite_ssr_import_0__.bar)
}
class Test {
constructor() {
if (false) {
const foo = 'foo'
console.log(foo)
} else if (false) {
const [bar] = ['bar']
console.log(bar)
} else {
console.log(__vite_ssr_import_0__.foo)
console.log(__vite_ssr_import_0__.bar)
}
}
}
Object.defineProperty(__vite_ssr_exports__, "Test", { enumerable: true, configurable: true, get(){ return Test }});;"
`)
})
// #10386
test('track var scope by function', async () => {
expect(
await ssrTransformSimpleCode(`
import { foo, bar } from 'foobar'
function test() {
if (true) {
var foo = () => { var why = 'would' }, bar = 'someone'
}
return [foo, bar]
}`),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("foobar", {"importedNames":["foo","bar"]});
function test() {
if (true) {
var foo = () => { var why = 'would' }, bar = 'someone'
}
return [foo, bar]
}"
`)
})
// #11806
test('track scope by blocks', async () => {
expect(
await ssrTransformSimpleCode(`
import { foo, bar, baz } from 'foobar'
function test() {
[foo];
{
let foo = 10;
let bar = 10;
}
try {} catch (baz){ baz };
return bar;
}`),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("foobar", {"importedNames":["foo","bar","baz"]});
function test() {
[__vite_ssr_import_0__.foo];
{
let foo = 10;
let bar = 10;
}
try {} catch (baz){ baz };
return __vite_ssr_import_0__.bar;
}"
`)
})
test('track scope in for loops', async () => {
expect(
await ssrTransformSimpleCode(`
import { test } from './test.js'
for (const test of tests) {
console.log(test)
}
for (let test = 0; test < 10; test++) {
console.log(test)
}
for (const test in tests) {
console.log(test)
}`),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("./test.js", {"importedNames":["test"]});
for (const test of tests) {
console.log(test)
}
for (let test = 0; test < 10; test++) {
console.log(test)
}
for (const test in tests) {
console.log(test)
}"
`)
})
test('avoid binding ClassExpression', async () => {
const result = await ssrTransformSimple(
`
import Foo, { Bar } from './foo';
console.log(Foo, Bar);
const obj = {
foo: class Foo {},
bar: class Bar {}
}
const Baz = class extends Foo {}
`,
)
expect(result?.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("./foo", {"importedNames":["default","Bar"]});
console.log(__vite_ssr_import_0__.default, __vite_ssr_import_0__.Bar);
const obj = {
foo: class Foo {},
bar: class Bar {}
}
const Baz = class extends __vite_ssr_import_0__.default {}
"
`)
})
test('import assertion attribute', async () => {
expect(
await ssrTransformSimpleCode(`
import * as foo from './foo.json' with { type: 'json' };
import('./bar.json', { with: { type: 'json' } });
`),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("./foo.json");