@@ -360,15 +360,27 @@ export function client_component(analysis, options) {
360360
361361 let component_block = b . block ( [
362362 store_init ,
363- ...store_setup ,
364363 ...legacy_reactive_declarations ,
365364 ...group_binding_declarations
366365 ] ) ;
367366
367+ const should_inject_context =
368+ dev ||
369+ analysis . needs_context ||
370+ analysis . reactive_statements . size > 0 ||
371+ component_returned_object . length > 0 ;
372+
368373 if ( analysis . instance . has_await ) {
374+ if ( should_inject_context && component_returned_object . length > 0 ) {
375+ component_block . body . push ( b . var ( '$$exports' ) ) ;
376+ }
369377 const body = b . block ( [
378+ ...store_setup ,
370379 ...state . instance_level_snippets ,
371380 .../** @type {ESTree.Statement[] } */ ( instance . body ) ,
381+ ...( should_inject_context && component_returned_object . length > 0
382+ ? [ b . stmt ( b . assignment ( '=' , b . id ( '$$exports' ) , b . object ( component_returned_object ) ) ) ]
383+ : [ ] ) ,
372384 b . if ( b . call ( '$.aborted' ) , b . return ( ) ) ,
373385 .../** @type {ESTree.Statement[] } */ ( template . body )
374386 ] ) ;
@@ -379,6 +391,10 @@ export function client_component(analysis, options) {
379391 ...state . instance_level_snippets ,
380392 .../** @type {ESTree.Statement[] } */ ( instance . body )
381393 ) ;
394+ if ( should_inject_context && component_returned_object . length > 0 ) {
395+ component_block . body . push ( b . var ( '$$exports' , b . object ( component_returned_object ) ) ) ;
396+ }
397+ component_block . body . unshift ( ...store_setup ) ;
382398
383399 if ( ! analysis . runes && analysis . needs_context ) {
384400 component_block . body . push ( b . stmt ( b . call ( '$.init' , analysis . immutable ? b . true : undefined ) ) ) ;
@@ -393,12 +409,6 @@ export function client_component(analysis, options) {
393409 ) ;
394410 }
395411
396- const should_inject_context =
397- dev ||
398- analysis . needs_context ||
399- analysis . reactive_statements . size > 0 ||
400- component_returned_object . length > 0 ;
401-
402412 let should_inject_props =
403413 should_inject_context ||
404414 analysis . needs_props ||
@@ -445,7 +455,7 @@ export function client_component(analysis, options) {
445455 let to_push ;
446456
447457 if ( component_returned_object . length > 0 ) {
448- let pop_call = b . call ( '$.pop' , b . object ( component_returned_object ) ) ;
458+ let pop_call = b . call ( '$.pop' , b . id ( '$$exports' ) ) ;
449459 to_push = needs_store_cleanup ? b . var ( '$$pop' , pop_call ) : b . return ( pop_call ) ;
450460 } else {
451461 to_push = b . stmt ( b . call ( '$.pop' ) ) ;
@@ -456,6 +466,7 @@ export function client_component(analysis, options) {
456466
457467 if ( needs_store_cleanup ) {
458468 component_block . body . push ( b . stmt ( b . call ( '$$cleanup' ) ) ) ;
469+
459470 if ( component_returned_object . length > 0 ) {
460471 component_block . body . push ( b . return ( b . id ( '$$pop' ) ) ) ;
461472 }
0 commit comments