Closed sparecycles closed 3 months ago
This is more so a flaw in the PP's hoisting logic than it is a Svelte 5 specific issue. However, it is interesting how it was working in Svelte 4 with no issues.
Here's the generated PP code for this situation:
<div class="mx-auto w-full max-w-md rounded-md shadow-lg" {...$root}>
{#each items as { id, title, description }, i}
{@const __MELTUI_BUILDER_0__ = $item(itemId)}
{@const itemId = id}
<div
{...__MELTUI_BUILDER_0__} use:__MELTUI_BUILDER_0__ .action
class="overflow-hidden transition-colors first:rounded-t
last:rounded-b focus-within:relative focus-within:z-10 focus-within:ring
focus-within:ring-magnum-400"
>
<!-- ... -->
The PP doesn't take into consideration existing @const
declarations. Will look to fix this soon.
Out of curiosity though, what's the usecase for the current configuration of just redeclaring the id
? Why use an @const
declaration instead of just renaming the value in the destructure? (e.g. {#each items as { id: itemId, title, description }, i}
)
In my project, I was using hasChildren
as a short-hand in a melt-ui TreeView like this,
{#each tree as { title, id: itemId, icon, children }, i}
{@const hasChildren = !!children?.length}
...
<button
...
use:melt={$item({
id: itemId,
hasChildren,
})}
>
I just recreated the problem in an even less compelling form to demonstrate the issue. 😉
Based on the existing PP output, it looks like either svelte4 might either be solving const problems too hard or has its own bug that's cancelling out the melt preprocessor one! 🤔
Anyway, thanks for taking a look.
This is now fixed as of version 0.3.2
!
Describe the bug
@const
-declared values get hoisted above their definitions in latest svelte 5 (svelte@5.0.0-next.123
as of today).A workaround might be to not use melt-pp for builder expressions that involve const values.
Reproduction
See
{@const itemId = id}
usage in here: https://stackblitz.com/edit/github-suxd85?file=src%2Flib%2Fcomponents%2FAccordion.svelteIn case the stackblitz repo is broken work, run
and change
Logs
Generated code in svelte:
System Info
Severity
annoyance
Workaround
Use moar
{@const}
, lol.