Skip to content

Svelte 5: only inject push/init/pop when necessary #11297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Rich-Harris opened this issue Apr 23, 2024 · 0 comments · Fixed by #11319
Closed

Svelte 5: only inject push/init/pop when necessary #11297

Rich-Harris opened this issue Apr 23, 2024 · 0 comments · Fixed by #11319

Comments

@Rich-Harris
Copy link
Member

Describe the problem

The Svelte 5 <h1>hello world</h1> component looks like this:

var root = $.template(`<h1>hello world</h1>`);

function App($$anchor, $$props) {
  $.push($$props, false);
  $.init();

  var h1 = root();

  $.append($$anchor, h1);
  $.pop();
}

The push and pop stuff is required to maintain the context stack for various reasons, and init is required if lifecycle functions (afterUpdate, beforeUpdate, onMount) are used.

Describe the proposed solution

A lot of the stuff on the component context object pertains to non-runes mode, and we could simplify it in runes mode. But we could also get rid of it altogether in a lot of cases. E.g. in runes mode, I'm pretty sure we can get rid of it if there are no exports and we can guarantee that we're not (indirectly) calling setContext. (The context object includes space for effects, but it's actually fine if they get attached to the parent context.)

This basically means that if a component contains a call/member expression that we can't guarantee is 'safe', we add the push/pop, but if not then we leave it out. Ideally the output above would look like this (note that the $$props parameter has also been removed):

var root = $.template(`<h1>hello world</h1>`);

function App($$anchor) {
  $.append($$anchor, root());
}

Importance

nice to have

Rich-Harris added a commit that referenced this issue Apr 25, 2024
* feat: only inject push/init/pop when necessary - closes #11297

* regenerate

* differentiate between safe/unsafe

* only inject $$props when necessary

* more

* fix

* simplify

* handle store subscriptions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant