rspack-plugin-cloudflare 0.0.1-stage → 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/LICENSE +21 -0
- package/README.md +129 -2
- package/THIRD_PARTY_NOTICES.txt +139 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.js +3599 -0
- package/dist/index.js.map +1 -0
- package/dist/workers/asset-worker/index.js +5783 -0
- package/dist/workers/asset-worker/index.js.map +1 -0
- package/dist/workers/router-worker/index.js +6245 -0
- package/dist/workers/router-worker/index.js.map +1 -0
- package/package.json +79 -3
- package/runtime/dev-runtime.js +265 -0
package/package.json
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rspack-plugin-cloudflare",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Develop and build Cloudflare Workers with rspack: runs your Worker in workerd with HMR, using your Wrangler config",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"rspack",
|
|
7
|
+
"rspack-plugin",
|
|
8
|
+
"cloudflare",
|
|
9
|
+
"cloudflare-workers",
|
|
10
|
+
"workerd",
|
|
11
|
+
"wrangler",
|
|
12
|
+
"miniflare",
|
|
13
|
+
"hmr"
|
|
14
|
+
],
|
|
5
15
|
"homepage": "https://github.com/Cherry/rspack-plugin-cloudflare#readme",
|
|
6
16
|
"bugs": {
|
|
7
17
|
"url": "https://github.com/Cherry/rspack-plugin-cloudflare/issues"
|
|
@@ -12,7 +22,73 @@
|
|
|
12
22
|
},
|
|
13
23
|
"license": "MIT",
|
|
14
24
|
"author": "James Ross <james@jross.me>",
|
|
25
|
+
"type": "module",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"default": "./dist/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
15
32
|
"files": [
|
|
16
|
-
"
|
|
17
|
-
|
|
33
|
+
"dist",
|
|
34
|
+
"runtime",
|
|
35
|
+
"THIRD_PARTY_NOTICES.txt"
|
|
36
|
+
],
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsdown",
|
|
39
|
+
"check-types": "tsc -p tsconfig.json && tsc -p src/workers/tsconfig.json && tsc -p test/fixtures/basic/worker/tsconfig.json && tsc -p test/fixtures/legacy-compat/worker/tsconfig.json && tsc -p test/fixtures/multi-worker/tsconfig.json",
|
|
40
|
+
"lint": "eslint .",
|
|
41
|
+
"lint:fix": "eslint . --fix",
|
|
42
|
+
"prepack": "node --run build",
|
|
43
|
+
"test": "node --run lint && node --run check-types && node --run test:unit",
|
|
44
|
+
"test:unit": "node --run build && vitest run"
|
|
45
|
+
},
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": ">=24.10"
|
|
48
|
+
},
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@cloudflare/workers-types": "5.20261102.1",
|
|
54
|
+
"@nodecraft/eslint-config": "54.0.0",
|
|
55
|
+
"@rspack/cli": "2.3.0",
|
|
56
|
+
"@rspack/core": "2.3.0",
|
|
57
|
+
"@rspack/dev-server": "2.2.2",
|
|
58
|
+
"@stylistic/eslint-plugin": "5.10.0",
|
|
59
|
+
"@types/node": "26.6.6",
|
|
60
|
+
"@types/ws": "8.18.2",
|
|
61
|
+
"eslint": "10.14.0",
|
|
62
|
+
"eslint-plugin-import-newlines": "2.0.0",
|
|
63
|
+
"eslint-plugin-import-x": "4.17.1",
|
|
64
|
+
"eslint-plugin-n": "18.5.0",
|
|
65
|
+
"eslint-plugin-regexp": "3.3.3",
|
|
66
|
+
"eslint-plugin-unicorn": "79.0.0",
|
|
67
|
+
"globals": "17.14.0",
|
|
68
|
+
"ignore": "5.3.1",
|
|
69
|
+
"mime": "4.0.7",
|
|
70
|
+
"semantic-release": "25.0.9",
|
|
71
|
+
"toucan-js": "4.0.0",
|
|
72
|
+
"tsdown": "0.23.4",
|
|
73
|
+
"typescript": "6.0.3",
|
|
74
|
+
"typescript-eslint": "8.72.1",
|
|
75
|
+
"vitest": "5.1.0"
|
|
76
|
+
},
|
|
77
|
+
"dependencies": {
|
|
78
|
+
"@cloudflare/containers-shared": "0.23.7",
|
|
79
|
+
"@cloudflare/unenv-preset": "2.16.2",
|
|
80
|
+
"miniflare": "5.20261102.0-beta",
|
|
81
|
+
"unenv": "2.0.0-rc.24",
|
|
82
|
+
"wrangler": "4.156.2",
|
|
83
|
+
"ws": "8.22.1"
|
|
84
|
+
},
|
|
85
|
+
"peerDependencies": {
|
|
86
|
+
"@rspack/core": "^2.0.0",
|
|
87
|
+
"@rspack/dev-server": "^2.0.0"
|
|
88
|
+
},
|
|
89
|
+
"peerDependenciesMeta": {
|
|
90
|
+
"@rspack/dev-server": {
|
|
91
|
+
"optional": true
|
|
92
|
+
}
|
|
93
|
+
}
|
|
18
94
|
}
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
// Bundled into the Worker in dev only. workerd binds handlers and Durable Object classes once at startup, so every
|
|
2
|
+
// export here is a stable wrapper that looks up the user's current code on each call, letting rspack HMR swap it in place.
|
|
3
|
+
import { DurableObject, WorkerEntrypoint, WorkflowEntrypoint } from 'cloudflare:workers';
|
|
4
|
+
|
|
5
|
+
const HMR_RUNNER_BINDING = '__RSPACK_HMR_RUNNER__';
|
|
6
|
+
const HMR_TOKEN_BINDING = '__RSPACK_HMR_TOKEN__';
|
|
7
|
+
const HMR_HEADER = 'x-rspack-plugin-cloudflare-hmr';
|
|
8
|
+
const HANDLER_KEYS = ['fetch', 'scheduled', 'queue', 'email', 'tail', 'tailStream', 'trace', 'test', 'connect'];
|
|
9
|
+
const DURABLE_OBJECT_KEYS = ['fetch', 'alarm', 'connect', 'webSocketMessage', 'webSocketClose', 'webSocketError'];
|
|
10
|
+
// properties workerd probes on RPC targets that must not be treated as RPC methods
|
|
11
|
+
const IGNORED_KEYS = new Set(['then', 'self', 'constructor']);
|
|
12
|
+
|
|
13
|
+
let getWorker = () => {
|
|
14
|
+
return {};
|
|
15
|
+
};
|
|
16
|
+
let initialShape = '';
|
|
17
|
+
|
|
18
|
+
function stripEnv(env) {
|
|
19
|
+
const userEnv = { ...env };
|
|
20
|
+
delete userEnv[HMR_RUNNER_BINDING];
|
|
21
|
+
delete userEnv[HMR_TOKEN_BINDING];
|
|
22
|
+
return userEnv;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function sortNames(names) {
|
|
26
|
+
return names.toSorted((left, right) => left.localeCompare(right));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Node asks for updates through a fetch, since Miniflare's Node-side RPC into the runner can't load modules
|
|
30
|
+
function isHmrRequest(request, env) {
|
|
31
|
+
const token = env[HMR_TOKEN_BINDING];
|
|
32
|
+
return Boolean(token) && request.headers.get(HMR_HEADER) === token;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function applyHotUpdate(env) {
|
|
36
|
+
return new Response(await env[HMR_RUNNER_BINDING].get('runner').apply());
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function prototypeKeys(ctor) {
|
|
40
|
+
const keys = new Set();
|
|
41
|
+
for (let proto = ctor.prototype; proto && proto !== Object.prototype; proto = Object.getPrototypeOf(proto)) {
|
|
42
|
+
for (const key of Object.getOwnPropertyNames(proto)) {
|
|
43
|
+
keys.add(key);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return sortNames([...keys]);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function kindOf(value) {
|
|
50
|
+
if (typeof value === 'function') {
|
|
51
|
+
if (value.prototype instanceof DurableObject) {
|
|
52
|
+
return `DurableObject(${prototypeKeys(value).join(',')})`;
|
|
53
|
+
}
|
|
54
|
+
if (value.prototype instanceof WorkflowEntrypoint) {
|
|
55
|
+
return 'Workflow';
|
|
56
|
+
}
|
|
57
|
+
if (value.prototype instanceof WorkerEntrypoint) {
|
|
58
|
+
return `WorkerEntrypoint(${prototypeKeys(value).join(',')})`;
|
|
59
|
+
}
|
|
60
|
+
return 'function';
|
|
61
|
+
}
|
|
62
|
+
if (value && typeof value === 'object') {
|
|
63
|
+
return `handlers(${HANDLER_KEYS.filter(key => typeof value[key] === 'function').join(',')})`;
|
|
64
|
+
}
|
|
65
|
+
return typeof value;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Fingerprints what workerd saw at startup; when an update changes it, only a restart can rebind the exports. */
|
|
69
|
+
function describeExports(worker) {
|
|
70
|
+
return JSON.stringify(sortNames(Object.keys(worker)).map(name => [name, kindOf(worker[name])]));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function initialize(getter) {
|
|
74
|
+
getWorker = getter;
|
|
75
|
+
initialShape = describeExports(getter());
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function reduceError(error) {
|
|
79
|
+
return {
|
|
80
|
+
name: error?.name,
|
|
81
|
+
message: error?.message ?? String(error),
|
|
82
|
+
stack: error?.stack,
|
|
83
|
+
cause: error?.cause === undefined ? undefined : reduceError(error.cause),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Miniflare revives this response into a source-mapped Error on the Node side, the same trick `wrangler dev` uses
|
|
88
|
+
async function captureFetchError(run) {
|
|
89
|
+
try {
|
|
90
|
+
return await run();
|
|
91
|
+
} catch (error) {
|
|
92
|
+
return Response.json(reduceError(error), { status: 500, headers: { 'MF-Experimental-Error-Stack': 'true' } });
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Makes an RPC property usable both awaited (`await stub.prop`) and called (`stub.method()`), like workerd's own stubs. */
|
|
97
|
+
function rpcProperty(key, valuePromise) {
|
|
98
|
+
const call = async (...args) => {
|
|
99
|
+
const value = await valuePromise;
|
|
100
|
+
if (typeof value !== 'function') {
|
|
101
|
+
throw new TypeError(`"${key}" is not a function.`);
|
|
102
|
+
}
|
|
103
|
+
return value(...args);
|
|
104
|
+
};
|
|
105
|
+
// a thenable on purpose, so `await stub.prop` works the way it does on a real workerd stub
|
|
106
|
+
// eslint-disable-next-line unicorn/no-thenable, unicorn/prefer-then-catch
|
|
107
|
+
call.then = (onFulfilled, onRejected) => valuePromise.then(onFulfilled, onRejected);
|
|
108
|
+
// a property probed but never used shouldn't surface as an unhandled rejection; real use still sees the error
|
|
109
|
+
valuePromise.catch(() => {});
|
|
110
|
+
return call;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function proxyRpc(wrapper, getInstance) {
|
|
114
|
+
return new Proxy(wrapper, {
|
|
115
|
+
get(target, key, receiver) {
|
|
116
|
+
const value = Reflect.get(target, key, receiver);
|
|
117
|
+
if (value !== undefined || typeof key === 'symbol' || IGNORED_KEYS.has(key)) {
|
|
118
|
+
return value;
|
|
119
|
+
}
|
|
120
|
+
return rpcProperty(key, Promise.try(() => {
|
|
121
|
+
const instance = getInstance(receiver);
|
|
122
|
+
const property = instance[key];
|
|
123
|
+
return typeof property === 'function' ? property.bind(instance) : property;
|
|
124
|
+
}));
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// the default export always gets a fetch handler so hot updates can reach the Worker; this keeps workerd's own error otherwise
|
|
130
|
+
function missingFetch() {
|
|
131
|
+
throw new Error('Handler does not export a fetch() function.');
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function createEntrypointWrapper(name, initial) {
|
|
135
|
+
const instanceFor = wrapper => new (getWorker()[name])(wrapper.ctx, stripEnv(wrapper.env));
|
|
136
|
+
class EntrypointWrapper extends WorkerEntrypoint {
|
|
137
|
+
constructor(ctx, env) {
|
|
138
|
+
super(ctx, env);
|
|
139
|
+
return proxyRpc(this, instanceFor);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
for (const key of HANDLER_KEYS) {
|
|
143
|
+
const hasHandler = typeof initial.prototype[key] === 'function';
|
|
144
|
+
if (key === 'fetch' && name === 'default') {
|
|
145
|
+
EntrypointWrapper.prototype.fetch = function(request) {
|
|
146
|
+
if (isHmrRequest(request, this.env)) {
|
|
147
|
+
return applyHotUpdate(this.env);
|
|
148
|
+
}
|
|
149
|
+
return captureFetchError(() => (hasHandler ? instanceFor(this).fetch(request) : missingFetch()));
|
|
150
|
+
};
|
|
151
|
+
} else if (hasHandler) {
|
|
152
|
+
EntrypointWrapper.prototype[key] = function(arg) {
|
|
153
|
+
const call = () => instanceFor(this)[key](arg);
|
|
154
|
+
return key === 'fetch' ? captureFetchError(call) : call();
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return EntrypointWrapper;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function createDurableObjectWrapper(name, initial) {
|
|
162
|
+
const states = new WeakMap();
|
|
163
|
+
// rebuilt when HMR swaps the class; storage survives, in-memory state starts over like the Vite plugin
|
|
164
|
+
const instanceFor = (wrapper) => {
|
|
165
|
+
const state = states.get(wrapper);
|
|
166
|
+
const ctor = getWorker()[name];
|
|
167
|
+
if (state.ctor !== ctor) {
|
|
168
|
+
state.ctor = ctor;
|
|
169
|
+
state.instance = new ctor(state.ctx, stripEnv(state.env));
|
|
170
|
+
}
|
|
171
|
+
return state.instance;
|
|
172
|
+
};
|
|
173
|
+
class DurableObjectWrapper extends DurableObject {
|
|
174
|
+
constructor(ctx, env) {
|
|
175
|
+
super(ctx, env);
|
|
176
|
+
const proxy = proxyRpc(this, instanceFor);
|
|
177
|
+
states.set(proxy, { ctx, env, ctor: undefined, instance: undefined });
|
|
178
|
+
return proxy;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
for (const key of DURABLE_OBJECT_KEYS) {
|
|
182
|
+
if (typeof initial.prototype[key] === 'function') {
|
|
183
|
+
DurableObjectWrapper.prototype[key] = function(...args) {
|
|
184
|
+
return instanceFor(this)[key](...args);
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return DurableObjectWrapper;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function createWorkflowWrapper(name) {
|
|
192
|
+
return class WorkflowWrapper extends WorkflowEntrypoint {
|
|
193
|
+
run(event, step) {
|
|
194
|
+
return new (getWorker()[name])(this.ctx, stripEnv(this.env)).run(event, step);
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function createHandlerWrapper(initial) {
|
|
200
|
+
const handlers = {
|
|
201
|
+
fetch(request, env, ctx) {
|
|
202
|
+
if (isHmrRequest(request, env)) {
|
|
203
|
+
return applyHotUpdate(env);
|
|
204
|
+
}
|
|
205
|
+
return captureFetchError(() => {
|
|
206
|
+
const current = getWorker().default;
|
|
207
|
+
return typeof current?.fetch === 'function' ? current.fetch(request, stripEnv(env), ctx) : missingFetch();
|
|
208
|
+
});
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
for (const key of HANDLER_KEYS) {
|
|
212
|
+
if (key === 'fetch' || typeof initial?.[key] !== 'function') {
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
handlers[key] = (arg, env, ctx) => getWorker().default[key](arg, stripEnv(env), ctx);
|
|
216
|
+
}
|
|
217
|
+
return handlers;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** Wraps one export of the user's Worker so it keeps working across hot updates. */
|
|
221
|
+
export function wrapExport(name) {
|
|
222
|
+
const initial = getWorker()[name];
|
|
223
|
+
if (typeof initial === 'function') {
|
|
224
|
+
if (initial.prototype instanceof DurableObject) {
|
|
225
|
+
return createDurableObjectWrapper(name, initial);
|
|
226
|
+
}
|
|
227
|
+
if (initial.prototype instanceof WorkflowEntrypoint) {
|
|
228
|
+
return createWorkflowWrapper(name);
|
|
229
|
+
}
|
|
230
|
+
if (initial.prototype instanceof WorkerEntrypoint) {
|
|
231
|
+
return createEntrypointWrapper(name, initial);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
if (name === 'default') {
|
|
235
|
+
return createHandlerWrapper(initial);
|
|
236
|
+
}
|
|
237
|
+
return initial;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Applies pending rspack hot updates. It runs in a Durable Object so modules re-evaluate in an I/O context that outlives
|
|
242
|
+
* the request that triggered the update, which matters for promises they create at the top level.
|
|
243
|
+
*/
|
|
244
|
+
export class RspackHmrRunner extends DurableObject {
|
|
245
|
+
async #apply() {
|
|
246
|
+
const hot = import.meta.webpackHot;
|
|
247
|
+
if (!hot) {
|
|
248
|
+
return { reload: true, reason: 'hot module replacement is not enabled' };
|
|
249
|
+
}
|
|
250
|
+
try {
|
|
251
|
+
await hot.check(true);
|
|
252
|
+
} catch (error) {
|
|
253
|
+
return { reload: true, reason: error?.message ?? String(error) };
|
|
254
|
+
}
|
|
255
|
+
if (describeExports(getWorker()) !== initialShape) {
|
|
256
|
+
return { reload: true, reason: 'the Worker\'s exports changed' };
|
|
257
|
+
}
|
|
258
|
+
return { reload: false };
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// serialized here since it becomes the body of the response Node reads
|
|
262
|
+
async apply() {
|
|
263
|
+
return JSON.stringify(await this.#apply());
|
|
264
|
+
}
|
|
265
|
+
}
|