svelte-bun
v0.10.0
Published
Svelte for Bun.build and Bun's dev server.
Readme
svelte-bun
Svelte for Bun.build and Bun's dev server.
svelte-bun compiles Svelte 5 components and rune modules for Bun's dev
server and Bun.build, from one line of bunfig,
and Svelte 3 and 4 components too.
It reads svelte.config.js, so your preprocessors
and compiler options work as they do under Vite,
and it handles the cases where Bun's dev server breaks a Svelte app.
When Svelte rejects a file, the build fails with the file
and Svelte's message.
| On Bun 1.4.6, Svelte 5.59.1 | bun-plugin-svelte 0.0.6 | svelte-bun |
| :------------------------------------------------------ | :--------------------------------------------------------------- | :----------- |
| <svelte:head> in the dev server | undefined is not an object (evaluating '$.document.title = …') | ✓ |
| a library's svelte export condition | Could not resolve: "fake-lib" | ✓ |
| @import "./card.css" in a component in a subdirectory | Could not resolve: "./card.css" | ✓ |
| preprocessors and svelte.config.js | ✗ | ✓ |
| a custom renderer, in bun run and bun test | ✗ can't pass experimental.customRenderer | ✓ |
| a minimal app, minify: true, production | 21.2 kB gzip | 18.7 kB gzip |
Quick start
bun add -d svelte-bun svelte# bunfig.toml
[serve.static]
plugins = ["svelte-bun"]bun index.htmlBun's dev server serves index.html,
compiling each .svelte file it reaches, with hot updates.
It keeps each compile in node_modules/.cache/svelte-bun,
so the next start reads it rather than compiling again.
Options come from svelte.config.js, since bunfig can't pass any:
// svelte.config.js
import { optimizeImports } from "carbon-preprocess-svelte";
export default {
preprocess: [optimizeImports()],
compilerOptions: { runes: true },
};In bun run and bun test, a preload compiles the .svelte files
Bun's runtime imports, with the same config,
for the client in a test file with a DOM and for the server in one
marked @vitest-environment node:
# bunfig.toml
preload = ["svelte-bun/register"]The preload reads and writes the same cache,
so under bun test --isolate a component compiles once,
not once per test file that imports it.
An entry is keyed by the component's source, its compiler options,
svelte.config.*, tsconfig.json and the lockfile,
so an edit misses rather than reading a stale entry,
and one unread for 7 days is removed;
a preprocessor that reads other files names them in cacheKey.
svelteBun: { cache: false } in svelte.config.js turns it off;
onwarn there takes each warning as it does under Vite.
A project with no svelte.config.js sets the cache,
and compiler options for the files a glob matches,
under [svelte-bun] in bunfig.toml.
From code
A production build, and a server build for prerendering:
// build.ts
import { svelte } from "svelte-bun";
await Bun.build({
entrypoints: ["index.html"],
outdir: "dist",
minify: true,
plugins: [svelte({ production: true })],
});
await Bun.build({
entrypoints: ["src/ssr.ts"],
outdir: "dist/server",
target: "bun",
plugins: [svelte({ production: true, side: "server" })],
});production drops Svelte's development checks and the dev-only code in
its runtime; it defaults to NODE_ENV === "production".
A host with its own bundler compiles files through
svelte-bun/compile instead.
Docs
- Options:
every option,
svelte.config.js, Svelte 3 and 4, custom renderers, and the preload - Compile API:
compileFile()andscanImports(), for another host - Recipes: prerendering, hydrating, build-time data, CSS types
- Compared:
what differs from
bun-plugin-svelte, and how it was measured
These docs ship in the package, at node_modules/svelte-bun/docs/.
