Migrate from Svelte · @pyreon/svelte-compat
Keep your stores. Move components to TSX.
@pyreon/svelte-compat runs the Svelte APIs your code imports, svelte/store plus lifecycle, context and events, on Pyreon's engine. It is not a .svelte compiler: components become TSX, and the store modules they share keep their imports.
npm install @pyreon/svelte-compat @pyreon/core @pyreon/reactivity @pyreon/runtime-dom
npm install -D @pyreon/vite-plugin before and after
The same Svelte code, on Pyreon.
coverage
What @pyreon/svelte-compat covers, and what it does not.
covers
svelte/store:writable,readable,derived,readonly,get, with Svelte's lazy start and stop and its equality check.svelte:onMount,onDestroy,beforeUpdate,afterUpdate,tick,setContext,getContext,hasContext,getAllContexts,createEventDispatcher,mount,unmount,flushSync.- Control-flow components re-exported for JSX:
<For>,<Show>,<Switch>,<Match>,<ErrorBoundary>,<Suspense>.
does not cover
- The
.sveltecompiler: single-file components, rune syntax ($state,$derived,$effect),$:statements and$storeauto-subscription. <svelte:component>and<svelte:element>.onDestroy(and anonMountcleanup) also fires when a parent re-renders, not only on real unmount.beforeUpdate/afterUpdaterun after the first render only, not on every update.getAllContexts()returns an emptyMap;createEventDispatcherreaches the direct parent only, with no bubbling.- The
create-zeroscaffold has no Svelte option; set the plugin up by hand.
setup
Install it, point the config at it.
npm install @pyreon/svelte-compat @pyreon/core @pyreon/reactivity @pyreon/runtime-dom
npm install -D @pyreon/vite-plugin vite.config.ts
// vite.config.ts
import { defineConfig } from 'vite'
import pyreon from '@pyreon/vite-plugin'
export default defineConfig({
plugins: [pyreon({ compat: 'svelte' })],
})Aliases svelte, svelte/store, svelte/internal and the JSX runtime.
tsconfig.json
// tsconfig.json
{
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "@pyreon/svelte-compat"
}
}Types and the JSX runtime come from the compat package.
port it
What stays: your stores
Svelte store module · stores/cart.ts
// stores/cart.ts: unchanged. `svelte/store` resolves to @pyreon/svelte-compat/store
import { derived, writable } from 'svelte/store'
export const items = writable<string[]>([])
export const count = derived(items, ($items: string[]) => $items.length)Same file on svelte-compat · stores/cart.ts
// stores/cart.ts: unchanged. `svelte/store` resolves to @pyreon/svelte-compat/store
import { derived, writable } from 'svelte/store'
export const items = writable<string[]>([])
export const count = derived(items, ($items: string[]) => $items.length)No edits: the alias does the work, and the store contract (subscribe, set, update) behaves like Svelte's.
port it
What moves: components
Svelte 5 · Counter.svelte
<script lang="ts">
let { step }: { step: number } = $props()
let count = $state(0)
const doubled = $derived(count * 2)
</script>
<button class="counter" onclick={() => (count += step)}>{count} / {doubled}</button>Pyreon · Counter.tsx
import { computed, signal } from '@pyreon/reactivity'
export function Counter(props: { step: number }) {
const count = signal(0)
const doubled = computed(() => count() * 2)
return (
<button class="counter" onClick={() => count.update((c) => c + props.step)}>
{count()} / {doubled()}
</button>
)
}Prefer writing count++? Plain Mode ('use plain') keeps that style and compiles it to signals.
read more