chore: treeshakeable store subs #10506
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In #10503 I removed the
store_sub_existmechanism for omitting store subscriptions for directive names, because it's not a great use of our complexity budget to optimise such a niche case while leaving more common uses (event handlers, effects, etc) unoptimised.This PR proposes a more universal approach — instead of trying to do the static analysis ourselves, we leave it to tools like Rollup. With this PR, a component like this...
...gets turned into this...
export default function App($$payload, $$props) { $.push(true); - const $$store_subs = {}; + var $$store_subs; const thing = writable(); function foo() { bar(); } function bar() { - console.log($.store_get($$store_subs, "$thing", thing)); + console.log($.store_get($$store_subs ??= {}, "$thing", thing)); } $$payload.out += `<button>click me</button>`; - $.unsubscribe_stores($$store_subs); + if ($$store_subs) $.unsubscribe_stores($$store_subs); $.pop(); }...which Rollup turns into this (not sure why it can't treeshake
writable()away, but that's a separate issue):The trade-off is that in the (perhaps more common?) case that a store value contributes to SSR, each
store_getreference gets a??= {}added, and theunsubscribe_stores(...)call now sits inside aif ($$store_subs)block.Before submitting the PR, please make sure you do the following
feat:,fix:,chore:, ordocs:.Tests and linting
pnpm testand lint the project withpnpm lint