agent-rules-kit
Version:
Bootstrap of **Cursor** rules (`.mdc`) and mirror documentation (`.md`) for AI agent-guided projects.
168 lines (120 loc) • 3.73 kB
Markdown
This document provides implementation details specific to Svelte 4.
For Svelte 4, the recommended way to create a new project is using SvelteKit or Vite:
```bash
npm create svelte@latest my-svelte-app
npm create vite@latest my-app -- --template svelte
```
Svelte 4 maintains most of the API from Svelte 3 with some important improvements:
The Svelte 4 runtime is significantly smaller than Svelte 3, resulting in smaller bundle sizes and faster load times.
Svelte 4 uses a completely rewritten compiler based on an AST (Abstract Syntax Tree) instead of regex. This enables better error messages and more efficient output.
### TypeScript Improvements
Native TypeScript support has been improved:
```svelte
<script lang="ts">
// TypeScript is now better supported
export let name: string;
export let count: number = 0;
interface Item {
id: number;
text: string;
}
let items: Item[] = [];
</script>
```
```svelte
<script lang="ts">
import { createEventDispatcher } from 'svelte';
// Typed event dispatcher
const dispatch = createEventDispatcher<{
click: { id: number };
hover: { id: number, position: { x: number, y: number } };
}>();
function handleClick(id: number) {
dispatch('click', { id });
}
</script>
```
Svelte 4 provides clearer error messages when something goes wrong during compilation or at runtime.
Svelte 4 includes more a11y warnings to help developers build more accessible applications.
You can now provide default values even for required props:
```svelte
<script>
// Will receive either the passed value or 'fallback'
export let required = 'fallback';
</script>
```
```svelte
<button on:click={(e) => console.log('First handler')}
on:click={(e) => console.log('Second handler')}>
Multiple handlers
</button>
```
```svelte
<div>
<slot name="header">
<h2>Default Header</h2>
</slot>
<slot>
<p>Default content</p>
</slot>
</div>
```
1. **Custom Elements:** The way custom elements are created has changed
```js
// Svelte 3
import App from './App.svelte';
const app = new App({
target: document.body,
props: { name: 'world' },
});
// Svelte 4
import App from './App.svelte';
new App({
target: document.body,
props: { name: 'world' },
});
```
2. **CSS Changes:** Removed the `global:` modifier, use `:global()` instead
```css
/* Svelte 3 */
global:p {
color: red;
}
/* Svelte 4 */
:global(p) {
color: red;
}
```
3. **TypeScript Imports:** Changed import path for TypeScript users
```ts
// Svelte 3
import { SvelteComponent } from 'svelte';
// Svelte 4
import type { SvelteComponent } from 'svelte';
```
- Most Svelte 3 code will work in Svelte 4 without changes
- For a smooth migration, follow the [official migration guide](https://svelte.dev/docs/migration)
- Use the migration tool for automated updates:
```bash
npx svelte-migrate@latest svelte-4
```
- Svelte 4 integrates with Vite for faster development
- The official VS Code extension provides better syntax highlighting and intellisense
- SvelteKit is the recommended meta-framework for building full applications with Svelte 4