9
9
FOR_ANCHOR_LABEL ,
10
10
IF_ANCHOR_LABEL ,
11
11
SLOT_ANCHOR_LABEL ,
12
+ isString ,
12
13
} from '@vue/shared'
13
14
14
15
const Vue = { ...runtimeDom , ...runtimeVapor }
@@ -17,7 +18,7 @@ function compile(
17
18
sfc : string ,
18
19
data : runtimeDom . Ref < any > ,
19
20
components : Record < string , any > = { } ,
20
- ssr = false ,
21
+ { vapor = true , ssr = false } = { } ,
21
22
) {
22
23
if ( ! sfc . includes ( `<script` ) ) {
23
24
sfc =
@@ -31,7 +32,7 @@ function compile(
31
32
isProd : true ,
32
33
inlineTemplate : true ,
33
34
genDefaultAs : '__sfc__' ,
34
- vapor : true ,
35
+ vapor,
35
36
templateOptions : {
36
37
ssr,
37
38
} ,
@@ -55,26 +56,45 @@ function compile(
55
56
56
57
async function testHydration (
57
58
code : string ,
58
- components : Record < string , string > = { } ,
59
+ components : Record < string , string | { code : string ; vapor : boolean } > = { } ,
59
60
data : any = ref ( 'foo' ) ,
61
+ { interop = false , vapor = true } = { } ,
60
62
) {
61
63
const ssrComponents : any = { }
62
64
const clientComponents : any = { }
63
65
for ( const key in components ) {
64
- clientComponents [ key ] = compile ( components [ key ] , data , clientComponents )
65
- ssrComponents [ key ] = compile ( components [ key ] , data , ssrComponents , true )
66
+ const comp = components [ key ]
67
+ const code = isString ( comp ) ? comp : comp . code
68
+ const isVaporComp = ! isString ( comp ) ? comp . vapor : true
69
+ clientComponents [ key ] = compile ( code , data , clientComponents , {
70
+ vapor : isVaporComp ,
71
+ ssr : false ,
72
+ } )
73
+ ssrComponents [ key ] = compile ( code , data , ssrComponents , {
74
+ vapor : isVaporComp ,
75
+ ssr : true ,
76
+ } )
66
77
}
67
78
68
- const serverComp = compile ( code , data , ssrComponents , true )
79
+ const serverComp = compile ( code , data , ssrComponents , { vapor , ssr : true } )
69
80
const html = await VueServerRenderer . renderToString (
70
81
runtimeDom . createSSRApp ( serverComp ) ,
71
82
)
72
83
const container = document . createElement ( 'div' )
73
84
document . body . appendChild ( container )
74
85
container . innerHTML = html
75
86
76
- const clientComp = compile ( code , data , clientComponents )
77
- const app = createVaporSSRApp ( clientComp )
87
+ const clientComp = compile ( code , data , clientComponents , {
88
+ vapor,
89
+ ssr : false ,
90
+ } )
91
+ let app
92
+ if ( interop ) {
93
+ app = runtimeDom . createSSRApp ( clientComp )
94
+ app . use ( runtimeVapor . vaporInteropPlugin )
95
+ } else {
96
+ app = createVaporSSRApp ( clientComp )
97
+ }
78
98
app . mount ( container )
79
99
return { data, container }
80
100
}
@@ -84,13 +104,13 @@ const triggerEvent = (type: string, el: Element) => {
84
104
el . dispatchEvent ( event )
85
105
}
86
106
87
- describe ( 'Vapor Mode hydration' , ( ) => {
88
- delegateEvents ( 'click' )
107
+ delegateEvents ( 'click' )
89
108
90
- beforeEach ( ( ) => {
91
- document . body . innerHTML = ''
92
- } )
109
+ beforeEach ( ( ) => {
110
+ document . body . innerHTML = ''
111
+ } )
93
112
113
+ describe ( 'Vapor Mode hydration' , ( ) => {
94
114
describe ( 'text' , ( ) => {
95
115
test ( 'root text' , async ( ) => {
96
116
const { data, container } = await testHydration ( `
@@ -3816,3 +3836,93 @@ describe('Vapor Mode hydration', () => {
3816
3836
test . todo ( 'Teleport' )
3817
3837
test . todo ( 'Suspense' )
3818
3838
} )
3839
+
3840
+ describe ( 'VDOM hydration interop' , ( ) => {
3841
+ test ( 'basic component' , async ( ) => {
3842
+ const data = ref ( true )
3843
+ const { container } = await testHydration (
3844
+ `<script setup>const data = _data; const components = _components;</script>
3845
+ <template>
3846
+ <components.VaporChild/>
3847
+ </template>` ,
3848
+ {
3849
+ VaporChild : {
3850
+ code : `<template>{{ data }}</template>` ,
3851
+ vapor : true ,
3852
+ } ,
3853
+ } ,
3854
+ data ,
3855
+ { interop : true , vapor : false } ,
3856
+ )
3857
+
3858
+ expect ( container . innerHTML ) . toMatchInlineSnapshot ( `"true"` )
3859
+
3860
+ data . value = false
3861
+ await nextTick ( )
3862
+ expect ( container . innerHTML ) . toMatchInlineSnapshot ( `"false"` )
3863
+ } )
3864
+
3865
+ test ( 'nested components (VDOM -> Vapor -> VDOM)' , async ( ) => {
3866
+ const data = ref ( true )
3867
+ const { container } = await testHydration (
3868
+ `<script setup>const data = _data; const components = _components;</script>
3869
+ <template>
3870
+ <components.VaporChild/>
3871
+ </template>` ,
3872
+ {
3873
+ VaporChild : {
3874
+ code : `<template><components.VdomChild/></template>` ,
3875
+ vapor : true ,
3876
+ } ,
3877
+ VdomChild : {
3878
+ code : `<script setup>const data = _data;</script>
3879
+ <template>{{ data }}</template>` ,
3880
+ vapor : false ,
3881
+ } ,
3882
+ } ,
3883
+ data ,
3884
+ { interop : true , vapor : false } ,
3885
+ )
3886
+
3887
+ expect ( container . innerHTML ) . toMatchInlineSnapshot ( `"true"` )
3888
+
3889
+ data . value = false
3890
+ await nextTick ( )
3891
+ expect ( container . innerHTML ) . toMatchInlineSnapshot ( `"false"` )
3892
+ } )
3893
+
3894
+ test . todo ( 'slots' , async ( ) => {
3895
+ const data = ref ( true )
3896
+ const { container } = await testHydration (
3897
+ `<script setup>const data = _data; const components = _components;</script>
3898
+ <template>
3899
+ <components.VaporChild>
3900
+ <components.VdomChild/>
3901
+ </components.VaporChild>
3902
+ </template>` ,
3903
+ {
3904
+ VaporChild : {
3905
+ code : `<template><div><slot/></div></template>` ,
3906
+ vapor : true ,
3907
+ } ,
3908
+ VdomChild : {
3909
+ code : `<script setup>const data = _data;</script>
3910
+ <template>{{ data }}</template>` ,
3911
+ vapor : false ,
3912
+ } ,
3913
+ } ,
3914
+ data ,
3915
+ { interop : true , vapor : false } ,
3916
+ )
3917
+
3918
+ expect ( container . innerHTML ) . toMatchInlineSnapshot (
3919
+ `"<div><!--[-->true<!--]--><!--slot--></div>"` ,
3920
+ )
3921
+
3922
+ data . value = false
3923
+ await nextTick ( )
3924
+ expect ( container . innerHTML ) . toMatchInlineSnapshot (
3925
+ `"<div><!--[-->false<!--]--><!--slot--></div>"` ,
3926
+ )
3927
+ } )
3928
+ } )
0 commit comments