Skip to content

How to understand a Svelte codebase

Svelte compiles components at build time instead of shipping a framework runtime that diffs a virtual DOM. Svelte 5 introduced runes (`$state`, `$derived`, `$effect`, `$props`) that make reactivity explicit; older code uses `let` assignments, `$:` statements and stores.

How to tell it's Svelte

  • .svelte files and svelte in package.json
  • svelte.config.js and vite.config.ts with the Svelte plugin
  • Runes such as $state(...) or legacy export let props and $: statements

Where things usually live

src/main.ts
Mounts the root component with mount(App, { target }) (Svelte 5) or new App(...) (Svelte 4).
src/App.svelte
Root component.
src/lib/
Components and helpers, often imported through the $lib alias.
src/lib/stores.ts
Writable and derived stores shared across components (pre-runes style).
*.svelte.ts
Svelte 5 modules that use runes outside components, for shared reactive state.

Conventions, not rules. Real projects rename and move things, so confirm against the repository.

Where to start reading

  1. src/main.ts and App.svelte
  2. Shared state in stores.ts or *.svelte.ts files
  3. If routes are file-based under src/routes/, it is a SvelteKit app; see the SvelteKit guide

How a request moves through the code

  1. 1main.ts mounts App.svelte into the page.
  2. 2Components declare state with $state (or let) and receive props with $props() (or export let).
  3. 3Derived values recompute through $derived (or $:), and side effects run in $effect.
  4. 4Shared state flows through stores ($store syntax) or exported rune-based objects.
  5. 5Children report changes upward with callback props or events, or through two-way bind:.

Questions to ask about the repository

  • Is this codebase on Svelte 5 runes, the older syntax, or a mix?
  • Where is shared state kept, and which components subscribe to it?
  • What side effects run in $effect or onMount, and when?

What trips people up

  • Svelte 4 and Svelte 5 syntax look very different; check the version before reading docs or examples.
  • The $ prefix means two things: rune calls like $state() and auto-subscription to a store like $user.
  • Because Svelte is compiled, stack traces point at generated code unless source maps are on.