Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wise-schools-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: place store setup inside async body
Original file line number Diff line number Diff line change
Expand Up @@ -360,15 +360,27 @@ export function client_component(analysis, options) {

let component_block = b.block([
store_init,
...store_setup,
...legacy_reactive_declarations,
...group_binding_declarations
]);

const should_inject_context =
dev ||
analysis.needs_context ||
analysis.reactive_statements.size > 0 ||
component_returned_object.length > 0;

if (analysis.instance.has_await) {
if (should_inject_context && component_returned_object.length > 0) {
component_block.body.push(b.var('$$exports'));
}
const body = b.block([
...store_setup,
...state.instance_level_snippets,
.../** @type {ESTree.Statement[]} */ (instance.body),
...(should_inject_context && component_returned_object.length > 0
? [b.stmt(b.assignment('=', b.id('$$exports'), b.object(component_returned_object)))]
: []),
b.if(b.call('$.aborted'), b.return()),
.../** @type {ESTree.Statement[]} */ (template.body)
]);
Expand All @@ -379,6 +391,10 @@ export function client_component(analysis, options) {
...state.instance_level_snippets,
.../** @type {ESTree.Statement[]} */ (instance.body)
);
if (should_inject_context && component_returned_object.length > 0) {
component_block.body.push(b.var('$$exports', b.object(component_returned_object)));
}
component_block.body.unshift(...store_setup);

if (!analysis.runes && analysis.needs_context) {
component_block.body.push(b.stmt(b.call('$.init', analysis.immutable ? b.true : undefined)));
Expand All @@ -393,12 +409,6 @@ export function client_component(analysis, options) {
);
}

const should_inject_context =
dev ||
analysis.needs_context ||
analysis.reactive_statements.size > 0 ||
component_returned_object.length > 0;

let should_inject_props =
should_inject_context ||
analysis.needs_props ||
Expand Down Expand Up @@ -445,7 +455,7 @@ export function client_component(analysis, options) {
let to_push;

if (component_returned_object.length > 0) {
let pop_call = b.call('$.pop', b.object(component_returned_object));
let pop_call = b.call('$.pop', b.id('$$exports'));
to_push = needs_store_cleanup ? b.var('$$pop', pop_call) : b.return(pop_call);
} else {
to_push = b.stmt(b.call('$.pop'));
Expand All @@ -456,6 +466,7 @@ export function client_component(analysis, options) {

if (needs_store_cleanup) {
component_block.body.push(b.stmt(b.call('$$cleanup')));

if (component_returned_object.length > 0) {
component_block.body.push(b.return(b.id('$$pop')));
}
Expand Down
Loading