svelte-5-vs-svelte-4-cursorrules-prompt-file — Cursor rule
Guides Cursor to write Svelte 5 code instead of Svelte 4, adopting runes like $state and $derived, native event attributes, and snippets.
.cursor/rules/svelte-5-vs-svelte-4-cursorrules-prompt-file.mdcWhat svelte-5-vs-svelte-4-cursorrules-prompt-file does
This instruction set establishes clear boundaries for Cursor when modernizing legacy Svelte 4 code. The assistant stops producing $: declarations and on:click directives, switching to explicit $state, $derived, and $effect runes alongside standard onclick attributes.
The prompt targets a frequent failure point in AI generation: compiler runes must never be imported. Adding import statements for $state, $derived, or $props breaks the build. It also blocks outdated syntax like |preventDefault, requiring plain calls to e.preventDefault() inside regular callback functions.
With globs set to **/* and alwaysApply: false, the rule triggers selectively in your workspace whenever Svelte files and component logic require updates.
What it helps with
- Migrate reactive
$:declarations to$derivedand$effectrunes - Replace legacy
on:clickdirectives with standardonclickattributes - Eliminate event modifiers like
|preventDefaultin component markup - Prevent invalid import statements for native compiler runes
Example prompts for the agent
- Convert this Svelte 4 counter component to Svelte 5 using runes
- Add a click handler to the button without using on:click directives
- Define a computed property for the total sum using $derived
How to install svelte-5-vs-svelte-4-cursorrules-prompt-file
Manually: put the files in place
.cursor/rules/svelte-5-vs-svelte-4-cursorrules-prompt-file.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.claude/rules/svelte-5-vs-svelte-4-cursorrules-prompt-file.mdThe paths frontmatter field limits the files: move globs from the source into paths
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.github/instructions/svelte-5-vs-svelte-4-cursorrules-prompt-file.instructions.mdThe applyTo field sets which files it applies to: move globs from the source into applyTo
Manually: put the files in place
.devin/rules/svelte-5-vs-svelte-4-cursorrules-prompt-file.mdManually: put the files in place
.clinerules/svelte-5-vs-svelte-4-cursorrules-prompt-file.mdStep by step, with commands for each platform: Cursor rules: .mdc files, four modes and what replaced .cursorrules
Questions about svelte-5-vs-svelte-4-cursorrules-prompt-file
How to install svelte-5-vs-svelte-4-cursorrules-prompt-file in Cursor?
Add the file .cursor/rules/svelte-5-vs-svelte-4-cursorrules-prompt-file.mdc to your project and paste the rule contents into it.
What does this rule do?
It guides Cursor to produce modern Svelte 5 component syntax, enforcing runes for reactive state and native HTML event attributes.
Do Svelte 5 runes need to be imported?
No, runes are compiler syntax and must not be imported. Only standalone functions like tick or untrack require import statements.
Source text
svelte-5-vs-svelte-4-cursorrules-prompt-file.mdc · CC0-1.0
---
description: "Cursor rules for comparing Svelte 5 and Svelte 4 development."
globs: **/*
alwaysApply: false
---
I'm using svelte 5 instead of svelte 4 here is an overview of the changes.
# .cursorrules for Svelte 5
## Overview of Changes
Svelte 5 introduces runes, a set of advanced primitives for controlling reactivity. The runes replace certain non-runes features and provide more explicit control over state and effects.
Snippets, along with render tags, help create reusable chunks of markup inside your components, reducing duplication and enhancing maintainability.
## Event Handlers in Svelte 5
In Svelte 5, event handlers are treated as standard HTML properties rather than Svelte-specific directives, simplifying their use and integrating them more closely with the rest of the properties in the component.
### Svelte 4 vs. Svelte 5:
**Before (Svelte 4):**
```html
<script>
let count = 0;
$: double = count * 2;
$: {
if (count > 10) alert('Too high!');
}
</script>
<button on:click={() => count++}> {count} / {double}</button>
```
**After (Svelte 5):**
```html
<script>
// Define state with runes
let count = $state(0);
// Option 1: Using $derived for computed values
let double = $derived(count * 2);
// Reactive effects using runes
$effect(() => {
if (count > 10) alert('Too high!');
});
</script>
<!-- Standard HTML event attributes instead of Svelte directives -->
<button onclick={() => count++}>
{count} / {double}
</button>
<!-- Alternatively, you can compute values inline -->
<!-- <button onclick={() => count++}>
{count} / {count * 2}
</button> -->
```
## Key Differences:
1. **Reactivity is Explicit**:
- Svelte 5 uses `$state()` to explicitly mark reactive variables
- `$derived()` replaces `$:` for computed values
- `$effect()` replaces `$: {}` blocks for side effects
2. **Event Handling is Standardized**:
- Svelte 4: `on:click={handler}`
- Svelte 5: `onclick={handler}`
3. **Runes are Compiler Syntax**:
- Do not import `$state`, `$derived`, `$effect`, `$props`, `$bindable`, or `$inspect`.
- Import only regular Svelte utilities that require imports, such as `tick`, `untrack`, `mount`, or `unmount`.
4. **No More Event Modifiers**:
- Svelte 4: `on:click|preventDefault={handler}`
- Svelte 5: `onclick={e => { e.preventDefault(); handler(e); }}`
This creates clearer, more maintainable components compared to Svelte 4's previous syntax by making reactivity explicit and using standardized web platform features.
© PatrickJS · CC0-1.0 · PatrickJS/awesome-cursorrules