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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 James Ross
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,5 +1,132 @@
1
1
  # rspack-plugin-cloudflare
2
2
 
3
- Placeholder that reserves the package name so releases can be published from GitHub Actions through npm trusted publishing. Use the latest release instead.
3
+ [![npm version](https://img.shields.io/npm/v/rspack-plugin-cloudflare)](https://www.npmjs.com/package/rspack-plugin-cloudflare)
4
+ [![CI](https://github.com/Cherry/rspack-plugin-cloudflare/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/Cherry/rspack-plugin-cloudflare/actions/workflows/ci.yml)
5
+ [![license](https://img.shields.io/npm/l/rspack-plugin-cloudflare)](./LICENSE)
4
6
 
5
- https://github.com/Cherry/rspack-plugin-cloudflare
7
+ Run Cloudflare Workers in workerd while developing with `rspack serve`, with hot module replacement inside the Worker, and build them alongside the client with `rspack build`. It's the rspack counterpart to `@cloudflare/vite-plugin`, and it reads the same Wrangler config.
8
+
9
+ > [!NOTE]
10
+ > **Heads-up:** this project was primarily AI-authored. The plugin, its tests, and this README were written by AI coding agents in interactive sessions, with a human operator directing the work, reviewing output, and validating it against real rspack and Wrangler projects. Read the code before trusting it in production.
11
+
12
+ ## Install
13
+
14
+ ```sh
15
+ npm install --save-dev rspack-plugin-cloudflare @rspack/core @rspack/cli @rspack/dev-server wrangler
16
+ ```
17
+
18
+ Requires Node.js 24.10 or later.
19
+
20
+ ## Usage
21
+
22
+ ```js
23
+ // rspack.config.js
24
+ import { cloudflare } from 'rspack-plugin-cloudflare';
25
+
26
+ export default {
27
+ entry: './client/index.js',
28
+ output: { path: 'build/assets', publicPath: '/assets/' },
29
+ plugins: [cloudflare()],
30
+ };
31
+ ```
32
+
33
+ The Worker is configured in your existing `wrangler.toml` / `wrangler.json(c)`, found by walking up from rspack's `context`.
34
+
35
+ | Command | What happens |
36
+ | --- | --- |
37
+ | `rspack serve` | The client gets the usual dev server and HMR. Each Worker is bundled by its own rspack compiler and runs in workerd through Miniflare, with HMR of its own. |
38
+ | `rspack build` | Each Worker is bundled into `dist/<worker name>/` with a generated `wrangler.json`, and `.wrangler/deploy/config.json` points at them. |
39
+ | `wrangler deploy` | Deploys the build, following that redirect, without bundling again. |
40
+ | `wrangler dev` | Previews the build the same way, with the `.dev.vars` copied beside it. |
41
+
42
+ Set `CLOUDFLARE_ENV` to pick a Wrangler environment for dev and build, as with the Vite plugin.
43
+
44
+ ### Options
45
+
46
+ | Option | Default | |
47
+ | --- | --- | --- |
48
+ | `configPath` | nearest wrangler config | Wrangler config, relative to `context` |
49
+ | `config` | | Overrides merged into the Wrangler config, or a function that edits it |
50
+ | `environment` | `CLOUDFLARE_ENV` | Wrangler environment, like `--env` |
51
+ | `auxiliaryWorkers` | | `[{ configPath, config?, outputDir? }]`, more Workers the entry Worker reaches through service bindings |
52
+ | `worker` | | `(config, { name }) => config` to adjust a Worker's rspack config |
53
+ | `outputDir` | `dist/<worker name>` | Build output for the Worker, relative to its Wrangler config |
54
+ | `persistState` | `.wrangler/state` | Local binding state; `false` keeps it in memory |
55
+ | `inspectorPort` | 9229, or the next free port | workerd inspector; `false` turns it off |
56
+ | `remoteBindings` | `true` | Connects bindings marked `remote: true` to the real resources; `false` keeps everything local |
57
+ | `tunnel` | `false` | `true` or `{ name?, allowedMail? }` shares the dev server through a Cloudflare Tunnel |
58
+
59
+ ## How dev works
60
+
61
+ Requests hit the rspack dev server first. A middleware in front of `dev-middleware` passes each one to Cloudflare's router and asset Workers, built from the same release of Cloudflare's code as `wrangler dev` runs:
62
+
63
+ - `assets.run_worker_first`, `html_handling`, `not_found_handling`, `_headers` and `_redirects` behave as they do when deployed. Editing `_headers` or `_redirects` restarts the Workers to apply it.
64
+ - Assets come from the client compiler's in-memory output, falling back to the assets directory on disk, so they're always the latest build. Their `ETag` and `Cache-Control` headers are dropped, so the browser never reuses a stale dev file.
65
+ - The Worker's `ASSETS` binding goes to the same asset Worker, so it sees dev builds too.
66
+ - WebSocket upgrades go to the Worker, except the dev server's own HMR socket.
67
+ - `/cdn-cgi/local/scheduled` and the other Miniflare trigger endpoints (and their older `/cdn-cgi/handler/*` paths) reach the Worker directly.
68
+ - `/__debug` opens DevTools for every Worker.
69
+
70
+ Bindings, vars, `.dev.vars` / `.env`, Durable Objects and compatibility settings all come from wrangler, so they match `wrangler dev`. Workers also join wrangler's dev registry, so service bindings reach Workers running in other local dev sessions.
71
+
72
+ ### Hot module replacement in the Worker
73
+
74
+ The dev bundle includes rspack's HMR runtime. After a rebuild, the plugin asks the running Worker to apply the update before letting requests through, the way the Vite plugin's module runner does:
75
+
76
+ - Changed modules and their importers re-evaluate in place. Other modules keep their state, and Durable Objects keep their storage.
77
+ - Exports are wrapped so workerd's startup bindings (default handlers, Durable Objects, named entrypoints, Workflows) always call the current code.
78
+ - The Worker restarts instead when its exports change, its `.wasm`/`.bin` modules change, its Wrangler config or local secrets change, or an update can't be applied.
79
+
80
+ Uncaught errors come back as a page with a source-mapped stack, and Worker build errors show in the dev server's overlay.
81
+
82
+ ### Node.js compatibility
83
+
84
+ With `nodejs_compat`, modules workerd provides stay external, and Cloudflare's unenv preset polyfills the rest and injects globals like `process` for older compatibility dates, the same as wrangler.
85
+
86
+ ### Non-JavaScript modules
87
+
88
+ `.wasm` and `.bin` imports ship beside the bundle as their own modules. Text modules (`.txt`, `.html`, `.sql` and any `Text` rules) are inlined. Custom `rules` in the Wrangler config apply.
89
+
90
+ ### Static assets imported by the Worker
91
+
92
+ When the Worker has an assets directory, importing an image, font, video or other static file (or any file with `?url`) gives its URL, like in Vite:
93
+
94
+ ```ts
95
+ import logoUrl from './logo.svg';
96
+
97
+ export default {
98
+ fetch(request, env) {
99
+ return env.ASSETS.fetch(new URL(logoUrl, request.url));
100
+ },
101
+ };
102
+ ```
103
+
104
+ Files are named `assets/<name>.<hash><ext>`. Dev serves them alongside the client build, and `rspack build` copies them into the assets directory after the client build emits.
105
+
106
+ ### Containers
107
+
108
+ Container-backed Durable Objects run locally like `wrangler dev`, using the same Cloudflare packages as wrangler and the Vite plugin: dev builds each Dockerfile (or pulls each registry image) before starting the Workers, then removes the containers when it exits. It needs a running Docker daemon, and like wrangler it doesn't support Windows outside WSL. Set `dev.enable_containers` to `false` in the Wrangler config to skip it. Images are built once per dev session, so restart dev after changing a Dockerfile.
109
+
110
+ ### Sharing with a tunnel
111
+
112
+ `tunnel: true` starts a Quick Tunnel on a random `trycloudflare.com` URL once the dev server is listening, with no account needed. `tunnel: { name }` runs an existing named tunnel instead, after checking it has a route to the dev server. `allowedMail` makes Quick Tunnel visitors sign in with a matching email. wrangler runs the tunnel, so it downloads `cloudflared` when needed, and the tunnel's hostnames are added to the dev server's `allowedHosts`.
113
+
114
+ Anyone with the URL reaches the dev server, including the Worker's local bindings and secrets, so only turn this on while you're sharing.
115
+
116
+ ## Contributing
117
+
118
+ ```sh
119
+ git submodule update --init
120
+ npm ci
121
+ node --run test
122
+ ```
123
+
124
+ `node --run test` lints, type checks, builds and runs the test suite, which starts real `rspack serve` and `rspack build` runs against the fixtures in `test/fixtures`.
125
+
126
+ Cloudflare's router and asset Workers and their `_headers` and `_redirects` parsers come from `@cloudflare/workers-shared`, which isn't published. The `workers-sdk` submodule is pinned to the commit of the wrangler release we depend on, and `node --run build` bundles that code into `dist`. The weekly Sync upstream workflow moves wrangler, Miniflare, the packages wrangler pins and the submodule to the latest wrangler release together, runs the tests and opens a pull request. To do the same locally, run `node scripts/sync-upstream.ts [version]`.
127
+
128
+ Releases are automated. Merging to `main` runs [semantic-release](https://github.com/semantic-release/semantic-release), which picks the version from the [conventional commit](https://www.conventionalcommits.org/) titles since the last release and publishes to npm through trusted publishing, with provenance. Pull requests are squash merged, so the pull request title is the commit that counts.
129
+
130
+ ## License
131
+
132
+ MIT
@@ -0,0 +1,139 @@
1
+ rspack-plugin-cloudflare bundles code from the following projects into dist/.
2
+
3
+ ----
4
+
5
+ cloudflare/workers-sdk: @cloudflare/workers-shared (dist/index.js and dist/workers)
6
+ https://github.com/cloudflare/workers-sdk, licensed under MIT OR Apache-2.0 and used here under the MIT License:
7
+
8
+ Copyright (c) 2020 Cloudflare, Inc. <wrangler@cloudflare.com>
9
+
10
+ Permission is hereby granted, free of charge, to any
11
+ person obtaining a copy of this software and associated
12
+ documentation files (the "Software"), to deal in the
13
+ Software without restriction, including without
14
+ limitation the rights to use, copy, modify, merge,
15
+ publish, distribute, sublicense, and/or sell copies of
16
+ the Software, and to permit persons to whom the Software
17
+ is furnished to do so, subject to the following
18
+ conditions:
19
+
20
+ The above copyright notice and this permission notice
21
+ shall be included in all copies or substantial portions
22
+ of the Software.
23
+
24
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
25
+ ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
26
+ TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
27
+ PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
28
+ SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
29
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
31
+ IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32
+ DEALINGS IN THE SOFTWARE.
33
+
34
+ ----
35
+
36
+ toucan-js (dist/workers)
37
+ https://github.com/robertcepa/toucan-js
38
+
39
+ MIT License
40
+
41
+ Copyright (c) 2022 Robert Cepa
42
+
43
+ Permission is hereby granted, free of charge, to any person obtaining a copy
44
+ of this software and associated documentation files (the "Software"), to deal
45
+ in the Software without restriction, including without limitation the rights
46
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
47
+ copies of the Software, and to permit persons to whom the Software is
48
+ furnished to do so, subject to the following conditions:
49
+
50
+ The above copyright notice and this permission notice shall be included in all
51
+ copies or substantial portions of the Software.
52
+
53
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
54
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
55
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
56
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
57
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
58
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
59
+ SOFTWARE.
60
+
61
+ ----
62
+
63
+ @sentry/core and @sentry/utils (dist/workers)
64
+ https://github.com/getsentry/sentry-javascript
65
+
66
+ MIT License
67
+
68
+ Copyright (c) 2019-2024 Functional Software, Inc. dba Sentry
69
+
70
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
71
+ this software and associated documentation files (the "Software"), to deal in
72
+ the Software without restriction, including without limitation the rights to
73
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
74
+ of the Software, and to permit persons to whom the Software is furnished to do
75
+ so, subject to the following conditions:
76
+
77
+ The above copyright notice and this permission notice shall be included in all
78
+ copies or substantial portions of the Software.
79
+
80
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
81
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
82
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
83
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
84
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
85
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
86
+ SOFTWARE.
87
+
88
+ ----
89
+
90
+ mime (dist/index.js)
91
+ https://github.com/broofa/mime
92
+
93
+ MIT License
94
+
95
+ Copyright (c) 2023 Robert Kieffer
96
+
97
+ Permission is hereby granted, free of charge, to any person obtaining a copy
98
+ of this software and associated documentation files (the "Software"), to deal
99
+ in the Software without restriction, including without limitation the rights
100
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
101
+ copies of the Software, and to permit persons to whom the Software is
102
+ furnished to do so, subject to the following conditions:
103
+
104
+ The above copyright notice and this permission notice shall be included in all
105
+ copies or substantial portions of the Software.
106
+
107
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
108
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
109
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
110
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
111
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
112
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
113
+ SOFTWARE.
114
+ ----
115
+
116
+ ignore (dist/index.js)
117
+ https://github.com/kaelzhang/node-ignore
118
+
119
+ Copyright (c) 2013 Kael Zhang <i@kael.me>, contributors
120
+ http://kael.me/
121
+
122
+ Permission is hereby granted, free of charge, to any person obtaining
123
+ a copy of this software and associated documentation files (the
124
+ "Software"), to deal in the Software without restriction, including
125
+ without limitation the rights to use, copy, modify, merge, publish,
126
+ distribute, sublicense, and/or sell copies of the Software, and to
127
+ permit persons to whom the Software is furnished to do so, subject to
128
+ the following conditions:
129
+
130
+ The above copyright notice and this permission notice shall be
131
+ included in all copies or substantial portions of the Software.
132
+
133
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
134
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
135
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
136
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
137
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
138
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
139
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,54 @@
1
+ import { Unstable_Config } from "wrangler";
2
+ import { Compiler, RspackOptions, RspackPluginInstance } from "@rspack/core";
3
+ type WorkerConfig = Unstable_Config;
4
+ /** Overrides merged into the Wrangler config after it loads, or a function that edits or returns overrides. */
5
+ type WorkerConfigCustomizer = Partial<WorkerConfig> | ((config: WorkerConfig) => Partial<WorkerConfig> | void);
6
+ interface AuxiliaryWorkerOptions {
7
+ /** Wrangler config for the auxiliary Worker, relative to the rspack `context`. */
8
+ configPath: string;
9
+ /** Changes the auxiliary Worker's config in code, applied after its config file loads. */
10
+ config?: WorkerConfigCustomizer;
11
+ /** Where `rspack build` writes it, relative to its Wrangler config. Defaults to `dist/<worker name>`. */
12
+ outputDir?: string;
13
+ }
14
+ interface TunnelOptions {
15
+ /** An existing named tunnel with a route to the dev server. Without one, a Quick Tunnel gets a random trycloudflare.com URL. */
16
+ name?: string;
17
+ /** Email addresses or `*@domain` patterns allowed through a Quick Tunnel, which then asks visitors to sign in. */
18
+ allowedMail?: string[];
19
+ }
20
+ interface CloudflarePluginOptions {
21
+ /** Wrangler config file, relative to the rspack `context`. Defaults to the nearest wrangler.jsonc, wrangler.json or wrangler.toml at or above `context`. */
22
+ configPath?: string;
23
+ /** Changes the Worker config in code, applied after the config file loads. */
24
+ config?: WorkerConfigCustomizer;
25
+ /** Wrangler environment to use, like `wrangler --env`, for every Worker. Defaults to `CLOUDFLARE_ENV`. */
26
+ environment?: string;
27
+ /** More Workers the entry Worker reaches through service bindings, run and built alongside it. */
28
+ auxiliaryWorkers?: AuxiliaryWorkerOptions[];
29
+ /** Adjusts the rspack config used to bundle a Worker. Return a new config or mutate the one given. */
30
+ worker?: (config: RspackOptions, context: {
31
+ name: string;
32
+ }) => RspackOptions | void;
33
+ /** Where `rspack build` writes the Worker bundle and its generated `wrangler.json`, relative to the Wrangler config. Defaults to `dist/<worker name>`. */
34
+ outputDir?: string;
35
+ /** Where local binding state (KV, Durable Objects, etc.) lives in dev, relative to the Wrangler config. `false` keeps it in memory. Defaults to `.wrangler/state`, shared with `wrangler dev`. */
36
+ persistState?: boolean | {
37
+ path: string;
38
+ };
39
+ /** Port for the workerd inspector in dev, or `false` to turn it off. Defaults to 9229, or the next free port. */
40
+ inspectorPort?: number | false;
41
+ /** Connects bindings marked `remote: true` to their real resources on Cloudflare in dev. `false` keeps every binding local. */
42
+ remoteBindings?: boolean;
43
+ /** Shares the dev server publicly through a Cloudflare Tunnel once it's listening. */
44
+ tunnel?: boolean | TunnelOptions;
45
+ }
46
+ export declare class CloudflarePlugin implements RspackPluginInstance {
47
+ #private;
48
+ readonly options: CloudflarePluginOptions;
49
+ constructor(options?: CloudflarePluginOptions);
50
+ apply(compiler: Compiler): void;
51
+ }
52
+ export declare function cloudflare(options?: CloudflarePluginOptions): CloudflarePlugin;
53
+ export type { AuxiliaryWorkerOptions, CloudflarePluginOptions, TunnelOptions, WorkerConfig, WorkerConfigCustomizer };
54
+ //# sourceMappingURL=index.d.ts.map