@oselvar/sveltekit-add-worker-exports 0.2.0 → 0.3.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 +2 -2
- package/dist/index.js +32 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@ A Vite plugin that makes Durable Objects and Workflows work with SvelteKit on Cl
|
|
|
4
4
|
|
|
5
5
|
**Build mode:** SvelteKit's `adapter-cloudflare` generates `_worker.js` with only a default export (the fetch handler). Cloudflare Workers requires Durable Object and Workflow classes to be **named exports**. This plugin post-processes the build output to merge your named exports with SvelteKit's default export.
|
|
6
6
|
|
|
7
|
-
**Dev mode:** `getPlatformProxy` (used by `adapter-cloudflare` in dev) can't run internal Durable Objects. This plugin starts a separate wrangler dev server that runs the real DO worker with hot-reload. Clients connect directly to
|
|
7
|
+
**Dev mode:** `getPlatformProxy` (used by `adapter-cloudflare` in dev) can't run internal Durable Objects or Workflows. This plugin starts a separate wrangler dev server that runs the real DO/Workflow worker with hot-reload. SvelteKit `+server.ts` handlers call DOs through `platform.env.MY_DO.<rpc>()` as usual — the plugin rewrites those bindings to point at the sidecar via wrangler's dev registry, so cross-worker calls Just Work. Clients can also connect directly to the sidecar via WebSocket on a separate port (see below).
|
|
8
8
|
|
|
9
9
|
## Install
|
|
10
10
|
|
|
@@ -87,7 +87,7 @@ export default defineConfig({
|
|
|
87
87
|
});
|
|
88
88
|
```
|
|
89
89
|
|
|
90
|
-
Point `adapter-cloudflare`'s platform proxy at the generated `.platform-proxy-wrangler.jsonc`.
|
|
90
|
+
Point `adapter-cloudflare`'s platform proxy at the generated `.platform-proxy-wrangler.jsonc`. The plugin writes this file with internal Durable Object bindings rewritten to cross-worker form (each gets a `script_name` pointing at the sidecar). Workflows and migrations are stripped — calling a Workflow from `platform.env` in vite dev isn't supported, but it works in production. Without this config path, `getPlatformProxy` would try to run the classes itself and warn that it can't:
|
|
91
91
|
|
|
92
92
|
```javascript
|
|
93
93
|
// svelte.config.js
|
package/dist/index.js
CHANGED
|
@@ -17,10 +17,31 @@
|
|
|
17
17
|
* @see https://github.com/sveltejs/kit/issues/1712
|
|
18
18
|
*/
|
|
19
19
|
import { build } from 'esbuild';
|
|
20
|
+
import { existsSync } from 'node:fs';
|
|
20
21
|
import { access, readFile, rename, writeFile, unlink } from 'node:fs/promises';
|
|
21
|
-
import {
|
|
22
|
+
import { homedir } from 'node:os';
|
|
23
|
+
import { join, resolve } from 'node:path';
|
|
22
24
|
import { parse as parseJsonc } from 'jsonc-parser';
|
|
23
25
|
const DEFAULT_DEV_PORT = 8787;
|
|
26
|
+
/**
|
|
27
|
+
* Mirrors wrangler's internal getRegistryPath() — wrangler doesn't export it.
|
|
28
|
+
* Workers register themselves under this path so cross-worker `script_name`
|
|
29
|
+
* bindings can find each other.
|
|
30
|
+
*/
|
|
31
|
+
function getWranglerRegistryPath() {
|
|
32
|
+
if (process.env.WRANGLER_REGISTRY_PATH)
|
|
33
|
+
return process.env.WRANGLER_REGISTRY_PATH;
|
|
34
|
+
const legacy = join(homedir(), '.wrangler');
|
|
35
|
+
if (existsSync(legacy))
|
|
36
|
+
return join(legacy, 'registry');
|
|
37
|
+
if (process.platform === 'darwin') {
|
|
38
|
+
return join(homedir(), 'Library', 'Preferences', '.wrangler', 'registry');
|
|
39
|
+
}
|
|
40
|
+
if (process.platform === 'win32') {
|
|
41
|
+
return join(process.env.APPDATA ?? join(homedir(), 'AppData', 'Roaming'), '.wrangler', 'registry');
|
|
42
|
+
}
|
|
43
|
+
return join(process.env.XDG_CONFIG_HOME ?? join(homedir(), '.config'), '.wrangler', 'registry');
|
|
44
|
+
}
|
|
24
45
|
/**
|
|
25
46
|
* Returns two Vite plugins: one for build (patches _worker.js) and one for
|
|
26
47
|
* dev (starts a wrangler dev server for Durable Objects).
|
|
@@ -102,20 +123,23 @@ function devPlugin(options) {
|
|
|
102
123
|
await writeFile(tempConfigPath, JSON.stringify(devConfig, null, '\t'));
|
|
103
124
|
// Also write a wrangler config for adapter-cloudflare's getPlatformProxy
|
|
104
125
|
// (used by vite dev for platform.env). It can't run internal DOs or
|
|
105
|
-
// Workflows -- those are served by the sidecar above.
|
|
106
|
-
//
|
|
126
|
+
// Workflows itself -- those are served by the sidecar above. Rewrite
|
|
127
|
+
// internal DO bindings as cross-worker bindings (script_name pointing
|
|
128
|
+
// at the sidecar) so platform.env.MY_DO calls in +server.ts reach the
|
|
129
|
+
// sidecar via the wrangler dev registry. Workflows and migrations get
|
|
130
|
+
// stripped (Workflow bindings have no script_name equivalent).
|
|
107
131
|
const proxyConfig = parseJsonc(rawJson);
|
|
108
132
|
if (proxyConfig.durable_objects?.bindings) {
|
|
109
|
-
proxyConfig.durable_objects.bindings = proxyConfig.durable_objects.bindings.
|
|
110
|
-
if (proxyConfig.durable_objects.bindings.length === 0) {
|
|
111
|
-
delete proxyConfig.durable_objects;
|
|
112
|
-
}
|
|
133
|
+
proxyConfig.durable_objects.bindings = proxyConfig.durable_objects.bindings.map((b) => b.script_name ? b : { ...b, script_name: devConfig.name });
|
|
113
134
|
}
|
|
114
135
|
delete proxyConfig.workflows;
|
|
115
136
|
delete proxyConfig.migrations;
|
|
116
137
|
proxyConfigPath = resolve('.platform-proxy-wrangler.jsonc');
|
|
117
138
|
await writeFile(proxyConfigPath, JSON.stringify(proxyConfig, null, '\t'));
|
|
118
|
-
worker = await unstable_startWorker({
|
|
139
|
+
worker = await unstable_startWorker({
|
|
140
|
+
config: tempConfigPath,
|
|
141
|
+
dev: { registry: getWranglerRegistryPath() }
|
|
142
|
+
});
|
|
119
143
|
server.httpServer?.on('close', async () => {
|
|
120
144
|
if (worker) {
|
|
121
145
|
await worker.dispose();
|
package/package.json
CHANGED