feat: better `<svelte:element>` SSR output (#12339)

pull/12342/head
Rich Harris 4 months ago committed by GitHub
parent a59bc99782
commit bbf489504d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
feat: better `<svelte:element>` SSR output

@ -1338,12 +1338,18 @@ const template_visitors = {
context.visit(node.fragment, state) context.visit(node.fragment, state)
); );
const body = b.stmt( context.state.template.push(
b.call('$.element', b.id('$$payload'), tag, b.thunk(attributes), b.thunk(children)) b.stmt(
b.call(
'$.element',
b.id('$$payload'),
tag,
attributes.body.length > 0 && b.thunk(attributes),
children.body.length > 0 && b.thunk(children)
)
)
); );
context.state.template.push(b.if(tag, body), block_anchor);
if (context.state.options.dev) { if (context.state.options.dev) {
context.state.template.push(b.stmt(b.call('$.pop_element'))); context.state.template.push(b.stmt(b.call('$.pop_element')));
} }

@ -72,18 +72,22 @@ export function assign_payload(p1, p2) {
* @param {() => void} children_fn * @param {() => void} children_fn
* @returns {void} * @returns {void}
*/ */
export function element(payload, tag, attributes_fn, children_fn) { export function element(payload, tag, attributes_fn = noop, children_fn = noop) {
payload.out += `<${tag} `; if (tag) {
attributes_fn(); payload.out += `<${tag} `;
payload.out += `>`; attributes_fn();
payload.out += `>`;
if (!VoidElements.has(tag)) {
children_fn(); if (!VoidElements.has(tag)) {
if (!RawTextElements.includes(tag)) { children_fn();
payload.out += BLOCK_ANCHOR; if (!RawTextElements.includes(tag)) {
payload.out += BLOCK_ANCHOR;
}
payload.out += `</${tag}>`;
} }
payload.out += `</${tag}>`;
} }
payload.out += '<!---->';
} }
/** /**

@ -3,6 +3,5 @@ import * as $ from "svelte/internal/server";
export default function Svelte_element($$payload, $$props) { export default function Svelte_element($$payload, $$props) {
let { tag = 'hr' } = $$props; let { tag = 'hr' } = $$props;
if (tag) $.element($$payload, tag, () => {}, () => {}); $.element($$payload, tag);
$$payload.out += `<!---->`;
} }
Loading…
Cancel
Save