Svelte 5 on Bun 1.2: Getting started

Table of contents

Summary Summary

The latest Svelte (opens in a new tab) 5 (opens in a new tab) update brings runes, a fresh approach to reactivity which replaced $:. Although some non-trivial tasks are required for Svelte 4 projects to migrate to 5 (opens in a new tab), developers now have greater flexibility and performance benefits thanks to the runes system. Their transition aims to make apps more efficient and maintainable.

This post shows how to start to develop a Svelte 5 project with Bun (opens in a new tab).

Environment Environment

Tutorial Tutorial

To be short, there are few steps. Run command-lines and you are there soon.

1. Create a project 1. Create a project

console
$ bunx sv create sveltekit-example

Some questions will be asked by the installer. There is an example set of my replies below:

console
┌  Welcome to the Svelte CLI! (v0.6.20)
│
◇  Which template would you like?
│  SvelteKit minimal
│
◇  Add type checking with Typescript?
│  Yes, using Typescript syntax
│
◆  Project created
│
◇  What would you like to add to your project? (use arrow keys / space bar)
│  prettier, eslint
│
◆  Successfully setup add-ons
│
◇  Which package manager do you want to install dependencies with?
│  bun
│
◆  Successfully installed dependencies
│
◇  Successfully formatted modified files
│
◇  Project next steps ─────────────────────────────────────────────────────╮
│                                                                          │
│  1: cd sveltekit-example                                                 │
│  2: git init && git add -A && git commit -m "Initial commit" (optional)  │
│  3: bun run dev --open                                                   │
│                                                                          │
│  To close the dev server, hit Ctrl-C                                     │
│                                                                          │
│  Stuck? Visit us at https://svelte.dev/chat                              │
│                                                                          │
├──────────────────────────────────────────────────────────────────────────╯
│
└  You're all set!

The project was created and is ready. Let’s come in:

console
$ cd sveltekit-example

Besides, the default set of directories and files (with selected options) are as below:

console
$ ls -a1F
./
../
.svelte-kit/
node_modules/
src/
static/
.gitignore
.npmrc
.prettierignore
.prettierrc
bun.lock
eslint.config.js
package.json
README.md
svelte.config.js
tsconfig.json
vite.config.ts

$ find src/
src/
src/app.html
src/app.d.ts
src/lib
src/lib/index.ts
src/routes
src/routes/+page.svelte

2. Run demo app 2. Run demo app

Run the demo of its minimal app template:

console
$ bun run dev --open

You will see:

The SvelteKit starter page at localhost:5173: the heading “Welcome to SvelteKit” and a link to the documentation at svelte.dev/docs/kit.

Note that the process watches files by default. When some change is detected, it will update the rendered page on browser.

3. Customize 3. Customize

Now you are ready to make your own app. For example, modify src/routes/+page.svelte like below:

svelte
<script lang="ts">
    let counter: number = $state(0)
    const incrementCounter = () => counter++
</script>

<div class="container">
    <div class="output">{counter}</div>
    <button onclick={incrementCounter}>Increment</button>
</div>

<style>
    .container {
        padding: 0.8rem 0.2rem;
        display: flex;
        justify-content: center;
        border: 0.5em solid #fdfa72;
        border-radius: 1.4rem;
    }

    .output {
        width: 2.5em;
        margin-right: 0.8rem;
        color: #4f68dc;
        text-align: right;
        font-size: 1.2rem;
        font-weight: bold;
    }
</style>

It will show:

The new component in the browser: the count 30 next to an “Increment” button, inside a yellow rounded frame.

You can add another Svelte component such as +layout.svelte (opens in a new tab), and also create your own components, layouts, styles and types.

Conclusion Conclusion

Svelte 5 refines its core on reactivity with runes, replacing the $:. It makes component updates clearer and more efficient. It also sets the stage for better scalability in larger projects.

Enjoy the latest sophisticated App development with Svelte 5 :)

More on: Web and back-end development

Scqr [secureal]

Scqr [secureal]: the engineering practice of Reflerd Inc. (opens in a new tab)

Comments and feedback are welcome.