-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Svelte 5 (experimental) (#9098)
Co-authored-by: Nate Moore <[email protected]>
- Loading branch information
1 parent
e63aac9
commit a600c14
Showing
7 changed files
with
120 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@astrojs/svelte': patch | ||
--- | ||
|
||
Adds experimental support for Svelte 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { mount } from 'svelte'; | ||
|
||
export default (element) => { | ||
return async (Component, props, slotted) => { | ||
if (!element.hasAttribute('ssr')) return; | ||
|
||
let children = undefined; | ||
let $$slots = undefined; | ||
for (const [key, value] of Object.entries(slotted)) { | ||
if (key === 'default') { | ||
children = createSlotDefinition(key, value); | ||
} else { | ||
$$slots ??= {}; | ||
$$slots[key] = createSlotDefinition(key, value); | ||
} | ||
} | ||
|
||
const [, destroy] = mount(Component, { | ||
target: element, | ||
props: { | ||
...props, | ||
children, | ||
$$slots, | ||
}, | ||
}); | ||
|
||
element.addEventListener('astro:unmount', () => destroy(), { once: true }); | ||
}; | ||
}; | ||
|
||
function createSlotDefinition(key, children) { | ||
/** | ||
* @param {Comment} $$anchor A comment node for slots in Svelte 5 | ||
*/ | ||
return ($$anchor, _$$slotProps) => { | ||
const parent = $$anchor.parentNode; | ||
const el = document.createElement('div'); | ||
el.innerHTML = `<astro-slot${ | ||
key === 'default' ? '' : ` name="${key}"` | ||
}>${children}</astro-slot>`; | ||
parent.insertBefore(el.children[0], $$anchor); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { render } from 'svelte/server'; | ||
|
||
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)) { | ||
if (key === 'default') { | ||
children = () => `<${tagName}>${value}</${tagName}>`; | ||
} else { | ||
$$slots ??= {}; | ||
$$slots[key] = () => `<${tagName} name="${key}">${value}</${tagName}>`; | ||
} | ||
} | ||
|
||
const { html } = render(Component, { | ||
props: { | ||
...props, | ||
children, | ||
$$slots, | ||
}, | ||
}); | ||
return { html }; | ||
} | ||
|
||
export default { | ||
check, | ||
renderToStaticMarkup, | ||
supportsAstroStaticSlot: true, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.