Skip to content

Commit 808cc6f

Browse files
authored
feat: shorter each blocks (#10937)
1 parent 8cfea9f commit 808cc6f

File tree

4 files changed

+42
-43
lines changed

4 files changed

+42
-43
lines changed

.changeset/three-lions-visit.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+
feat: more efficient each block compiler output

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

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2314,12 +2314,6 @@ export const template_visitors = {
23142314
// TODO should use context.visit?
23152315
const children = create_block(node, 'each_block', node.body.nodes, context);
23162316

2317-
const else_block = node.fallback
2318-
? b.arrow(
2319-
[b.id('$$anchor')],
2320-
/** @type {import('estree').BlockStatement} */ (context.visit(node.fallback))
2321-
)
2322-
: b.literal(null);
23232317
const key_function = node.key
23242318
? b.arrow(
23252319
[node.context.type === 'Identifier' ? node.context : b.id('$$item'), index],
@@ -2337,40 +2331,46 @@ export const template_visitors = {
23372331
declarations.push(b.let(node.index, index));
23382332
}
23392333

2334+
let callee = '$.each_indexed';
2335+
2336+
/** @type {import('estree').Expression[]} */
2337+
const args = [];
2338+
23402339
if ((each_type & EACH_KEYED) !== 0) {
23412340
if (context.state.options.dev && key_function.type !== 'Literal') {
23422341
context.state.init.push(
23432342
b.stmt(b.call('$.validate_each_keys', b.thunk(collection), key_function))
23442343
);
23452344
}
23462345

2347-
context.state.after_update.push(
2348-
b.stmt(
2349-
b.call(
2350-
'$.each_keyed',
2351-
context.state.node,
2352-
each_node_meta.array_name ? each_node_meta.array_name : b.thunk(collection),
2353-
b.literal(each_type),
2354-
key_function,
2355-
b.arrow([b.id('$$anchor'), item, index], b.block(declarations.concat(children))),
2356-
else_block
2357-
)
2358-
)
2346+
callee = '$.each_keyed';
2347+
2348+
args.push(
2349+
context.state.node,
2350+
each_node_meta.array_name ? each_node_meta.array_name : b.thunk(collection),
2351+
b.literal(each_type),
2352+
key_function,
2353+
b.arrow([b.id('$$anchor'), item, index], b.block(declarations.concat(children)))
23592354
);
23602355
} else {
2361-
context.state.after_update.push(
2362-
b.stmt(
2363-
b.call(
2364-
'$.each_indexed',
2365-
context.state.node,
2366-
each_node_meta.array_name ? each_node_meta.array_name : b.thunk(collection),
2367-
b.literal(each_type),
2368-
b.arrow([b.id('$$anchor'), item, index], b.block(declarations.concat(children))),
2369-
else_block
2370-
)
2356+
args.push(
2357+
context.state.node,
2358+
each_node_meta.array_name ? each_node_meta.array_name : b.thunk(collection),
2359+
b.literal(each_type),
2360+
b.arrow([b.id('$$anchor'), item, index], b.block(declarations.concat(children)))
2361+
);
2362+
}
2363+
2364+
if (node.fallback) {
2365+
args.push(
2366+
b.arrow(
2367+
[b.id('$$anchor')],
2368+
/** @type {import('estree').BlockStatement} */ (context.visit(node.fallback))
23712369
)
23722370
);
23732371
}
2372+
2373+
context.state.after_update.push(b.stmt(b.call(callee, ...args)));
23742374
},
23752375
IfBlock(node, context) {
23762376
context.state.template.push('<!>');

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,10 @@ function each(anchor, get_collection, flags, get_key, render_fn, fallback_fn, re
197197
* @param {number} flags
198198
* @param {null | ((item: V) => string)} get_key
199199
* @param {(anchor: null, item: V, index: import('#client').MaybeSource<number>) => void} render_fn
200-
* @param {null | ((anchor: Node | null) => void)} fallback_fn
200+
* @param {null | ((anchor: Node | null) => void)} [fallback_fn]
201201
* @returns {void}
202202
*/
203-
export function each_keyed(anchor, get_collection, flags, get_key, render_fn, fallback_fn) {
203+
export function each_keyed(anchor, get_collection, flags, get_key, render_fn, fallback_fn = null) {
204204
each(anchor, get_collection, flags, get_key, render_fn, fallback_fn, reconcile_tracked_array);
205205
}
206206

@@ -210,10 +210,10 @@ export function each_keyed(anchor, get_collection, flags, get_key, render_fn, fa
210210
* @param {() => V[]} get_collection
211211
* @param {number} flags
212212
* @param {(anchor: null, item: V, index: import('#client').MaybeSource<number>) => void} render_fn
213-
* @param {null | ((anchor: Node | null) => void)} fallback_fn
213+
* @param {null | ((anchor: Node | null) => void)} [fallback_fn]
214214
* @returns {void}
215215
*/
216-
export function each_indexed(anchor, get_collection, flags, render_fn, fallback_fn) {
216+
export function each_indexed(anchor, get_collection, flags, render_fn, fallback_fn = null) {
217217
each(anchor, get_collection, flags, null, render_fn, fallback_fn, reconcile_indexed_array);
218218
}
219219

packages/svelte/tests/snapshot/samples/each-string-template/_expected/client/index.svelte.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,12 @@ export default function Each_string_template($$anchor, $$props) {
1010
var fragment = $.comment($$anchor);
1111
var node = $.first_child(fragment);
1212

13-
$.each_indexed(
14-
node,
15-
() => ['foo', 'bar', 'baz'],
16-
1,
17-
($$anchor, thing, $$index) => {
18-
var text = $.space_frag($$anchor);
13+
$.each_indexed(node, () => ['foo', 'bar', 'baz'], 1, ($$anchor, thing, $$index) => {
14+
var text = $.space_frag($$anchor);
1915

20-
$.render_effect(() => $.set_text(text, `${$.stringify($.unwrap(thing))}, `));
21-
return $.close($$anchor, text);
22-
},
23-
null
24-
);
16+
$.render_effect(() => $.set_text(text, `${$.stringify($.unwrap(thing))}, `));
17+
return $.close($$anchor, text);
18+
});
2519

2620
$.close_frag($$anchor, fragment);
2721
$.pop();

0 commit comments

Comments
 (0)