@oselvar/sveltekit-add-worker-exports 0.1.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -0
- package/dist/index.js +19 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -21,9 +21,12 @@ Create a worker entry point that exports your Durable Object classes and a defau
|
|
|
21
21
|
```typescript
|
|
22
22
|
// src/lib/server/index.ts
|
|
23
23
|
export { MyDurableObject } from './MyDurableObject';
|
|
24
|
+
export { MyWorkflow } from './MyWorkflow';
|
|
24
25
|
export { default } from './devHandler';
|
|
25
26
|
```
|
|
26
27
|
|
|
28
|
+
Workflow classes (extending `WorkflowEntrypoint`) are exported the same way as Durable Objects — the plugin merges them into `_worker.js` as named exports. Declare them in `wrangler.jsonc` under `workflows`, and they become available as bindings (e.g. `env.MY_WORKFLOW.create({ params })`) in both dev and production.
|
|
29
|
+
|
|
27
30
|
Both the dev handler and the production SvelteKit route need to do the same thing: validate the upgrade header and forward the request to a Durable Object. Extract that into a small helper so the two callers stay in sync:
|
|
28
31
|
|
|
29
32
|
```typescript
|
|
@@ -84,6 +87,23 @@ export default defineConfig({
|
|
|
84
87
|
});
|
|
85
88
|
```
|
|
86
89
|
|
|
90
|
+
Point `adapter-cloudflare`'s platform proxy at the generated `.platform-proxy-wrangler.jsonc`. This is a copy of your wrangler config with internal Durable Object bindings, Workflows, and migrations stripped — those are served by the dev sidecar on a separate port, and `getPlatformProxy` would otherwise log warnings about classes it can't load:
|
|
91
|
+
|
|
92
|
+
```javascript
|
|
93
|
+
// svelte.config.js
|
|
94
|
+
import adapter from '@sveltejs/adapter-cloudflare';
|
|
95
|
+
|
|
96
|
+
export default {
|
|
97
|
+
kit: {
|
|
98
|
+
adapter: adapter({
|
|
99
|
+
platformProxy: {
|
|
100
|
+
configPath: '.platform-proxy-wrangler.jsonc'
|
|
101
|
+
}
|
|
102
|
+
})
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
```
|
|
106
|
+
|
|
87
107
|
### Dev mode: connecting to Durable Objects
|
|
88
108
|
|
|
89
109
|
In dev mode, the plugin starts a wrangler dev server on a separate port and injects `__DEV_WORKER_PORT__` as a compile-time constant. Use it to connect your client:
|
package/dist/index.js
CHANGED
|
@@ -76,6 +76,7 @@ function devPlugin(options) {
|
|
|
76
76
|
const devPort = options.devPort ?? DEFAULT_DEV_PORT;
|
|
77
77
|
let worker = null;
|
|
78
78
|
let tempConfigPath = null;
|
|
79
|
+
let proxyConfigPath = null;
|
|
79
80
|
return {
|
|
80
81
|
name: 'add-worker-exports-dev',
|
|
81
82
|
apply: 'serve',
|
|
@@ -99,6 +100,21 @@ function devPlugin(options) {
|
|
|
99
100
|
delete devConfig.assets;
|
|
100
101
|
tempConfigPath = resolve('.dev-worker-wrangler.jsonc');
|
|
101
102
|
await writeFile(tempConfigPath, JSON.stringify(devConfig, null, '\t'));
|
|
103
|
+
// Also write a wrangler config for adapter-cloudflare's getPlatformProxy
|
|
104
|
+
// (used by vite dev for platform.env). It can't run internal DOs or
|
|
105
|
+
// Workflows -- those are served by the sidecar above. Strip them so it
|
|
106
|
+
// doesn't emit warnings about classes it can't load.
|
|
107
|
+
const proxyConfig = parseJsonc(rawJson);
|
|
108
|
+
if (proxyConfig.durable_objects?.bindings) {
|
|
109
|
+
proxyConfig.durable_objects.bindings = proxyConfig.durable_objects.bindings.filter((b) => b.script_name);
|
|
110
|
+
if (proxyConfig.durable_objects.bindings.length === 0) {
|
|
111
|
+
delete proxyConfig.durable_objects;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
delete proxyConfig.workflows;
|
|
115
|
+
delete proxyConfig.migrations;
|
|
116
|
+
proxyConfigPath = resolve('.platform-proxy-wrangler.jsonc');
|
|
117
|
+
await writeFile(proxyConfigPath, JSON.stringify(proxyConfig, null, '\t'));
|
|
102
118
|
worker = await unstable_startWorker({ config: tempConfigPath });
|
|
103
119
|
server.httpServer?.on('close', async () => {
|
|
104
120
|
if (worker) {
|
|
@@ -108,6 +124,9 @@ function devPlugin(options) {
|
|
|
108
124
|
if (tempConfigPath) {
|
|
109
125
|
await unlink(tempConfigPath).catch(() => { });
|
|
110
126
|
}
|
|
127
|
+
if (proxyConfigPath) {
|
|
128
|
+
await unlink(proxyConfigPath).catch(() => { });
|
|
129
|
+
}
|
|
111
130
|
});
|
|
112
131
|
}
|
|
113
132
|
};
|
package/package.json
CHANGED