@@ -410,7 +410,11 @@ export default babel => {
410
410
if ( child . id ) {
411
411
registerTemplate ( path , child ) ;
412
412
if (
413
- ! ( child . exprs . length || child . dynamics . length ) &&
413
+ ! (
414
+ child . exprs . length ||
415
+ child . dynamics . length ||
416
+ child . postExprs . length
417
+ ) &&
414
418
child . decl . declarations . length === 1
415
419
)
416
420
return child . decl . declarations [ 0 ] . init ;
@@ -421,7 +425,8 @@ export default babel => {
421
425
t . blockStatement ( [
422
426
child . decl ,
423
427
...child . exprs . concat (
424
- wrapDynamics ( path , child . dynamics ) || [ ]
428
+ wrapDynamics ( path , child . dynamics ) || [ ] ,
429
+ child . postExprs || [ ]
425
430
) ,
426
431
t . returnStatement ( child . id )
427
432
] )
@@ -648,13 +653,15 @@ export default babel => {
648
653
return { exprs, template : "" , component : true } ;
649
654
}
650
655
651
- function transformAttributes ( path , jsx , results ) {
656
+ function transformAttributes ( path , jsx , opts , results ) {
652
657
let elem = results . id ,
658
+ hasHydratableEvent = false ,
653
659
children ;
654
- const hasChildren = jsx . children . length > 0 ;
655
660
const spread = t . identifier ( "_$spread" ) ,
656
661
tagName = getTagName ( jsx ) ,
657
- isSVG = SVGElements . has ( tagName ) ;
662
+ isSVG = SVGElements . has ( tagName ) ,
663
+ hasChildren = jsx . children . length > 0 ;
664
+
658
665
jsx . openingElement . attributes . forEach ( attribute => {
659
666
if ( t . isJSXSpreadAttribute ( attribute ) ) {
660
667
registerImportMethod ( path , "spread" ) ;
@@ -673,6 +680,8 @@ export default babel => {
673
680
] )
674
681
)
675
682
) ;
683
+ //TODO: generate runtime hydratable event check
684
+ hasHydratableEvent = true ;
676
685
return ;
677
686
}
678
687
@@ -713,6 +722,10 @@ export default babel => {
713
722
key !== key . toLowerCase ( ) &&
714
723
! NonComposedEvents . has ( ev )
715
724
) {
725
+ // can only hydrate delegated events
726
+ hasHydratableEvent = opts . hydratableEvents
727
+ ? opts . hydratableEvents . includes ( ev )
728
+ : true ;
716
729
const events =
717
730
path . scope . getProgramParent ( ) . data . events ||
718
731
( path . scope . getProgramParent ( ) . data . events = new Set ( ) ) ;
@@ -804,6 +817,8 @@ export default babel => {
804
817
if ( ! hasChildren && children ) {
805
818
jsx . children . push ( children ) ;
806
819
}
820
+
821
+ results . hasHydratableEvent = results . hasHydratableEvent || hasHydratableEvent ;
807
822
}
808
823
809
824
function transformChildren ( path , jsx , opts , results ) {
@@ -832,6 +847,7 @@ export default babel => {
832
847
results . decl . push ( ...child . decl ) ;
833
848
results . exprs . push ( ...child . exprs ) ;
834
849
results . dynamics . push ( ...child . dynamics ) ;
850
+ results . hasHydratableEvent = results . hasHydratableEvent || child . hasHydratableEvent ;
835
851
tempPath = child . id . name ;
836
852
i ++ ;
837
853
} else if ( child . exprs . length ) {
@@ -924,7 +940,11 @@ export default babel => {
924
940
if ( child . id ) {
925
941
registerTemplate ( path , child ) ;
926
942
if (
927
- ! ( child . exprs . length || child . dynamics . length ) &&
943
+ ! (
944
+ child . exprs . length ||
945
+ child . dynamics . length ||
946
+ child . postExprs . length
947
+ ) &&
928
948
child . decl . declarations . length === 1
929
949
)
930
950
return child . decl . declarations [ 0 ] . init ;
@@ -935,7 +955,8 @@ export default babel => {
935
955
t . blockStatement ( [
936
956
child . decl ,
937
957
...child . exprs . concat (
938
- wrapDynamics ( path , child . dynamics ) || [ ]
958
+ wrapDynamics ( path , child . dynamics ) || [ ] ,
959
+ child . postExprs || [ ]
939
960
) ,
940
961
t . returnStatement ( child . id )
941
962
] )
@@ -961,11 +982,12 @@ export default babel => {
961
982
decl : [ ] ,
962
983
exprs : [ ] ,
963
984
dynamics : [ ] ,
985
+ postExprs : [ ] ,
964
986
isSVG : wrapSVG
965
987
} ;
966
988
if ( wrapSVG ) results . template = "<svg>" + results . template ;
967
989
if ( ! info . skipId ) results . id = path . scope . generateUidIdentifier ( "el$" ) ;
968
- transformAttributes ( path , jsx , results ) ;
990
+ transformAttributes ( path , jsx , opts , results ) ;
969
991
if (
970
992
contextToCustomElements &&
971
993
( tagName === "slot" || tagName . indexOf ( "-" ) > - 1 )
@@ -986,6 +1008,19 @@ export default babel => {
986
1008
transformChildren ( path , jsx , opts , results ) ;
987
1009
results . template += `</${ tagName } >` ;
988
1010
}
1011
+ if ( info . topLevel && generate === "hydrate" && results . hasHydratableEvent ) {
1012
+ registerImportMethod ( path , "runHydrationEvents" ) ;
1013
+ results . postExprs . push (
1014
+ t . expressionStatement (
1015
+ t . callExpression ( t . identifier ( "_$runHydrationEvents" ) , [
1016
+ t . callExpression (
1017
+ t . memberExpression ( results . id , t . identifier ( "getAttribute" ) ) ,
1018
+ [ t . stringLiteral ( "_hk" ) ]
1019
+ )
1020
+ ] )
1021
+ )
1022
+ ) ;
1023
+ }
989
1024
if ( wrapSVG ) results . template += "</svg>" ;
990
1025
return results ;
991
1026
} else if ( t . isJSXFragment ( jsx ) ) {
@@ -1056,7 +1091,11 @@ export default babel => {
1056
1091
if ( result . id ) {
1057
1092
registerTemplate ( path , result ) ;
1058
1093
if (
1059
- ! ( result . exprs . length || result . dynamics . length ) &&
1094
+ ! (
1095
+ result . exprs . length ||
1096
+ result . dynamics . length ||
1097
+ result . postExprs . length
1098
+ ) &&
1060
1099
result . decl . declarations . length === 1
1061
1100
)
1062
1101
path . replaceWith ( result . decl . declarations [ 0 ] . init ) ;
@@ -1065,6 +1104,7 @@ export default babel => {
1065
1104
[ result . decl ] . concat (
1066
1105
result . exprs ,
1067
1106
wrapDynamics ( path , result . dynamics ) || [ ] ,
1107
+ result . postExprs || [ ] ,
1068
1108
t . returnStatement ( result . id )
1069
1109
)
1070
1110
) ;
0 commit comments