0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 09:21:21 +01:00

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

This commit is contained in:
Rich Harris 2024-07-07 20:02:11 -07:00 committed by GitHub
parent a59bc99782
commit bbf489504d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 29 additions and 15 deletions

View File

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

View File

@ -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')));
} }

View File

@ -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)) { if (!VoidElements.has(tag)) {
children_fn(); children_fn();
if (!RawTextElements.includes(tag)) { if (!RawTextElements.includes(tag)) {
payload.out += BLOCK_ANCHOR; payload.out += BLOCK_ANCHOR;
}
payload.out += `</${tag}>`;
} }
payload.out += `</${tag}>`;
} }
payload.out += '<!---->';
} }
/** /**

View File

@ -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 += `<!---->`;
} }