Skip to content

Commit 8bcbe45

Browse files
committed
feat: embed durableObjects exports into workers script
1 parent 18b0599 commit 8bcbe45

File tree

6 files changed

+39
-5
lines changed

6 files changed

+39
-5
lines changed

packages/adapter-cloudflare-workers/files/entry.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Server } from 'SERVER';
22
import { manifest, prerendered } from 'MANIFEST';
3+
DURABLE_OBJECT_EXPORTS;
34
import { getAssetFromKV, mapRequestToAsset } from '@cloudflare/kv-asset-handler';
45
import static_asset_manifest_json from '__STATIC_CONTENT_MANIFEST';
56
const static_asset_manifest = JSON.parse(static_asset_manifest_json);
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
import { Adapter } from '@sveltejs/kit';
22
import './ambient.js';
33

4-
export default function plugin(options: { config?: string }): Adapter;
4+
export default function plugin(options: {
5+
config?: string
6+
/**
7+
* Customize the file that exports DurableObject classes definitions
8+
* @default "./src/lib/durableObjects.js"
9+
*/
10+
durableObjectExports?: string
11+
}): Adapter;

packages/adapter-cloudflare-workers/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { fileURLToPath } from 'node:url';
1515
*/
1616

1717
/** @type {import('.').default} */
18-
export default function ({ config = 'wrangler.toml' } = {}) {
18+
export default function ({ config = 'wrangler.toml', ...options } = {}) {
1919
return {
2020
name: '@sveltejs/adapter-cloudflare-workers',
2121

@@ -38,10 +38,20 @@ export default function ({ config = 'wrangler.toml' } = {}) {
3838
builder.log.minor('Generating worker...');
3939
const relativePath = posix.relative(tmp, builder.getServerDirectory());
4040

41+
let durable_objects_exports = ''
42+
try {
43+
readFileSync(options.durableObjectExports ?? './src/lib/durableObjects.js')
44+
} catch {
45+
if (options.durableObjectExports) {
46+
throw new Error('Specified durableObjectExports file not found');
47+
}
48+
}
49+
4150
builder.copy(`${files}/entry.js`, `${tmp}/entry.js`, {
4251
replace: {
4352
SERVER: `${relativePath}/index.js`,
44-
MANIFEST: './manifest.js'
53+
MANIFEST: './manifest.js',
54+
DURABLE_OBJECT_EXPORTS: durable_objects_exports
4555
}
4656
});
4757

packages/adapter-cloudflare/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ export interface AdapterOptions {
3030
*/
3131
exclude?: string[];
3232
};
33+
/**
34+
* Customize the file that exports DurableObject classes definitions
35+
* @default "./src/lib/durableObjects.js"
36+
*/
37+
durableObjectExports?: string
3338
}
3439

3540
export interface RoutesJSONSpec {

packages/adapter-cloudflare/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { writeFileSync } from 'node:fs';
1+
import { readFileSync, writeFileSync } from 'node:fs';
22
import * as path from 'node:path';
33
import { fileURLToPath } from 'node:url';
44
import * as esbuild from 'esbuild';
@@ -38,10 +38,20 @@ export default function (options = {}) {
3838

3939
writeFileSync(`${dest}/_headers`, generate_headers(builder.config.kit.appDir), { flag: 'a' });
4040

41+
let durable_objects_exports = ''
42+
try {
43+
readFileSync(options.durableObjectExports ?? './src/lib/durableObjects.js')
44+
} catch {
45+
if (options.durableObjectExports) {
46+
throw new Error('Specified durableObjectExports file not found');
47+
}
48+
}
49+
4150
builder.copy(`${files}/worker.js`, `${tmp}/_worker.js`, {
4251
replace: {
4352
SERVER: `${relativePath}/index.js`,
44-
MANIFEST: './manifest.js'
53+
MANIFEST: './manifest.js',
54+
DURABLE_OBJECT_EXPORTS: durable_objects_exports
4555
}
4656
});
4757

packages/adapter-cloudflare/src/worker.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Server } from 'SERVER';
22
import { manifest, prerendered } from 'MANIFEST';
3+
DURABLE_OBJECT_EXPORTS;
34
import * as Cache from 'worktop/cfw.cache';
45

56
const server = new Server(manifest);

0 commit comments

Comments
 (0)