chore: treeshakeable store subs (#10506)

* make store subscriptions treeshakeable on the server

* drive-by fix

* changeset

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
pull/10509/head
Rich Harris 2 years ago committed by GitHub
parent df10204fcc
commit 40ac2cafd6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
chore: treeshake unused store subscriptions in SSR mode

@ -322,7 +322,7 @@ function serialize_get_binding(node, state) {
const store_id = b.id(node.name.slice(1));
return b.call(
'$.store_get',
b.id('$$store_subs'),
b.assignment('??=', b.id('$$store_subs'), b.object([])),
b.literal(node.name),
serialize_get_binding(store_id, state)
);
@ -460,7 +460,7 @@ function serialize_set_binding(node, context, fallback) {
} else if (is_store) {
return b.call(
'$.mutate_store',
b.id('$$store_subs'),
b.assignment('??=', b.id('$$store_subs'), b.object([])),
b.literal(left.name),
b.id(left_name),
b.assignment(node.operator, /** @type {import('estree').Pattern} */ (visit(node.left)), value)
@ -506,7 +506,11 @@ const global_visitors = {
if (node.prefix) fn += '_pre';
/** @type {import('estree').Expression[]} */
const args = [b.id('$$store_subs'), b.literal(argument.name), b.id(argument.name.slice(1))];
const args = [
b.assignment('??=', b.id('$$store_subs'), b.object([])),
b.literal(argument.name),
b.id(argument.name.slice(1))
];
if (node.operator === '--') {
args.push(b.literal(-1));
}
@ -2094,8 +2098,10 @@ export function server_component(analysis, options) {
(binding) => binding.kind === 'store_sub'
)
) {
instance.body.unshift(b.const('$$store_subs', b.object([])));
template.body.push(b.stmt(b.call('$.unsubscribe_stores', b.id('$$store_subs'))));
instance.body.unshift(b.var('$$store_subs'));
template.body.push(
b.if(b.id('$$store_subs'), b.stmt(b.call('$.unsubscribe_stores', b.id('$$store_subs'))))
);
}
// Propagate values of bound props upwards if they're undefined in the parent and have a value.
// Don't do this as part of the props retrieval because people could eagerly mutate the prop in the instance script.

Loading…
Cancel
Save