Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 35 ยท Getting Started

The Svelte Compiler and Your Bundle

See what the Svelte compiler produces, how it tracks dependencies, what warnings it gives, and why Svelte bundles are small.

Knowing a little about what the compiler does makes the rest of the course easier. You do not need to read generated code to use Svelte, but a mental model helps you predict when things update and why some patterns need a rune while others do not.

From file to JavaScript

The compiler lives in the svelte/compiler module, and in practice a bundler plugin calls it for you. Conceptually:

JavaScript
import { compile } from 'svelte/compiler';

const result = compile(source, {
  filename: 'Counter.svelte',
  generate: 'client' // or 'server' for server rendering
});

console.log(result.js.code); // component as JavaScript
console.log(result.css?.code); // scoped styles, if any
console.log(result.warnings); // problems the compiler noticed

The same component can compile in two modes:

  • client: creates DOM nodes in the browser and sets up reactive updates.
  • server: produces a function that returns HTML strings for server-side rendering (SSR).

SvelteKit uses both: the server mode for the first render and the client mode to make the page interactive (hydration).

What the compiler does

The compiler parses your file into a tree, then analyzes it:

  1. Scope analysis: which variables exist, which are runes, which are used in the markup.
  2. Dependency tracking: which parts of the template read which reactive values.
  3. Code generation: DOM creation code, update code, and the CSS with scoping classes.
  4. Warnings: unused CSS, missing alt text, non-reactive updates and other mistakes.

Source file

Counter.svelte script + markup + style
1 / 3

What a rune is, to the compiler

$state, $derived and the others look like functions but are not imported and are not real runtime functions you could pass around. They are syntax the compiler recognizes and rewrites. That is why:

  • You cannot store $state in a variable and call it later.
  • You cannot use runes in plain .js files, only in .svelte and .svelte.js / .svelte.ts.
  • The compiler can reject misuse at build time with a clear message.
SVELTE
<script>
  let count = $state(0);       // fine
  const make = $state;         // error: not a normal function
</script>

Why bundles stay small

Because the compiler decides what each component needs, the browser only receives:

  • the compiled code of your components,
  • a small shared helper library (internal functions for DOM operations and reactivity),
  • anything you import yourself.

Bundlers tree-shake the helper library, so features you never use (such as transitions or {#await} support code) are not shipped. The more your components share, the more the fixed helper cost is spread out.

Do not treat "small" as a promise for every case. A big component tree with many features can still produce a big bundle. Measure with your bundler's report or the browser's network tab when it matters.

Warnings are your friend

The compiler is also a linter. Some examples of problems it reports:

  • a11y_missing_attribute: an <img> with no alt.
  • css_unused_selector: a CSS rule that matches nothing.
  • state_referenced_locally: reading a $state value in a place where it will not stay reactive (you will see this in the next chapter).
  • a11y_click_events_have_key_events: a click handler on a non-interactive element with no keyboard support.

Warnings do not stop the build. Fix them anyway: they are usually real bugs or accessibility gaps. You can silence one intentionally with a comment such as <!-- svelte-ignore a11y_missing_attribute -->, but you should have a reason.

A stand-in for dependency tracking

The compiler's job is to answer "which DOM parts depend on which variable?" before the page runs. This stand-in does it at run time by scanning a template string for markers, then updates only the nodes that mention the changed variable. Real Svelte does this at compile time with no scanning cost in the browser.

JS Update only what depends on a value (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The footer and the greeting were not touched when count changed. That targeted update is the whole point.

Seeing real output

To look at real compiler output, paste a component into the official playground on the Svelte website and open its JS output tab, or call compile yourself in Node. Expect to see calls to internal helpers that create elements, set text, and attach event listeners. The exact shape changes between versions, which is why you should treat generated code as an implementation detail, never as an API.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What are the two compile targets mentioned in this lesson?

    client generates DOM code in the browser; server generates strings for server-side rendering.
  2. 2. Why can't you pass $state around like a normal function?

    Runes are compiler syntax. That is also why they are limited to .svelte and .svelte.js/.ts files.
  3. 3. What should you do about compiler warnings?

    Warnings do not stop the build but usually point to real issues.
  4. 4. Is generated compiler output a stable API?

    Rely on documented component behavior, not the shape of generated code.

Summary

  • The compiler analyzes scopes and dependencies and emits client or server code plus scoped CSS.
  • Runes are compiler syntax, not importable functions.
  • Bundles stay small because only needed helpers are shipped and unused code is tree-shaken.
  • Compiler warnings often flag real accessibility and reactivity mistakes.

Next: Reactive State with $state