@@ -27,6 +27,9 @@ import { regex_starts_with_newline, regex_whitespaces_strict } from '../../patte
27
27
import { DOMBooleanAttributes } from '../../../../constants.js' ;
28
28
import { sanitize_template_string } from '../../../utils/sanitize_template_string.js' ;
29
29
30
+ const block_open = t_string ( '<![>' ) ;
31
+ const block_close = t_string ( '<!]>' ) ;
32
+
30
33
/**
31
34
* @param {string } value
32
35
* @returns {import('./types').TemplateString }
@@ -52,15 +55,6 @@ function t_statement(value) {
52
55
return { type : 'statement' , value } ;
53
56
}
54
57
55
- /**
56
- * @param {import('./types').ServerTransformState } state
57
- * @returns {[import('estree').VariableDeclaration, import('estree').Identifier] }
58
- */
59
- function serialize_anchor ( state ) {
60
- const id = state . scope . root . unique ( 'anchor' ) ;
61
- return [ b . const ( id , b . call ( '$.create_anchor' , b . id ( '$$payload' ) ) ) , id ] ;
62
- }
63
-
64
58
/**
65
59
* @param {import('./types').Template[] } template
66
60
* @param {import('estree').Identifier } out
@@ -1237,12 +1231,10 @@ const template_visitors = {
1237
1231
} ,
1238
1232
HtmlTag ( node , context ) {
1239
1233
const state = context . state ;
1240
- const [ dec , id ] = serialize_anchor ( state ) ;
1241
- state . init . push ( dec ) ;
1242
- state . template . push ( t_expression ( id ) ) ;
1234
+ state . template . push ( block_open ) ;
1243
1235
const raw = /** @type {import('estree').Expression } */ ( context . visit ( node . expression ) ) ;
1244
1236
context . state . template . push ( t_expression ( raw ) ) ;
1245
- state . template . push ( t_expression ( id ) ) ;
1237
+ state . template . push ( block_close ) ;
1246
1238
} ,
1247
1239
ConstTag ( node , { state, visit } ) {
1248
1240
const declaration = node . declaration . declarations [ 0 ] ;
@@ -1273,10 +1265,8 @@ const template_visitors = {
1273
1265
} ,
1274
1266
RenderTag ( node , context ) {
1275
1267
const state = context . state ;
1276
- const [ anchor , anchor_id ] = serialize_anchor ( state ) ;
1277
1268
1278
- state . init . push ( anchor ) ;
1279
- state . template . push ( t_expression ( anchor_id ) ) ;
1269
+ state . template . push ( block_open ) ;
1280
1270
1281
1271
const callee = unwrap_optional ( node . expression ) . callee ;
1282
1272
const raw_args = unwrap_optional ( node . expression ) . arguments ;
@@ -1302,7 +1292,7 @@ const template_visitors = {
1302
1292
)
1303
1293
) ;
1304
1294
1305
- state . template . push ( t_expression ( anchor_id ) ) ;
1295
+ state . template . push ( block_close ) ;
1306
1296
} ,
1307
1297
ClassDirective ( node ) {
1308
1298
error ( node , 'INTERNAL' , 'Node should have been handled elsewhere' ) ;
@@ -1427,9 +1417,7 @@ const template_visitors = {
1427
1417
}
1428
1418
} ;
1429
1419
1430
- const [ el_anchor , anchor_id ] = serialize_anchor ( context . state ) ;
1431
- context . state . init . push ( el_anchor ) ;
1432
- context . state . template . push ( t_expression ( anchor_id ) ) ;
1420
+ context . state . template . push ( block_open ) ;
1433
1421
1434
1422
const main = create_block ( node , node . fragment . nodes , {
1435
1423
...context ,
@@ -1465,17 +1453,15 @@ const template_visitors = {
1465
1453
)
1466
1454
)
1467
1455
) ,
1468
- t_expression ( anchor_id )
1456
+ block_close
1469
1457
) ;
1470
1458
if ( context . state . options . dev ) {
1471
1459
context . state . template . push ( t_statement ( b . stmt ( b . call ( '$.pop_element' ) ) ) ) ;
1472
1460
}
1473
1461
} ,
1474
1462
EachBlock ( node , context ) {
1475
1463
const state = context . state ;
1476
- const [ dec , id ] = serialize_anchor ( state ) ;
1477
- state . init . push ( dec ) ;
1478
- state . template . push ( t_expression ( id ) ) ;
1464
+ state . template . push ( block_open ) ;
1479
1465
1480
1466
const each_node_meta = node . metadata ;
1481
1467
const collection = /** @type {import('estree').Expression } */ ( context . visit ( node . expression ) ) ;
@@ -1486,14 +1472,6 @@ const template_visitors = {
1486
1472
: b . id ( node . index ) ;
1487
1473
const children = node . body . nodes ;
1488
1474
1489
- const [ each_dec , each_id ] = serialize_anchor ( state ) ;
1490
-
1491
- /** @type {import('./types').Anchor } */
1492
- const anchor = {
1493
- type : 'Anchor' ,
1494
- id : each_id
1495
- } ;
1496
-
1497
1475
const array_id = state . scope . root . unique ( 'each_array' ) ;
1498
1476
state . init . push ( b . const ( array_id , b . call ( '$.ensure_array_like' , collection ) ) ) ;
1499
1477
@@ -1507,11 +1485,14 @@ const template_visitors = {
1507
1485
each . push ( b . let ( node . index , index ) ) ;
1508
1486
}
1509
1487
1488
+ each . push ( b . stmt ( b . assignment ( '+=' , b . id ( '$$payload.out' ) , b . literal ( block_open . value ) ) ) ) ;
1489
+
1510
1490
each . push (
1511
- each_dec ,
1512
- .../** @type {import('estree').Statement[] } */ ( create_block ( node , children , context , anchor ) )
1491
+ .../** @type {import('estree').Statement[] } */ ( create_block ( node , children , context ) )
1513
1492
) ;
1514
1493
1494
+ each . push ( b . stmt ( b . assignment ( '+=' , b . id ( '$$payload.out' ) , b . literal ( block_close . value ) ) ) ) ;
1495
+
1515
1496
const for_loop = b . for (
1516
1497
b . let ( index , b . literal ( 0 ) ) ,
1517
1498
b . binary ( '<' , index , b . member ( array_id , b . id ( 'length' ) ) ) ,
@@ -1535,13 +1516,11 @@ const template_visitors = {
1535
1516
} else {
1536
1517
state . template . push ( t_statement ( for_loop ) ) ;
1537
1518
}
1538
- state . template . push ( t_expression ( id ) ) ;
1519
+ state . template . push ( block_close ) ;
1539
1520
} ,
1540
1521
IfBlock ( node , context ) {
1541
1522
const state = context . state ;
1542
- const [ dec , id ] = serialize_anchor ( state ) ;
1543
- state . init . push ( dec ) ;
1544
- state . template . push ( t_expression ( id ) ) ;
1523
+ state . template . push ( block_open ) ;
1545
1524
1546
1525
// Insert ssr:if:true/false anchors in addition to the other anchors so that
1547
1526
// the if block can catch hydration mismatches (false on the server, true on the client and vice versa)
@@ -1568,13 +1547,11 @@ const template_visitors = {
1568
1547
)
1569
1548
)
1570
1549
) ;
1571
- state . template . push ( t_expression ( id ) ) ;
1550
+ state . template . push ( block_close ) ;
1572
1551
} ,
1573
1552
AwaitBlock ( node , context ) {
1574
1553
const state = context . state ;
1575
- const [ dec , id ] = serialize_anchor ( state ) ;
1576
- state . init . push ( dec ) ;
1577
- state . template . push ( t_expression ( id ) ) ;
1554
+ state . template . push ( block_open ) ;
1578
1555
1579
1556
state . template . push (
1580
1557
t_statement (
@@ -1608,16 +1585,14 @@ const template_visitors = {
1608
1585
)
1609
1586
) ;
1610
1587
1611
- state . template . push ( t_expression ( id ) ) ;
1588
+ state . template . push ( block_close ) ;
1612
1589
} ,
1613
1590
KeyBlock ( node , context ) {
1614
1591
const state = context . state ;
1615
- const [ dec , id ] = serialize_anchor ( state ) ;
1616
- state . init . push ( dec ) ;
1617
- state . template . push ( t_expression ( id ) ) ;
1592
+ state . template . push ( block_open ) ;
1618
1593
const body = create_block ( node , node . fragment . nodes , context ) ;
1619
1594
state . template . push ( t_statement ( b . block ( body ) ) ) ;
1620
- state . template . push ( t_expression ( id ) ) ;
1595
+ state . template . push ( block_close ) ;
1621
1596
} ,
1622
1597
SnippetBlock ( node , context ) {
1623
1598
// TODO hoist where possible
@@ -1635,34 +1610,28 @@ const template_visitors = {
1635
1610
} ,
1636
1611
Component ( node , context ) {
1637
1612
const state = context . state ;
1638
- const [ dec , id ] = serialize_anchor ( state ) ;
1639
- state . init . push ( dec ) ;
1640
- state . template . push ( t_expression ( id ) ) ;
1613
+ state . template . push ( block_open ) ;
1641
1614
const call = serialize_inline_component ( node , node . name , context ) ;
1642
1615
state . template . push ( t_statement ( call ) ) ;
1643
- state . template . push ( t_expression ( id ) ) ;
1616
+ state . template . push ( block_close ) ;
1644
1617
} ,
1645
1618
SvelteSelf ( node , context ) {
1646
1619
const state = context . state ;
1647
- const [ dec , id ] = serialize_anchor ( state ) ;
1648
- state . init . push ( dec ) ;
1649
- state . template . push ( t_expression ( id ) ) ;
1620
+ state . template . push ( block_open ) ;
1650
1621
const call = serialize_inline_component ( node , context . state . analysis . name , context ) ;
1651
1622
state . template . push ( t_statement ( call ) ) ;
1652
- state . template . push ( t_expression ( id ) ) ;
1623
+ state . template . push ( block_close ) ;
1653
1624
} ,
1654
1625
SvelteComponent ( node , context ) {
1655
1626
const state = context . state ;
1656
- const [ dec , id ] = serialize_anchor ( state ) ;
1657
- state . init . push ( dec ) ;
1658
- state . template . push ( t_expression ( id ) ) ;
1627
+ state . template . push ( block_open ) ;
1659
1628
const call = serialize_inline_component (
1660
1629
node ,
1661
1630
/** @type {import('estree').Expression } */ ( context . visit ( node . expression ) ) ,
1662
1631
context
1663
1632
) ;
1664
1633
state . template . push ( t_statement ( call ) ) ;
1665
- state . template . push ( t_expression ( id ) ) ;
1634
+ state . template . push ( block_close ) ;
1666
1635
} ,
1667
1636
LetDirective ( node , { state } ) {
1668
1637
if ( node . expression && node . expression . type !== 'Identifier' ) {
@@ -1745,9 +1714,7 @@ const template_visitors = {
1745
1714
} ,
1746
1715
SlotElement ( node , context ) {
1747
1716
const state = context . state ;
1748
- const [ dec , id ] = serialize_anchor ( state ) ;
1749
- state . init . push ( dec ) ;
1750
- state . template . push ( t_expression ( id ) ) ;
1717
+ state . template . push ( block_open ) ;
1751
1718
1752
1719
/** @type {import('estree').Property[] } */
1753
1720
const props = [ ] ;
@@ -1794,7 +1761,7 @@ const template_visitors = {
1794
1761
const slot = b . call ( '$.slot' , b . id ( '$$payload' ) , expression , props_expression , fallback ) ;
1795
1762
1796
1763
state . template . push ( t_statement ( b . stmt ( slot ) ) ) ;
1797
- state . template . push ( t_expression ( id ) ) ;
1764
+ state . template . push ( block_close ) ;
1798
1765
} ,
1799
1766
SvelteHead ( node , context ) {
1800
1767
const state = context . state ;
0 commit comments