Skip to main content
rule

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.

Install into Cursor · Manually: put the files in place All methods and platforms ↓
.cursor/rules/svelte-5-vs-svelte-4-cursorrules-prompt-file.mdc
Open on GitHub

What 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 $derived and $effect runes
  • Replace legacy on:click directives with standard onclick attributes
  • Eliminate event modifiers like |preventDefault in 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.mdc

Frontmatter: description, globs, alwaysApply

Step 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

Similar IDE rules

All IDE rules for Frontend and UI →

From the same set210

sveltekit-typescript-guide-cursorrules-prompt-file A Cursor rule that points the agent at Svelte 5, SvelteKit and TypeScript: project structure, runes, state handling. ★ 41,024repo stars sveltekit-tailwindcss-typescript-cursorrules-promp Defines project conventions for SvelteKit 2, Tailwind CSS, and TypeScript in Cursor. It keeps code consistent by enforcing typed components and proper directory structure. ★ 41,024repo stars sveltekit-restful-api-tailwind-css-cursorrules-pro This rule gives Cursor project guidance for SvelteKit, REST APIs, Tailwind CSS, testing, and file management. Use it to keep those practices in view while working on a project. ★ 41,024repo stars svelte A Cursor rule that steers the agent toward Svelte conventions: component structure, reactivity, stores, SvelteKit routing and tests. Useful when the agent writes or edits Svelte code. ★ 41,024repo stars solidjs-typescript-cursorrules-prompt-file This rule guides Cursor through Solid.js and TypeScript conventions, from typed reactive state to component props. Use it when you want those practices reflected in code suggestions. ★ 41,024repo stars solidjs-tailwind-cursorrules-prompt-file A Cursor rule that tells the agent how to write Solid.js code with Tailwind CSS: signals, utility classes, folder layout. ★ 41,024repo stars solidity-react-blockchain-apps-cursorrules-prompt- This Cursor rule guides Solidity contracts and React apps that connect to blockchain services. Use it when reviewing contract safety or Web3 frontend code. ★ 41,024repo stars solidity-hardhat-cursorrules-prompt-file A Cursor rule that turns the AI assistant into a Solidity and smart contract security expert, with Hardhat-based testing and gas work. ★ 41,024repo stars

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor