Vite with Svelte
Svelte is a compiler: instead of shipping a framework runtime that diffs a virtual DOM, it turns your components into small, direct DOM-updating JavaScript at build time. That fits Vite perfectly — the Svelte plugin compiles .svelte files on demand during development and in the build.
Two starting points
| Command | Use it for |
|---|---|
npm create vite@latest app -- --template svelte-ts |
A plain single-page Svelte app |
npx sv create app |
SvelteKit: routing, SSR, adapters for many hosts (also Vite-based) |
npm create vite@latest svelte-todo -- --template svelte-ts
cd svelte-todo
npm install
npm run dev> npx
> create-vite svelte-todo --template svelte-ts
│
◇ Scaffolding project in /home/me/svelte-todo...
│
└ Done.
added 41 packages, and audited 42 packages in 5s
found 0 vulnerabilities
> svelte-todo@0.0.0 dev
> vite
VITE v8.3.4 ready in 331 ms
➜ Local: http://localhost:5173/
➜ Network: use --host to expose
➜ press h + enter to show help
Config
// vite.config.ts
import { defineConfig } from 'vite';
import { svelte } from '@sveltejs/vite-plugin-svelte';
export default defineConfig({
plugins: [svelte()],
});// svelte.config.js — read by the plugin and by editor tooling
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
export default {
preprocess: vitePreprocess(), // TypeScript, SCSS etc. inside .svelte files
};A Svelte 5 component
<!-- src/lib/Counter.svelte -->
<script lang="ts">
let count = $state(0);
let doubled = $derived(count * 2);
</script>
<button onclick={() => count++}>
clicked {count} times (doubled: {doubled})
</button>
<style>
button { border: 2px solid #ff3e00; border-radius: 8px; padding: 0.5rem 1rem; }
</style>$state and $derived are runes: compiler instructions that mark reactive values. Styles in a component are scoped automatically.
What the compiler produces
The generated code updates exactly the DOM nodes that depend on changed state. Conceptually it's like this hand-written version:
<!doctype html>
<html>
<body style="font-family: system-ui; padding: 1rem">
<button id="btn" style="border:2px solid #ff3e00;border-radius:8px;padding:.5rem 1rem"></button>
<script>
// roughly what Svelte compiles Counter.svelte into: direct, targeted DOM updates
let count = 0;
const btn = document.getElementById('btn');
const text = document.createTextNode('');
btn.append(text);
function update() { text.data = `clicked ${count} times (doubled: ${count * 2})`; }
btn.addEventListener('click', () => { count++; update(); });
update();
</script>
</body>
</html>And the dependency idea behind $derived, in plain JavaScript:
function state(initial) {
let value = initial;
const listeners = [];
return {
get: () => value,
set: (v) => { value = v; listeners.forEach((fn) => fn()); },
subscribe: (fn) => listeners.push(fn),
};
}
const count = state(0);
const doubled = () => count.get() * 2;
count.subscribe(() => console.log(`count=${count.get()} doubled=${doubled()}`));
count.set(1);
count.set(5);count=1 doubled=2
count=5 doubled=10
Type-checking
Like Vue, .svelte files need their own checker. The template includes svelte-check:
npx svelte-check====================================
Loading svelte-check in workspace: /home/me/svelte-todo
Getting Svelte diagnostics...
svelte-check found 0 errors and 0 warnings
Tip: Choose plain Vite + Svelte for widgets or SPAs embedded in other sites; choose SvelteKit when you need routing, server rendering or static-site generation.
Test yourself
Which package provides the Vite plugin for Svelte?
- @vitejs/plugin-svelte
- @sveltejs/vite-plugin-svelte
- svelte-loader
- vite-svelte
In Svelte 5, what marks a variable as reactive state?
let x = ref(0)let x = $state(0)useState(0)export let x