sveltekit-cloudflare-do
v0.2.1
Published
Bridge the gap between SvelteKit and Cloudflare Durable Objects - automatic export of Durable Objects to your worker bundle
Maintainers
Readme
sveltekit-cloudflare-do
Bridge the gap between SvelteKit and Cloudflare Durable Objects
Automatically export your Durable Objects to the Cloudflare Worker bundle generated by @sveltejs/adapter-cloudflare.
[!IMPORTANT] Forked from jillesme/sveltekit-cloudflare-durable-objects. Published separately as
sveltekit-cloudflare-dosince the upstream PR was not merged.
This fork fixes https://github.com/jillesme/sveltekit-cloudflare-durable-objects/issues/1 and adds tsdown as bundler.
The Problem
When using SvelteKit with Cloudflare's adapter, Durable Object classes need to be exported from the worker entry point (.svelte-kit/cloudflare/_worker.js). However, this file is generated during the build process, making it difficult to include your Durable Objects exports.
The typical error you'll encounter:
[ERROR] Your Worker depends on the following Durable Objects, which are not exported in your entrypoint fileThere is a suggested work-around
Unfortunately that is not idempotent. This plugins aims to solve that by setting a marker and making it a Vite plugin so that you don't need to worry about it.
The Solution
This package provides two ways to automatically add your Durable Objects exports to the worker bundle:
- Vite Plugin (recommended) - Seamlessly integrates with your build process
- CLI Tool - For manual or custom build workflows
Both methods are idempotent - safe to run multiple times without duplicating exports.
Installation
pnpm add -D sveltekit-cloudflare-do
# or
npm install -D sveltekit-cloudflare-do
# or
yarn add -D sveltekit-cloudflare-doUsage
Option 1: Vite Plugin (Recommended)
Add the plugin to your vite.config.ts:
import { sveltekit } from "@sveltejs/kit/vite";
import cloudflareDoExporter from "sveltekit-cloudflare-do";
import { defineConfig } from "vite";
export default defineConfig({
plugins: [
sveltekit(),
cloudflareDoExporter({
// Optional: specify your Durable Object files
// Default: ['src/lib/durable-objects.ts']
durableObjects: ["src/lib/durable-objects.ts"],
}),
],
});That's it! The plugin will automatically export your Durable Objects after each build.
Option 2: CLI Tool
Add to your package.json scripts:
{
"scripts": {
"build": "vite build && sveltekit-cloudflare-do"
}
}Or run directly:
pnpm sveltekit-cloudflare-doConfiguration
Vite Plugin Options
interface DurableObjectsExporterOptions {
/**
* Path(s) to your Durable Object file(s)
* @default ['src/lib/durable-objects.ts']
*/
durableObjects?: string | string[];
/**
* Path to the worker file (relative to project root)
* @default '.svelte-kit/cloudflare/_worker.js'
*/
workerPath?: string;
/**
* Enable verbose logging
* @default false
*/
verbose?: boolean;
}CLI Options
sveltekit-cloudflare-do [options]
Options:
--help, -h Show help message
--version, -v Show version
--verbose Enable verbose logging
--worker <path> Path to worker file
--do <path> Path to durable object file (can be used multiple times)Configuration File
You can also configure options in package.json:
{
"sveltekit-cloudflare-do": {
"durableObjects": ["src/lib/durable-objects.ts"],
"workerPath": ".svelte-kit/cloudflare/_worker.js"
}
}Or in .do-exporter.json:
{
"durableObjects": ["src/lib/durable-objects.ts"],
"workerPath": ".svelte-kit/cloudflare/_worker.js"
}Multiple Durable Objects
If you have multiple Durable Object files:
// vite.config.ts
cloudflareDoExporter({
durableObjects: [
"src/lib/durable-objects/chat.ts",
"src/lib/durable-objects/counter.ts",
"src/lib/durable-objects/session.ts",
],
});Complete Example
1. Create your Durable Object
// src/lib/durable-objects.ts
import { DurableObject } from "cloudflare:workers";
export class MyDurableObject extends DurableObject<Env> {
constructor(ctx: DurableObjectState, env: Env) {
super(ctx, env);
}
async fetch(request: Request): Promise<Response> {
return new Response("Hello from Durable Object!");
}
}2. Configure Wrangler
// wrangler.jsonc
{
"name": "my-sveltekit-app",
"main": ".svelte-kit/cloudflare/_worker.js",
"durable_objects": {
"bindings": [
{
"name": "MY_DURABLE_OBJECT",
"class_name": "MyDurableObject",
},
],
},
}3. Add the Vite Plugin
// vite.config.ts
import { sveltekit } from "@sveltejs/kit/vite";
import cloudflareDoExporter from "sveltekit-cloudflare-do";
import { defineConfig } from "vite";
export default defineConfig({
plugins: [sveltekit(), cloudflareDoExporter()],
});4. Build and Deploy
pnpm build
wrangler deployHow It Works
After your SvelteKit build completes, this tool:
- Reads the generated worker file (
.svelte-kit/cloudflare/_worker.js) - Checks if Durable Objects are already exported (idempotency)
- Appends export statements for your Durable Object files
- Adds a marker comment to prevent duplicate exports
Example of what gets added:
// DURABLE_OBJECTS_EXPORT - do not remove
export * from "../../src/lib/durable-objects.ts";Troubleshooting
Worker file not found
Make sure you run vite build before the export tool. The plugin handles this automatically.
Durable Object not found at runtime
Ensure your Durable Object class:
- Imports
DurableObjectfrom'cloudflare:workers' - Is exported with
export class MyDurableObject - Is specified in your Wrangler configuration
TypeScript errors
Generate Cloudflare types:
wrangler typesLicense
MIT
Contributing
Issues and pull requests welcome!
