sveltekit-cloudflare-durable-objects Svelte Themes

Sveltekit Cloudflare Durable Objects

Bridge the gap between SvelteKit and Cloudflare Durable Objects - automatic export of Durable Objects to your worker bundle

sveltekit-cloudflare-durable-objects

Bridge the gap between SvelteKit and Cloudflare Durable Objects

Automatically export your Durable Objects to the Cloudflare Worker bundle generated by @sveltejs/adapter-cloudflare.

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 file

There is a suggested work-around:

https://github.com/sveltejs/kit/issues/13692#issuecomment-3055244347

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:

  1. Vite Plugin (recommended) - Seamlessly integrates with your build process
  2. 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-durable-objects
# or
npm install -D sveltekit-cloudflare-durable-objects
# or
yarn add -D sveltekit-cloudflare-durable-objects

Usage

Add the plugin to your vite.config.ts:

import { sveltekit } from '@sveltejs/kit/vite';
import cloudflareDoExporter from 'sveltekit-cloudflare-durable-objects';
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-do

Configuration

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-durable-objects';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [
    sveltekit(),
    cloudflareDoExporter()
  ]
});

4. Build and Deploy

pnpm build
wrangler deploy

How It Works

After your SvelteKit build completes, this tool:

  1. Reads the generated worker file (.svelte-kit/cloudflare/_worker.js)
  2. Checks if Durable Objects are already exported (idempotency)
  3. Appends export statements for your Durable Object files
  4. 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:

  1. Imports DurableObject from 'cloudflare:workers'
  2. Is exported with export class MyDurableObject
  3. Is specified in your Wrangler configuration

TypeScript errors

Generate Cloudflare types:

wrangler types

License

MIT

Contributing

Issues and pull requests welcome!

Top categories

Loading Svelte Themes