2024-07-18 08:32:39 -05:00
|
|
|
import { createRawSnippet } from 'svelte';
|
2024-02-27 06:15:27 -05:00
|
|
|
import { render } from 'svelte/server';
|
2023-12-07 09:47:04 -05:00
|
|
|
|
2023-11-15 10:40:23 -05:00
|
|
|
function check(Component) {
|
|
|
|
// Svelte 5 generated components always accept these two props
|
|
|
|
const str = Component.toString();
|
|
|
|
return str.includes('$$payload') && str.includes('$$props');
|
|
|
|
}
|
|
|
|
|
|
|
|
function needsHydration(metadata) {
|
|
|
|
// Adjust how this is hydrated only when the version of Astro supports `astroStaticSlot`
|
|
|
|
return metadata.astroStaticSlot ? !!metadata.hydrate : true;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function renderToStaticMarkup(Component, props, slotted, metadata) {
|
|
|
|
const tagName = needsHydration(metadata) ? 'astro-slot' : 'astro-static-slot';
|
|
|
|
|
|
|
|
let children = undefined;
|
|
|
|
let $$slots = undefined;
|
|
|
|
for (const [key, value] of Object.entries(slotted)) {
|
2024-07-18 08:32:39 -05:00
|
|
|
$$slots ??= {};
|
2023-11-15 10:40:23 -05:00
|
|
|
if (key === 'default') {
|
2024-07-18 08:32:39 -05:00
|
|
|
$$slots.default = true;
|
|
|
|
children = createRawSnippet(() => ({
|
|
|
|
render: () => `<${tagName}>${value}</${tagName}>`,
|
|
|
|
}));
|
2023-11-15 10:40:23 -05:00
|
|
|
} else {
|
2024-07-18 08:32:39 -05:00
|
|
|
$$slots[key] = createRawSnippet(() => ({
|
|
|
|
render: () => `<${tagName} name="${key}">${value}</${tagName}>`,
|
|
|
|
}));
|
2023-11-15 10:40:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-18 08:32:39 -05:00
|
|
|
const result = render(Component, {
|
2023-11-15 10:40:23 -05:00
|
|
|
props: {
|
|
|
|
...props,
|
|
|
|
children,
|
|
|
|
$$slots,
|
|
|
|
},
|
|
|
|
});
|
2024-07-18 08:32:39 -05:00
|
|
|
return { html: result.body };
|
2023-11-15 10:40:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
check,
|
|
|
|
renderToStaticMarkup,
|
|
|
|
supportsAstroStaticSlot: true,
|
|
|
|
};
|