A monorepo containing a lightweight Svelte SPA framework built for Bun runtime.
This project provides a simple framework for building Single Page Applications with Svelte 5 and Bun. It includes:
bun-svelte-spa
) - Core build tools and type-safe runtime router# Create a new project from template
bunx giget gh:torstendittmann/bun-svelte-spa/packages/starter my-app
cd my-app
# Install dependencies and start developing
bun install
bun run dev
# Install the framework
bun add bun-svelte-spa
# Add Svelte 5
bun add svelte
# Create basic files (see Framework Usage below)
bun-svelte-spa
(Framework)Core framework providing:
build()
- Production build with Svelte compilationdev()
- Development server with hot reloadstarter
(Starter Template)A starter template with:
# Install dependencies
bun install
# Format code
bun run format
# Lint code
bun run lint
import { build, dev } from "bun-svelte-spa";
// Build for production
await build({
outdir: "./dist",
});
// Development server
import entrypoint from "./src/index.html";
dev({ entrypoint });
The framework includes a powerful type-safe router with reactive state management.
import About from "@routes/about.svelte";
import Index from "@routes/index.svelte";
import User from "@routes/User.svelte";
import {
create_goto,
create_resolver,
create_routes,
} from "bun-svelte-spa/runtime";
export const routes = create_routes([
{ path: "/", component: Index },
{ path: "/about", component: About },
{ path: "/users/:id", component: User },
]);
export const goto = create_goto(routes);
export const resolve = create_resolver(routes);
<script lang="ts">
import { routes } from "@router";
import { Router } from "bun-svelte-spa/runtime";
</script>
<Router {routes} />
import { goto } from "@router";
// Type-safe navigation
goto("/about");
// Navigation with parameters
goto("/users/:id", { id: "123" });
Or use it in Svelte components:
<script>
import { goto } from "@router";
</script>
<button onclick={() => goto("/about")}>
Go to About
</button>
Access current route data anywhere in your app:
<script>
import { route } from "bun-svelte-spa/runtime";
</script>
<!-- Reactive route information -->
<p>Current path: {$route.path}</p>
<p>Route pattern: {$route.route?.path}</p>
<p>User ID: {$route.params.id}</p>
Extract parameters from the current route:
<script>
import { route } from "bun-svelte-spa/runtime";
// Reactively get route parameters
let userId = $derived($route.params.id);
let isUserRoute = $derived($route.route?.path === "/users/:id");
</script>
Generate URLs programmatically:
import { resolve } from "@router";
// Resolve URLs with parameters
const userUrl = resolve("/users/:id", { id: "123" }); // "/users/123"
The starter template includes convenient path aliases in tsconfig.json
:
{
"compilerOptions": {
"paths": {
"@lib/*": ["./src/lib/*"],
"@routes/*": ["./src/routes/*"],
"@router": ["./src/router.ts"]
}
}
}
MIT