@@ -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 ;
@@ -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
/**
@@ -2027,6 +2040,7 @@ export const template_visitors = {
2027
2040
state : {
2028
2041
...context . state ,
2029
2042
node : element_id ,
2043
+ before_init : [ ] ,
2030
2044
init : [ ] ,
2031
2045
update : [ ] ,
2032
2046
after_update : [ ]
0 commit comments