Skip to content

Commit 5836c1c

Browse files
authored
chore: improve output for <svelte:element> (#9648)
- doesn't add spread_dynamic_element_attributes when there are no attributes — Unnecessary spread_dynamic_element_attributes call #9646 - skips the child render function altogether if there is nothing to do
1 parent 6e863e6 commit 5836c1c

File tree

7 files changed

+71
-3
lines changed

7 files changed

+71
-3
lines changed

.changeset/thin-foxes-lick.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: improve `<svelte:element>` generated code

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,15 @@ function serialize_element_spread_attributes(attributes, context, element, eleme
347347
* @returns {boolean}
348348
*/
349349
function serialize_dynamic_element_spread_attributes(attributes, context, element_id) {
350+
if (attributes.length === 0) {
351+
if (context.state.analysis.stylesheet.id) {
352+
context.state.init.push(
353+
b.stmt(b.call('$.class_name', element_id, b.literal(context.state.analysis.stylesheet.id)))
354+
);
355+
}
356+
return false;
357+
}
358+
350359
let is_reactive = false;
351360

352361
/** @type {import('estree').Expression[]} */
@@ -2104,7 +2113,9 @@ export const template_visitors = {
21042113
'$.element',
21052114
context.state.node,
21062115
get_tag,
2107-
b.arrow([element_id, b.id('$$anchor')], b.block(inner)),
2116+
inner.length === 0
2117+
? /** @type {any} */ (undefined)
2118+
: b.arrow([element_id, b.id('$$anchor')], b.block(inner)),
21082119
namespace === 'http://www.w3.org/2000/svg'
21092120
? b.literal(true)
21102121
: /** @type {any} */ (undefined)

packages/svelte/src/internal/client/render.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,7 +1542,7 @@ function swap_block_dom(block, from, to) {
15421542
/**
15431543
* @param {Comment} anchor_node
15441544
* @param {() => string} tag_fn
1545-
* @param {null | ((element: Element, anchor: Node) => void)} render_fn
1545+
* @param {undefined | ((element: Element, anchor: Node) => void)} render_fn
15461546
* @param {any} is_svg
15471547
* @returns {void}
15481548
*/
@@ -1582,7 +1582,7 @@ export function element(anchor_node, tag_fn, render_fn, is_svg = false) {
15821582
block.d = null;
15831583
}
15841584
element = next_element;
1585-
if (element !== null && render_fn !== null) {
1585+
if (element !== null && render_fn !== undefined) {
15861586
let anchor;
15871587
if (current_hydration_fragment !== null) {
15881588
// Use the existing ssr comment as the anchor so that the inner open and close
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { test } from '../../test';
2+
3+
export default test({});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// index.svelte (Svelte VERSION)
2+
// Note: compiler output will change before 5.0 is released!
3+
import "svelte/internal/disclose-version";
4+
import * as $ from "svelte/internal";
5+
6+
export default function Svelte_element($$anchor, $$props) {
7+
$.push($$props, true);
8+
9+
let tag = $.prop_source($$props, "tag", 'hr', false);
10+
/* Init */
11+
var fragment = $.comment($$anchor);
12+
var node = $.child_frag(fragment);
13+
14+
$.element(node, () => $.get(tag));
15+
$.close_frag($$anchor, fragment);
16+
$.pop();
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// index.svelte (Svelte VERSION)
2+
// Note: compiler output will change before 5.0 is released!
3+
import * as $ from "svelte/internal/server";
4+
5+
export default function Svelte_element($$payload, $$props) {
6+
$.push(true);
7+
8+
let { tag = 'hr' } = $$props;
9+
const anchor = $.create_anchor($$payload);
10+
11+
$$payload.out += `${anchor}`;
12+
13+
if (tag) {
14+
const anchor_1 = $.create_anchor($$payload);
15+
16+
$$payload.out += `<${tag}>`;
17+
18+
if (!$.VoidElements.has(tag)) {
19+
$$payload.out += `${anchor_1}`;
20+
$$payload.out += `${anchor_1}</${tag}>`;
21+
}
22+
}
23+
24+
$$payload.out += `${anchor}`;
25+
$.bind_props($$props, { tag });
26+
$.pop();
27+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
let { tag = 'hr' } = $props();
3+
</script>
4+
5+
<svelte:element this={tag} />

0 commit comments

Comments
 (0)