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
.sveltefiles andsvelteinpackage.jsonsvelte.config.jsandvite.config.tswith the Svelte plugin- Runes such as
$state(...)or legacyexport letprops and$:statements
Where things usually live
- src/main.ts
- Mounts the root component with
mount(App, { target })(Svelte 5) ornew App(...)(Svelte 4). - src/App.svelte
- Root component.
- src/lib/
- Components and helpers, often imported through the
$libalias. - 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
src/main.tsandApp.svelte- Shared state in
stores.tsor*.svelte.tsfiles - If routes are file-based under
src/routes/, it is a SvelteKit app; see the SvelteKit guide
How a request moves through the code
main.tsmountsApp.svelteinto the page.- Components declare state with
$state(orlet) and receive props with$props()(orexport let). - Derived values recompute through
$derived(or$:), and side effects run in$effect. - Shared state flows through stores (
$storesyntax) or exported rune-based objects. - Children 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
$effectoronMount, 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.