Skip to content

Commit a220330

Browse files
trueadmRich-Harris
andauthored
chore: make if blocks tree-shakable (#14549)
* chore: make if blocks dead code eliminable * chore: make if blocks dead code eliminable * chore: make if blocks dead code eliminable * address feedback * address feedback * prettier --------- Co-authored-by: Rich Harris <rich.harris@vercel.com>
1 parent 51c9eac commit a220330

File tree

3 files changed

+65
-21
lines changed

3 files changed

+65
-21
lines changed

.changeset/beige-windows-happen.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
chore: make if blocks tree-shakable

packages/svelte/src/compiler/phases/3-transform/client/visitors/IfBlock.js

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,44 @@ import * as b from '../../../../utils/builders.js';
99
*/
1010
export function IfBlock(node, context) {
1111
context.state.template.push('<!>');
12+
const statements = [];
1213

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

17+
statements.push(b.var(b.id(consequent_id), b.arrow([b.id('$$anchor')], consequent)));
18+
19+
let alternate_id;
20+
21+
if (node.alternate) {
22+
const alternate = /** @type {BlockStatement} */ (context.visit(node.alternate));
23+
alternate_id = context.state.scope.generate('alternate');
24+
statements.push(b.var(b.id(alternate_id), b.arrow([b.id('$$anchor')], alternate)));
25+
}
26+
27+
/** @type {Expression[]} */
1528
const args = [
1629
context.state.node,
17-
b.thunk(/** @type {Expression} */ (context.visit(node.test))),
18-
b.arrow([b.id('$$anchor')], consequent)
30+
b.arrow(
31+
[b.id('$$render')],
32+
b.block([
33+
b.if(
34+
/** @type {Expression} */ (context.visit(node.test)),
35+
b.stmt(b.call(b.id('$$render'), b.id(consequent_id))),
36+
alternate_id
37+
? b.stmt(
38+
b.call(
39+
b.id('$$render'),
40+
b.id(alternate_id),
41+
node.alternate ? b.literal(false) : undefined
42+
)
43+
)
44+
: undefined
45+
)
46+
])
47+
)
1948
];
2049

21-
if (node.alternate || node.elseif) {
22-
args.push(
23-
node.alternate
24-
? b.arrow([b.id('$$anchor')], /** @type {BlockStatement} */ (context.visit(node.alternate)))
25-
: b.literal(null)
26-
);
27-
}
28-
2950
if (node.elseif) {
3051
// We treat this...
3152
//
@@ -51,5 +72,7 @@ export function IfBlock(node, context) {
5172
args.push(b.literal(true));
5273
}
5374

54-
context.state.init.push(b.stmt(b.call('$.if', ...args)));
75+
statements.push(b.stmt(b.call('$.if', ...args)));
76+
77+
context.state.init.push(b.block(statements));
5578
}

packages/svelte/src/internal/client/dom/blocks/if.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ import { HYDRATION_START_ELSE } from '../../../../constants.js';
1313

1414
/**
1515
* @param {TemplateNode} node
16-
* @param {() => boolean} get_condition
17-
* @param {(anchor: Node) => void} consequent_fn
18-
* @param {null | ((anchor: Node) => void)} [alternate_fn]
16+
* @param {(branch: (fn: (anchor: Node) => void, flag?: boolean) => void) => void} fn
1917
* @param {boolean} [elseif] True if this is an `{:else if ...}` block rather than an `{#if ...}`, as that affects which transitions are considered 'local'
2018
* @returns {void}
2119
*/
22-
export function if_block(node, get_condition, consequent_fn, alternate_fn = null, elseif = false) {
20+
export function if_block(node, fn, elseif = false) {
2321
if (hydrating) {
2422
hydrate_next();
2523
}
@@ -37,8 +35,18 @@ export function if_block(node, get_condition, consequent_fn, alternate_fn = null
3735

3836
var flags = elseif ? EFFECT_TRANSPARENT : 0;
3937

40-
block(() => {
41-
if (condition === (condition = !!get_condition())) return;
38+
var has_branch = false;
39+
40+
const set_branch = (/** @type {(anchor: Node) => void} */ fn, flag = true) => {
41+
has_branch = true;
42+
update_branch(flag, fn);
43+
};
44+
45+
const update_branch = (
46+
/** @type {boolean | null} */ new_condition,
47+
/** @type {null | ((anchor: Node) => void)} */ fn
48+
) => {
49+
if (condition === (condition = new_condition)) return;
4250

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

6775
if (alternate_effect) {
@@ -72,8 +80,8 @@ export function if_block(node, get_condition, consequent_fn, alternate_fn = null
7280
} else {
7381
if (alternate_effect) {
7482
resume_effect(alternate_effect);
75-
} else if (alternate_fn) {
76-
alternate_effect = branch(() => alternate_fn(anchor));
83+
} else if (fn) {
84+
alternate_effect = branch(() => fn(anchor));
7785
}
7886

7987
if (consequent_effect) {
@@ -87,6 +95,14 @@ export function if_block(node, get_condition, consequent_fn, alternate_fn = null
8795
// continue in hydration mode
8896
set_hydrating(true);
8997
}
98+
};
99+
100+
block(() => {
101+
has_branch = false;
102+
fn(set_branch);
103+
if (!has_branch) {
104+
update_branch(null, null);
105+
}
90106
}, flags);
91107

92108
if (hydrating) {

0 commit comments

Comments
 (0)