Skip to main content
The migrate() function performs a best-effort migration of Svelte 4 code to Svelte 5, transforming legacy syntax to use runes, event attributes, and render tags.
The migration is not perfect and may require manual adjustments. Always review the migrated code and test thoroughly. The migrator may add @migration-task comments where manual intervention is needed.

Signature

Parameters

source
string
required
The Svelte 4 component source code to migrate
options.filename
string
The filename of the component (used for debugging and self-referencing components)
options.use_ts
boolean
Whether to generate TypeScript syntax. If not specified, the migrator will infer from the source code.

Return Value

code
string
The migrated Svelte 5 code. May include @migration-task comments for manual review.

What Gets Migrated

The migrator handles:
  • Reactive declarations ($:) → $derived and $effect
  • Exported props (export let) → $props() destructuring
  • Reactive statements$effect.pre() or run()
  • Event handlers (on:click) → event attributes (onclick)
  • Slots → Snippets ({@render children()})
  • <svelte:component> with dynamic this{@const} or $derived
  • <svelte:self> → Self-import
  • CSS selector scoping:global() where needed
  • beforeUpdate/afterUpdate imports → Removed if unused

Usage Examples

Basic Migration

Output:

Migrating Props with TypeScript

Output:

Migrating Slots to Snippets

Output:

Migrating Event Handlers

Output:

Programmatic Migration

Handling Migration Tasks

When the migrator can’t automatically handle certain patterns, it adds comments:
Search for @migration-task in the migrated code and address these manually.

Using with CLI

Svelte provides a CLI migration tool:

Limitations

  • CSS preprocessors: CSS is blanked during migration. Use a preprocessor-aware tool if needed.
  • Complex reactive patterns: Some complex $: patterns may not migrate cleanly.
  • beforeUpdate/afterUpdate: Must be migrated manually to runes or lifecycle functions.
  • Store auto-subscription: $store syntax is not migrated (still works in Svelte 5).
  • Component events: Must be migrated to callback props manually.

Best Practices

  1. Commit before migrating - Always have a clean git state
  2. Review the output - Don’t blindly accept migrations
  3. Test thoroughly - Run your test suite after migration
  4. Migrate incrementally - Start with leaf components
  5. Read migration tasks - Address all @migration-task comments