Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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/beige-windows-happen.md
Copy link
Contributor

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 !

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

chore: make if blocks tree-shakable
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,35 @@ export function IfBlock(node, context) {
context.state.template.push('<!>');

const consequent = /** @type {BlockStatement} */ (context.visit(node.consequent));
const consequent_id = context.state.scope.generate('consequent');

context.state.init.push(b.var(b.id(consequent_id), b.arrow([b.id('$$anchor')], consequent)));

let alternate_id;

if (node.alternate) {
const alternate = /** @type {BlockStatement} */ (context.visit(node.alternate));
alternate_id = context.state.scope.generate('alternate');
context.state.init.push(b.var(b.id(alternate_id), b.arrow([b.id('$$anchor')], alternate)));
}

/** @type {Expression[]} */
const args = [
context.state.node,
b.thunk(/** @type {Expression} */ (context.visit(node.test))),
b.arrow([b.id('$$anchor')], consequent)
b.arrow(
[b.id('$$branch')],
b.block([
b.if(
/** @type {Expression} */ (context.visit(node.test)),
b.stmt(b.call(b.id('$$branch'), b.literal(0), b.id(consequent_id))),
alternate_id
? b.stmt(b.call(b.id('$$branch'), b.literal(1), b.id(alternate_id)))
: undefined
)
])
)
];

if (node.alternate || node.elseif) {
args.push(
node.alternate
? b.arrow([b.id('$$anchor')], /** @type {BlockStatement} */ (context.visit(node.alternate)))
: b.literal(null)
);
}

if (node.elseif) {
// We treat this...
//
Expand Down
36 changes: 26 additions & 10 deletions packages/svelte/src/internal/client/dom/blocks/if.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ import { HYDRATION_START_ELSE } from '../../../../constants.js';

/**
* @param {TemplateNode} node
* @param {() => boolean} get_condition
* @param {(anchor: Node) => void} consequent_fn
* @param {null | ((anchor: Node) => void)} [alternate_fn]
* @param {(branch: (flag: 0 | 1, fn: (anchor: Node) => void) => void) => void} fn
* @param {boolean} [elseif] True if this is an `{:else if ...}` block rather than an `{#if ...}`, as that affects which transitions are considered 'local'
* @returns {void}
*/
export function if_block(node, get_condition, consequent_fn, alternate_fn = null, elseif = false) {
export function if_block(node, fn, elseif = false) {
if (hydrating) {
hydrate_next();
}
Expand All @@ -37,8 +35,18 @@ export function if_block(node, get_condition, consequent_fn, alternate_fn = null

var flags = elseif ? EFFECT_TRANSPARENT : 0;

block(() => {
if (condition === (condition = !!get_condition())) return;
var has_branch = false;

const set_branch = (/** @type {0 | 1} */ flag, /** @type {(anchor: Node) => void} */ fn) => {
has_branch = true;
update_branch(flag === 0, fn);
};

const update_branch = (
/** @type {boolean | null} */ new_condition,
/** @type {null | ((anchor: Node) => void)} */ fn
) => {
if (condition === (condition = new_condition)) return;

/** Whether or not there was a hydration mismatch. Needs to be a `let` or else it isn't treeshaken out */
let mismatch = false;
Expand All @@ -60,8 +68,8 @@ export function if_block(node, get_condition, consequent_fn, alternate_fn = null
if (condition) {
if (consequent_effect) {
resume_effect(consequent_effect);
} else {
consequent_effect = branch(() => consequent_fn(anchor));
} else if (fn) {
consequent_effect = branch(() => fn(anchor));
}

if (alternate_effect) {
Expand All @@ -72,8 +80,8 @@ export function if_block(node, get_condition, consequent_fn, alternate_fn = null
} else {
if (alternate_effect) {
resume_effect(alternate_effect);
} else if (alternate_fn) {
alternate_effect = branch(() => alternate_fn(anchor));
} else if (fn) {
alternate_effect = branch(() => fn(anchor));
}

if (consequent_effect) {
Expand All @@ -87,6 +95,14 @@ export function if_block(node, get_condition, consequent_fn, alternate_fn = null
// continue in hydration mode
set_hydrating(true);
}
};

block(() => {
has_branch = false;
fn(set_branch);
if (!has_branch) {
update_branch(null, null);
}
}, flags);

if (hydrating) {
Expand Down
Loading