@@ -1005,6 +1005,7 @@ function create_block(parent, name, nodes, context) {
1005
1005
/** @type {import('../types').ComponentClientTransformState } */
1006
1006
const state = {
1007
1007
...context . state ,
1008
+ before_init : [ ] ,
1008
1009
init : [ ] ,
1009
1010
update : [ ] ,
1010
1011
after_update : [ ] ,
@@ -1050,11 +1051,11 @@ function create_block(parent, name, nodes, context) {
1050
1051
args . push ( b . false ) ;
1051
1052
}
1052
1053
1053
- body . push ( b . var ( id , b . call ( '$.open' , ...args ) ) , ...state . init ) ;
1054
+ body . push ( b . var ( id , b . call ( '$.open' , ...args ) ) , ...state . before_init , ... state . init ) ;
1054
1055
close = b . stmt ( b . call ( '$.close' , b . id ( '$$anchor' ) , id ) ) ;
1055
1056
} else if ( is_single_child_not_needing_template ) {
1056
1057
context . visit ( trimmed [ 0 ] , state ) ;
1057
- body . push ( ...state . init ) ;
1058
+ body . push ( ...state . before_init , ... state . init ) ;
1058
1059
} else if ( trimmed . length > 0 ) {
1059
1060
const id = b . id ( context . state . scope . generate ( 'fragment' ) ) ;
1060
1061
@@ -1071,7 +1072,11 @@ function create_block(parent, name, nodes, context) {
1071
1072
state
1072
1073
} ) ;
1073
1074
1074
- body . push ( b . var ( id , b . call ( '$.space_frag' , b . id ( '$$anchor' ) ) ) , ...state . init ) ;
1075
+ body . push (
1076
+ b . var ( id , b . call ( '$.space_frag' , b . id ( '$$anchor' ) ) ) ,
1077
+ ...state . before_init ,
1078
+ ...state . init
1079
+ ) ;
1075
1080
close = b . stmt ( b . call ( '$.close' , b . id ( '$$anchor' ) , id ) ) ;
1076
1081
} else {
1077
1082
/** @type {(is_text: boolean) => import('estree').Expression } */
@@ -1107,12 +1112,12 @@ function create_block(parent, name, nodes, context) {
1107
1112
body . push ( b . var ( id , b . call ( '$.open_frag' , ...args ) ) ) ;
1108
1113
}
1109
1114
1110
- body . push ( ...state . init ) ;
1115
+ body . push ( ...state . before_init , ... state . init ) ;
1111
1116
1112
1117
close = b . stmt ( b . call ( '$.close_frag' , b . id ( '$$anchor' ) , id ) ) ;
1113
1118
}
1114
1119
} else {
1115
- body . push ( ...state . init ) ;
1120
+ body . push ( ...state . before_init , ... state . init ) ;
1116
1121
}
1117
1122
1118
1123
if ( state . update . length > 0 ) {
@@ -1256,6 +1261,9 @@ function serialize_event_handler(node, { state, visit }) {
1256
1261
function serialize_event ( node , context ) {
1257
1262
const state = context . state ;
1258
1263
1264
+ /** @type {import('estree').Statement } */
1265
+ let statement ;
1266
+
1259
1267
if ( node . expression ) {
1260
1268
let handler = serialize_event_handler ( node , context ) ;
1261
1269
const event_name = node . name ;
@@ -1291,7 +1299,7 @@ function serialize_event(node, context) {
1291
1299
delegated_assignment = handler ;
1292
1300
}
1293
1301
1294
- state . after_update . push (
1302
+ state . init . push (
1295
1303
b . stmt (
1296
1304
b . assignment (
1297
1305
'=' ,
@@ -1323,14 +1331,19 @@ function serialize_event(node, context) {
1323
1331
}
1324
1332
1325
1333
// Events need to run in order with bindings/actions
1326
- state . after_update . push ( b . stmt ( b . call ( '$.event' , ...args ) ) ) ;
1334
+ statement = b . stmt ( b . call ( '$.event' , ...args ) ) ;
1327
1335
} else {
1328
- state . after_update . push (
1329
- b . stmt (
1330
- b . call ( '$.event' , b . literal ( node . name ) , state . node , serialize_event_handler ( node , context ) )
1331
- )
1336
+ statement = b . stmt (
1337
+ b . call ( '$.event' , b . literal ( node . name ) , state . node , serialize_event_handler ( node , context ) )
1332
1338
) ;
1333
1339
}
1340
+
1341
+ const parent = /** @type {import('#compiler').SvelteNode } */ ( context . path . at ( - 1 ) ) ;
1342
+ if ( parent . type === 'SvelteDocument' || parent . type === 'SvelteWindow' ) {
1343
+ state . before_init . push ( statement ) ;
1344
+ } else {
1345
+ state . after_update . push ( statement ) ;
1346
+ }
1334
1347
}
1335
1348
1336
1349
/**
@@ -1720,11 +1733,9 @@ export const template_visitors = {
1720
1733
}
1721
1734
1722
1735
if ( is_reactive ) {
1723
- context . state . after_update . push (
1724
- b . stmt ( b . call ( '$.snippet' , b . thunk ( snippet_function ) , ...args ) )
1725
- ) ;
1736
+ context . state . init . push ( b . stmt ( b . call ( '$.snippet' , b . thunk ( snippet_function ) , ...args ) ) ) ;
1726
1737
} else {
1727
- context . state . after_update . push (
1738
+ context . state . init . push (
1728
1739
b . stmt (
1729
1740
( node . expression . type === 'CallExpression' ? b . call : b . maybe_call ) (
1730
1741
snippet_function ,
@@ -2029,6 +2040,7 @@ export const template_visitors = {
2029
2040
state : {
2030
2041
...context . state ,
2031
2042
node : element_id ,
2043
+ before_init : [ ] ,
2032
2044
init : [ ] ,
2033
2045
update : [ ] ,
2034
2046
after_update : [ ]
@@ -2091,7 +2103,7 @@ export const template_visitors = {
2091
2103
}
2092
2104
} )
2093
2105
) ;
2094
- context . state . after_update . push (
2106
+ context . state . init . push (
2095
2107
b . stmt (
2096
2108
b . call (
2097
2109
'$.element' ,
@@ -2372,7 +2384,7 @@ export const template_visitors = {
2372
2384
) ;
2373
2385
}
2374
2386
2375
- context . state . after_update . push ( b . stmt ( b . call ( callee , ...args ) ) ) ;
2387
+ context . state . init . push ( b . stmt ( b . call ( callee , ...args ) ) ) ;
2376
2388
} ,
2377
2389
IfBlock ( node , context ) {
2378
2390
context . state . template . push ( '<!>' ) ;
@@ -2423,12 +2435,12 @@ export const template_visitors = {
2423
2435
args . push ( b . literal ( true ) ) ;
2424
2436
}
2425
2437
2426
- context . state . after_update . push ( b . stmt ( b . call ( '$.if' , ...args ) ) ) ;
2438
+ context . state . init . push ( b . stmt ( b . call ( '$.if' , ...args ) ) ) ;
2427
2439
} ,
2428
2440
AwaitBlock ( node , context ) {
2429
2441
context . state . template . push ( '<!>' ) ;
2430
2442
2431
- context . state . after_update . push (
2443
+ context . state . init . push (
2432
2444
b . stmt (
2433
2445
b . call (
2434
2446
'$.await' ,
@@ -2470,7 +2482,7 @@ export const template_visitors = {
2470
2482
context . state . template . push ( '<!>' ) ;
2471
2483
const key = /** @type {import('estree').Expression } */ ( context . visit ( node . expression ) ) ;
2472
2484
const body = /** @type {import('estree').Expression } */ ( context . visit ( node . fragment ) ) ;
2473
- context . state . after_update . push (
2485
+ context . state . init . push (
2474
2486
b . stmt ( b . call ( '$.key' , context . state . node , b . thunk ( key ) , b . arrow ( [ b . id ( '$$anchor' ) ] , body ) ) )
2475
2487
) ;
2476
2488
} ,
@@ -2791,7 +2803,7 @@ export const template_visitors = {
2791
2803
if ( binding !== null && binding . kind !== 'normal' ) {
2792
2804
// Handle dynamic references to what seems like static inline components
2793
2805
const component = serialize_inline_component ( node , '$$component' , context ) ;
2794
- context . state . after_update . push (
2806
+ context . state . init . push (
2795
2807
b . stmt (
2796
2808
b . call (
2797
2809
'$.component' ,
@@ -2808,12 +2820,12 @@ export const template_visitors = {
2808
2820
return ;
2809
2821
}
2810
2822
const component = serialize_inline_component ( node , node . name , context ) ;
2811
- context . state . after_update . push ( component ) ;
2823
+ context . state . init . push ( component ) ;
2812
2824
} ,
2813
2825
SvelteSelf ( node , context ) {
2814
2826
context . state . template . push ( '<!>' ) ;
2815
2827
const component = serialize_inline_component ( node , context . state . analysis . name , context ) ;
2816
- context . state . after_update . push ( component ) ;
2828
+ context . state . init . push ( component ) ;
2817
2829
} ,
2818
2830
SvelteComponent ( node , context ) {
2819
2831
context . state . template . push ( '<!>' ) ;
@@ -2822,7 +2834,7 @@ export const template_visitors = {
2822
2834
if ( context . state . options . dev ) {
2823
2835
component = b . stmt ( b . call ( '$.validate_dynamic_component' , b . thunk ( b . block ( [ component ] ) ) ) ) ;
2824
2836
}
2825
- context . state . after_update . push (
2837
+ context . state . init . push (
2826
2838
b . stmt (
2827
2839
b . call (
2828
2840
'$.component' ,
@@ -2974,7 +2986,7 @@ export const template_visitors = {
2974
2986
: b . member ( b . member ( b . id ( '$$props' ) , b . id ( '$$slots' ) ) , name , true , true ) ;
2975
2987
2976
2988
const slot = b . call ( '$.slot' , context . state . node , expression , props_expression , fallback ) ;
2977
- context . state . after_update . push ( b . stmt ( slot ) ) ;
2989
+ context . state . init . push ( b . stmt ( slot ) ) ;
2978
2990
} ,
2979
2991
SvelteHead ( node , context ) {
2980
2992
// TODO attributes?
0 commit comments