-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
chore: make if blocks tree-shakable #14549
Conversation
🦋 Changeset detectedLatest commit: d2ecf58 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
preview: https://svelte-dev-git-preview-svelte-14549-svelte.vercel.app/ this is an automated message |
|
@adiguba Can you add a comment to this PR so you can get co-authoring credit? There's no way for me to manually do it. |
I think you can add |
Nice to see this PR 😉 |
I meant to comment on a code suggestion adding a comment to one of the changes sorry. |
.changeset/beige-windows-happen.md
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if it's the good place for the comment.
Otherwise it's not a big deal... Just merge it !
For readability, in cases where we have multiple if blocks... {#if true}
<p>yes</p>
{:else}
<p>no</p>
{/if}
{#if true}
<p>yes</p>
{:else}
<p>no</p>
{/if} ...would it make sense to put everything in a single var node = $.first_child(fragment);
{
var consequent = ($$anchor) => {
var p = root_1();
$.append($$anchor, p);
};
var alternate = ($$anchor) => {
var p_1 = root_2();
$.append($$anchor, p_1);
};
$.if(node, ($$branch) => {
if (true) $$branch(0, consequent); else $$branch(1, alternate);
});
}
var node_1 = $.sibling(node, 2);
{
var consequent_1 = ($$anchor) => {
var p_2 = root_3();
$.append($$anchor, p_2);
};
var alternate_1 = ($$anchor) => {
var p_3 = root_4();
$.append($$anchor, p_3);
};
$.if(node_1, ($$branch) => {
if (true) $$branch(0, consequent_1); else $$branch(1, alternate_1);
});
} A minifier will get rid of the extra |
packages/svelte/src/compiler/phases/3-transform/client/visitors/IfBlock.js
Outdated
Show resolved
Hide resolved
Thanks for the feedback @Rich-Harris, addressed. |
@Rich-Harris The changes introduced in svelte@5.6.2 cause the following problems in one of my code bases when including the I have the following code: {#if !browser}
<Spinner size="10" />
{/if} Starting form svelte@5.6.2 this now causes the following error on the client (no errors on the server): The error only occurs, when trying to negate the browser variable inside of an {#if browser}
<span>Some placeholder text</span>
{:else}
<Spinner size="10" />
{/if} |
@LeonHeidelbach I'm looking into this now. |
We're also experiencing this. In our case, it happens if we put anything in the |
#14589 should fix this issue |
Please open an issue with a self-contained REPL reproduction |
Closes #12833.
Thanks to @adiguba for the idea as shown in #13313. This PR aims to make
{#if}
blocks tree-shakable without any runtime performance regressions.The principle is very similar to #13313, but executed very differently to ensure that the following remain true:
The result of these things, means that this PR compiles this:
In to this:
You'll notice that we now have a simple if block inside the
$.if
that can easily be tree-shaken. Furthermore, unlike #13313, we don't return an array with the path index and a closure, but instead invoke$$render
which does a similar thing. Explained in more detail:$.if
block will be invoked even if the condition hasn't changed – so creating a new closure each time is just utterly wasteful.Another difference to #13313 is that we keep the same heuristics as we have now when it comes to "else if" cases, and nest them on the else case, rather than as flattened else if cases within the same
$.if
block as shown in #13313. Flattening causes tests to fail and also had the side-effect of adding a significant amount of complexity to the codebase.The nice thing about this PR is that very little changes. However, we can revisit this in the future if we decide it's worth it. In many ways, doing these two workloads separately might actually make more sense to happen in different PRs anyway, as it will be easier to review and revert if something goes wrong that isn't covered by tests.