A Bun plugin to handle SvelteKit-specific imports when running scripts with Bun.
When you have a SvelteKit application and use Bun as your runtime, you often want to run scripts that import (directly or indirectly) SvelteKit-specific modules. For example:
In your SvelteKit app:
import { GITHUB_TOKEN } from '$env/static/private';
In your Bun CLI scripts:
const GITHUB_TOKEN = process.env.GITHUB_TOKEN; // or Bun.env.DATABASE_URL
This creates a challenge when building shared utility functions that need to work in both environments - the SvelteKit app (requiring special imports) and Bun CLI scripts (using process.env
).
This plugin allows Bun to resolve SvelteKit's virtual modules, enabling you to use the same imports in both your SvelteKit app and standalone Bun scripts.
Download the plugin file directly:
curl -O https://raw.githubusercontent.com/mquandalle/bun-plugin-sveltekit/main/bun-plugin-sveltekit.ts
Then configure your bunfig.toml
to preload the plugin:
preload = ["./bun-plugin-sveltekit.ts"]
See Bun's plugin documentation for more details on plugin configuration.
The plugin registers handlers for SvelteKit's virtual modules:
$env/static/private
→ Maps to Bun.env
(equivalent to process.env
)$app/environment
→ Returns { dev: true }
This allows your scripts to use SvelteKit imports without modification:
// This now works in both SvelteKit and Bun CLI scripts!
import { DATABASE_URL } from '$env/static/private';
import { dev } from '$app/environment';
console.log('Database:', DATABASE_URL);
console.log('Dev mode:', dev);
Note that $lib alias is automatically handled by Bun
Currently, the plugin only supports:
$env/static/private
$app/environment
It's easy to extend support for other SvelteKit modules by adding more module handlers in the plugin.
An alternative approach is using vite-node
, which can execute scripts with full Vite transformation pipeline, including SvelteKit's module resolution. This might be a better choice if you need more comprehensive SvelteKit module support.
Feel free to submit issues or PRs to add support for more SvelteKit modules!
MIT