rspack-plugin-cloudflare 0.0.0-stage → 0.1.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 +128 -2
- package/dist/assets.d.ts +31 -0
- package/dist/assets.js +112 -0
- package/dist/assets.js.map +1 -0
- package/dist/build.d.ts +216 -0
- package/dist/build.js +140 -0
- package/dist/build.js.map +1 -0
- package/dist/config.d.ts +19 -0
- package/dist/config.js +103 -0
- package/dist/config.js.map +1 -0
- package/dist/containers.d.ts +30 -0
- package/dist/containers.js +167 -0
- package/dist/containers.js.map +1 -0
- package/dist/dev-entry.d.ts +11 -0
- package/dist/dev-entry.js +44 -0
- package/dist/dev-entry.js.map +1 -0
- package/dist/dev-worker.d.ts +73 -0
- package/dist/dev-worker.js +247 -0
- package/dist/dev-worker.js.map +1 -0
- package/dist/dev.d.ts +28 -0
- package/dist/dev.js +422 -0
- package/dist/dev.js.map +1 -0
- package/dist/http.d.ts +23 -0
- package/dist/http.js +89 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +139 -0
- package/dist/index.js.map +1 -0
- package/dist/inspector.d.ts +5 -0
- package/dist/inspector.js +41 -0
- package/dist/inspector.js.map +1 -0
- package/dist/miniflare.d.ts +1 -0
- package/dist/miniflare.js +6 -0
- package/dist/miniflare.js.map +1 -0
- package/dist/module-rules.d.ts +4 -0
- package/dist/module-rules.js +29 -0
- package/dist/module-rules.js.map +1 -0
- package/dist/nodejs-compat.d.ts +14 -0
- package/dist/nodejs-compat.js +46 -0
- package/dist/nodejs-compat.js.map +1 -0
- package/dist/options.d.ts +45 -0
- package/dist/options.js +2 -0
- package/dist/options.js.map +1 -0
- package/dist/routing.d.ts +12 -0
- package/dist/routing.js +40 -0
- package/dist/routing.js.map +1 -0
- package/dist/tunnel.d.ts +15 -0
- package/dist/tunnel.js +94 -0
- package/dist/tunnel.js.map +1 -0
- package/dist/worker-config.d.ts +42 -0
- package/dist/worker-config.js +194 -0
- package/dist/worker-config.js.map +1 -0
- package/package.json +91 -5
- package/runtime/dev-runtime.js +265 -0
- package/src/assets.ts +148 -0
- package/src/build.ts +153 -0
- package/src/config.ts +138 -0
- package/src/containers.ts +183 -0
- package/src/dev-entry.ts +50 -0
- package/src/dev-worker.ts +297 -0
- package/src/dev.ts +478 -0
- package/src/http.ts +102 -0
- package/src/index.ts +164 -0
- package/src/inspector.ts +44 -0
- package/src/miniflare.ts +7 -0
- package/src/module-rules.ts +36 -0
- package/src/nodejs-compat.ts +63 -0
- package/src/options.ts +46 -0
- package/src/routing.ts +48 -0
- package/src/tunnel.ts +106 -0
- package/src/worker-config.ts +237 -0
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,3 +1,129 @@
|
|
|
1
|
-
#
|
|
1
|
+
# rspack-plugin-cloudflare
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/rspack-plugin-cloudflare)
|
|
4
|
+
[](https://github.com/Cherry/rspack-plugin-cloudflare/actions/workflows/ci.yml)
|
|
5
|
+
[](./LICENSE)
|
|
6
|
+
|
|
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` routes each one the way Cloudflare's router does:
|
|
62
|
+
|
|
63
|
+
- `assets.run_worker_first` rules decide whether the Worker sees a request before assets, with `!/` rules always going to assets.
|
|
64
|
+
- Assets come from the client compiler's in-memory output, falling back to the assets directory on disk, with `html_handling` and `not_found_handling` applied.
|
|
65
|
+
- The Worker's `ASSETS` binding reads from the same place, 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`: 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
|
+
npm ci
|
|
120
|
+
node --run test
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
`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`.
|
|
124
|
+
|
|
125
|
+
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.
|
|
126
|
+
|
|
127
|
+
## License
|
|
128
|
+
|
|
129
|
+
MIT
|
package/dist/assets.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
/** The slice of rspack's output filesystem we read from; the dev server swaps in an in-memory one. */
|
|
3
|
+
export interface ReadableFileSystem {
|
|
4
|
+
readFile: (filePath: string, callback: (error: NodeJS.ErrnoException | null, data?: Buffer | string) => void) => void;
|
|
5
|
+
stat: (filePath: string, callback: (error: NodeJS.ErrnoException | null, stats?: {
|
|
6
|
+
isFile: () => boolean;
|
|
7
|
+
}) => void) => void;
|
|
8
|
+
}
|
|
9
|
+
interface AssetsConfig {
|
|
10
|
+
html_handling?: string;
|
|
11
|
+
not_found_handling?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface AssetServer {
|
|
14
|
+
/** Whether a request path matches a file, applying the configured html_handling. */
|
|
15
|
+
exists: (pathname: string) => Promise<boolean>;
|
|
16
|
+
/** Serves a request from the assets directory, applying not_found_handling when nothing matches. */
|
|
17
|
+
handle: (req: IncomingMessage, res: ServerResponse) => Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Serves the Worker's assets directory the way the asset Worker does, reading the compiler's output filesystem first so
|
|
21
|
+
* files only emitted in memory by the dev server are visible.
|
|
22
|
+
*/
|
|
23
|
+
export declare function createAssetServer(options: {
|
|
24
|
+
directory: string;
|
|
25
|
+
config: AssetsConfig | undefined;
|
|
26
|
+
getOutputFileSystem: () => ReadableFileSystem | undefined;
|
|
27
|
+
waitUntilValid: () => Promise<void>;
|
|
28
|
+
/** More directories served at the root, checked after `directory`. */
|
|
29
|
+
extraDirectories?: string[];
|
|
30
|
+
}): AssetServer;
|
|
31
|
+
export {};
|
package/dist/assets.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { lookup } from 'mrmime';
|
|
4
|
+
function statFile(fileSystem, filePath) {
|
|
5
|
+
return new Promise((resolve) => {
|
|
6
|
+
fileSystem.stat(filePath, (error, stats) => {
|
|
7
|
+
resolve(!error && Boolean(stats?.isFile()));
|
|
8
|
+
});
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
function readFile(fileSystem, filePath) {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
fileSystem.readFile(filePath, (error, data) => {
|
|
14
|
+
if (error || data === undefined) {
|
|
15
|
+
reject(error ?? new Error(`Could not read ${filePath}`));
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
resolve(Buffer.from(data));
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
function htmlCandidates(pathname, htmlHandling) {
|
|
23
|
+
if (htmlHandling === 'none') {
|
|
24
|
+
return [pathname];
|
|
25
|
+
}
|
|
26
|
+
if (pathname.endsWith('/')) {
|
|
27
|
+
return [`${pathname}index.html`];
|
|
28
|
+
}
|
|
29
|
+
return [pathname, `${pathname}.html`, `${pathname}/index.html`];
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Serves the Worker's assets directory the way the asset Worker does, reading the compiler's output filesystem first so
|
|
33
|
+
* files only emitted in memory by the dev server are visible.
|
|
34
|
+
*/
|
|
35
|
+
export function createAssetServer(options) {
|
|
36
|
+
const { directory, config } = options;
|
|
37
|
+
const roots = [directory, ...options.extraDirectories ?? []];
|
|
38
|
+
async function findFile(filePath) {
|
|
39
|
+
const outputFileSystem = options.getOutputFileSystem();
|
|
40
|
+
if (outputFileSystem && await statFile(outputFileSystem, filePath)) {
|
|
41
|
+
return { fileSystem: outputFileSystem, filePath };
|
|
42
|
+
}
|
|
43
|
+
if (await statFile(fs, filePath)) {
|
|
44
|
+
return { fileSystem: fs, filePath };
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function toFilePath(root, pathname) {
|
|
48
|
+
const filePath = path.join(root, decodeURIComponent(pathname));
|
|
49
|
+
// path.join normalizes `..`, so anything still escaping the directory is a traversal attempt
|
|
50
|
+
if (filePath !== root && !filePath.startsWith(root + path.sep)) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
return filePath;
|
|
54
|
+
}
|
|
55
|
+
async function resolveFile(pathname) {
|
|
56
|
+
await options.waitUntilValid();
|
|
57
|
+
for (const candidate of htmlCandidates(pathname, config?.html_handling)) {
|
|
58
|
+
for (const root of roots) {
|
|
59
|
+
const filePath = toFilePath(root, candidate);
|
|
60
|
+
const found = filePath && await findFile(filePath);
|
|
61
|
+
if (found) {
|
|
62
|
+
return found;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async function resolveNotFound(pathname) {
|
|
68
|
+
if (config?.not_found_handling === 'single-page-application') {
|
|
69
|
+
return { status: 200, file: await resolveFile('/index.html') };
|
|
70
|
+
}
|
|
71
|
+
if (config?.not_found_handling === '404-page') {
|
|
72
|
+
// the nearest 404.html walking up from the requested path
|
|
73
|
+
const segments = pathname.split('/').slice(0, -1);
|
|
74
|
+
while (segments.length > 0) {
|
|
75
|
+
const file = await resolveFile(`${segments.join('/')}/404.html`);
|
|
76
|
+
if (file) {
|
|
77
|
+
return { status: 404, file };
|
|
78
|
+
}
|
|
79
|
+
segments.pop();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return { status: 404, file: undefined };
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
async exists(pathname) {
|
|
86
|
+
const file = await resolveFile(pathname);
|
|
87
|
+
return file !== undefined;
|
|
88
|
+
},
|
|
89
|
+
async handle(req, res) {
|
|
90
|
+
const { pathname } = new URL(req.url ?? '/', 'http://localhost');
|
|
91
|
+
let status = 200;
|
|
92
|
+
let file = await resolveFile(pathname);
|
|
93
|
+
if (!file) {
|
|
94
|
+
({ status, file } = await resolveNotFound(pathname));
|
|
95
|
+
}
|
|
96
|
+
if (!file) {
|
|
97
|
+
res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
|
|
98
|
+
res.end('Not Found');
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const body = await readFile(file.fileSystem, file.filePath);
|
|
102
|
+
res.writeHead(status, {
|
|
103
|
+
'Content-Type': lookup(file.filePath) ?? 'application/octet-stream',
|
|
104
|
+
'Content-Length': body.length,
|
|
105
|
+
// dev output changes on every rebuild, so never let the browser reuse it without asking
|
|
106
|
+
'Cache-Control': 'no-cache',
|
|
107
|
+
});
|
|
108
|
+
res.end(req.method === 'HEAD' ? undefined : body);
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=assets.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assets.js","sourceRoot":"","sources":["../src/assets.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAsBhC,SAAS,QAAQ,CAAC,UAA8B,EAAE,QAAgB;IACjE,OAAO,IAAI,OAAO,CAAU,CAAC,OAAO,EAAE,EAAE;QACvC,UAAU,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE;YAC1C,OAAO,CAAC,CAAC,KAAK,IAAI,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC;QAC7C,CAAC,CAAC,CAAC;IACJ,CAAC,CAAC,CAAC;AACJ,CAAC;AAED,SAAS,QAAQ,CAAC,UAA8B,EAAE,QAAgB;IACjE,OAAO,IAAI,OAAO,CAAS,CAAC,OAAO,EAAE,M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AC;gBACrB,OAAO;YACR,CAAC;YACD,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;YAC5D,GAAG,CAAC,SAAS,CAAC,MAAM,EAAE;gBACrB,cAAc,EAAE,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,0BAA0B;gBACnE,gBAAgB,EAAE,IAAI,CAAC,MAAM;gBAC7B,wFAAwF;gBACxF,eAAe,EAAE,UAAU;aAC3B,CAAC,CAAC;YACH,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,KAAK,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;QACnD,CAAC;KACD,CAAC;AACH,CAAC"}
|
package/dist/build.d.ts
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import type { ResolvedWorker } from './config.js';
|
|
2
|
+
import type { CloudflarePluginOptions } from './options.js';
|
|
3
|
+
import type { Compiler } from '@rspack/core';
|
|
4
|
+
/** The Wrangler config `wrangler deploy` reads for the built Worker, with every path rebased onto the output directory. */
|
|
5
|
+
export declare function createOutputConfig(worker: ResolvedWorker): {
|
|
6
|
+
assets: {
|
|
7
|
+
directory: string;
|
|
8
|
+
binding?: string;
|
|
9
|
+
html_handling?: "auto-trailing-slash" | "force-trailing-slash" | "drop-trailing-slash" | "none";
|
|
10
|
+
not_found_handling?: "single-page-application" | "404-page" | "none";
|
|
11
|
+
base_path?: string;
|
|
12
|
+
run_worker_first?: string[] | boolean;
|
|
13
|
+
} | undefined;
|
|
14
|
+
containers: {
|
|
15
|
+
name?: string;
|
|
16
|
+
instances?: number;
|
|
17
|
+
max_instances?: number;
|
|
18
|
+
image?: string;
|
|
19
|
+
images?: Record<string, {
|
|
20
|
+
dockerfile: string;
|
|
21
|
+
build_context?: string;
|
|
22
|
+
build_vars?: Record<string, string>;
|
|
23
|
+
image?: never;
|
|
24
|
+
} | {
|
|
25
|
+
image: string;
|
|
26
|
+
dockerfile?: never;
|
|
27
|
+
build_context?: never;
|
|
28
|
+
build_vars?: never;
|
|
29
|
+
}>;
|
|
30
|
+
image_build_context?: string;
|
|
31
|
+
image_vars?: Record<string, string>;
|
|
32
|
+
class_name?: string;
|
|
33
|
+
observability?: {
|
|
34
|
+
enabled?: boolean;
|
|
35
|
+
logs?: {
|
|
36
|
+
enabled?: boolean;
|
|
37
|
+
};
|
|
38
|
+
target_instance_percentage?: number;
|
|
39
|
+
target_instance_count?: number;
|
|
40
|
+
};
|
|
41
|
+
scheduling_policy?: "default" | "durable_object" | "moon" | "regional";
|
|
42
|
+
instance_type?: "dev" | "basic" | "standard" | "lite" | "standard-1" | "standard-2" | "standard-3" | "standard-4" | {
|
|
43
|
+
vcpu?: number;
|
|
44
|
+
memory_mib?: number;
|
|
45
|
+
disk_mb?: number;
|
|
46
|
+
};
|
|
47
|
+
ssh?: {
|
|
48
|
+
enabled: boolean;
|
|
49
|
+
port?: number;
|
|
50
|
+
};
|
|
51
|
+
wrangler_ssh?: {
|
|
52
|
+
enabled: boolean;
|
|
53
|
+
port?: number;
|
|
54
|
+
};
|
|
55
|
+
authorized_keys?: {
|
|
56
|
+
name: string;
|
|
57
|
+
public_key: string;
|
|
58
|
+
}[];
|
|
59
|
+
trusted_user_ca_keys?: {
|
|
60
|
+
name?: string;
|
|
61
|
+
public_key: string;
|
|
62
|
+
}[];
|
|
63
|
+
configuration?: {
|
|
64
|
+
image?: string;
|
|
65
|
+
labels?: {
|
|
66
|
+
name: string;
|
|
67
|
+
value: string;
|
|
68
|
+
}[];
|
|
69
|
+
secrets?: {
|
|
70
|
+
name: string;
|
|
71
|
+
type: "env";
|
|
72
|
+
secret: string;
|
|
73
|
+
}[];
|
|
74
|
+
disk?: {
|
|
75
|
+
size_mb: number;
|
|
76
|
+
};
|
|
77
|
+
vcpu?: number;
|
|
78
|
+
memory_mib?: number;
|
|
79
|
+
};
|
|
80
|
+
constraints?: {
|
|
81
|
+
regions?: ("ENAM" | "WNAM" | "EEUR" | "WEUR" | "APAC" | "SAM" | "ME" | "OC" | "AFR")[];
|
|
82
|
+
jurisdiction?: "eu" | "fedramp" | "us";
|
|
83
|
+
cities?: string[];
|
|
84
|
+
tier?: number;
|
|
85
|
+
tiers?: number[];
|
|
86
|
+
};
|
|
87
|
+
affinities?: {
|
|
88
|
+
colocation?: "datacenter";
|
|
89
|
+
hardware_generation?: "highest-overall-performance";
|
|
90
|
+
};
|
|
91
|
+
durable_objects?: {
|
|
92
|
+
namespace_id: string;
|
|
93
|
+
};
|
|
94
|
+
rollout_step_percentage?: number | number[];
|
|
95
|
+
rollout_kind?: "full_auto" | "none" | "full_manual";
|
|
96
|
+
rollout_active_grace_period?: number;
|
|
97
|
+
unsafe?: Record<string, unknown>;
|
|
98
|
+
}[] | undefined;
|
|
99
|
+
upload_source_maps: boolean;
|
|
100
|
+
main: string;
|
|
101
|
+
no_bundle: boolean;
|
|
102
|
+
rules: ({
|
|
103
|
+
type: "CompiledWasm" | "Data" | "Text" | "ESModule" | "CommonJS" | "PythonModule" | "PythonRequirement";
|
|
104
|
+
globs: string[];
|
|
105
|
+
fallthrough?: boolean;
|
|
106
|
+
} | {
|
|
107
|
+
type: string;
|
|
108
|
+
globs: string[];
|
|
109
|
+
})[];
|
|
110
|
+
} | {
|
|
111
|
+
assets: {
|
|
112
|
+
directory: string;
|
|
113
|
+
binding?: string;
|
|
114
|
+
html_handling?: "auto-trailing-slash" | "force-trailing-slash" | "drop-trailing-slash" | "none";
|
|
115
|
+
not_found_handling?: "single-page-application" | "404-page" | "none";
|
|
116
|
+
base_path?: string;
|
|
117
|
+
run_worker_first?: string[] | boolean;
|
|
118
|
+
} | undefined;
|
|
119
|
+
containers: {
|
|
120
|
+
name?: string;
|
|
121
|
+
instances?: number;
|
|
122
|
+
max_instances?: number;
|
|
123
|
+
image?: string;
|
|
124
|
+
images?: Record<string, {
|
|
125
|
+
dockerfile: string;
|
|
126
|
+
build_context?: string;
|
|
127
|
+
build_vars?: Record<string, string>;
|
|
128
|
+
image?: never;
|
|
129
|
+
} | {
|
|
130
|
+
image: string;
|
|
131
|
+
dockerfile?: never;
|
|
132
|
+
build_context?: never;
|
|
133
|
+
build_vars?: never;
|
|
134
|
+
}>;
|
|
135
|
+
image_build_context?: string;
|
|
136
|
+
image_vars?: Record<string, string>;
|
|
137
|
+
class_name?: string;
|
|
138
|
+
observability?: {
|
|
139
|
+
enabled?: boolean;
|
|
140
|
+
logs?: {
|
|
141
|
+
enabled?: boolean;
|
|
142
|
+
};
|
|
143
|
+
target_instance_percentage?: number;
|
|
144
|
+
target_instance_count?: number;
|
|
145
|
+
};
|
|
146
|
+
scheduling_policy?: "default" | "durable_object" | "moon" | "regional";
|
|
147
|
+
instance_type?: "dev" | "basic" | "standard" | "lite" | "standard-1" | "standard-2" | "standard-3" | "standard-4" | {
|
|
148
|
+
vcpu?: number;
|
|
149
|
+
memory_mib?: number;
|
|
150
|
+
disk_mb?: number;
|
|
151
|
+
};
|
|
152
|
+
ssh?: {
|
|
153
|
+
enabled: boolean;
|
|
154
|
+
port?: number;
|
|
155
|
+
};
|
|
156
|
+
wrangler_ssh?: {
|
|
157
|
+
enabled: boolean;
|
|
158
|
+
port?: number;
|
|
159
|
+
};
|
|
160
|
+
authorized_keys?: {
|
|
161
|
+
name: string;
|
|
162
|
+
public_key: string;
|
|
163
|
+
}[];
|
|
164
|
+
trusted_user_ca_keys?: {
|
|
165
|
+
name?: string;
|
|
166
|
+
public_key: string;
|
|
167
|
+
}[];
|
|
168
|
+
configuration?: {
|
|
169
|
+
image?: string;
|
|
170
|
+
labels?: {
|
|
171
|
+
name: string;
|
|
172
|
+
value: string;
|
|
173
|
+
}[];
|
|
174
|
+
secrets?: {
|
|
175
|
+
name: string;
|
|
176
|
+
type: "env";
|
|
177
|
+
secret: string;
|
|
178
|
+
}[];
|
|
179
|
+
disk?: {
|
|
180
|
+
size_mb: number;
|
|
181
|
+
};
|
|
182
|
+
vcpu?: number;
|
|
183
|
+
memory_mib?: number;
|
|
184
|
+
};
|
|
185
|
+
constraints?: {
|
|
186
|
+
regions?: ("ENAM" | "WNAM" | "EEUR" | "WEUR" | "APAC" | "SAM" | "ME" | "OC" | "AFR")[];
|
|
187
|
+
jurisdiction?: "eu" | "fedramp" | "us";
|
|
188
|
+
cities?: string[];
|
|
189
|
+
tier?: number;
|
|
190
|
+
tiers?: number[];
|
|
191
|
+
};
|
|
192
|
+
affinities?: {
|
|
193
|
+
colocation?: "datacenter";
|
|
194
|
+
hardware_generation?: "highest-overall-performance";
|
|
195
|
+
};
|
|
196
|
+
durable_objects?: {
|
|
197
|
+
namespace_id: string;
|
|
198
|
+
};
|
|
199
|
+
rollout_step_percentage?: number | number[];
|
|
200
|
+
rollout_kind?: "full_auto" | "none" | "full_manual";
|
|
201
|
+
rollout_active_grace_period?: number;
|
|
202
|
+
unsafe?: Record<string, unknown>;
|
|
203
|
+
}[] | undefined;
|
|
204
|
+
upload_source_maps: boolean;
|
|
205
|
+
main: undefined;
|
|
206
|
+
no_bundle?: undefined;
|
|
207
|
+
rules?: undefined;
|
|
208
|
+
};
|
|
209
|
+
/** Moves assets the Worker imported into its assets directory, once the client build has emitted (and maybe cleaned) it. */
|
|
210
|
+
export declare function copyImportedAssets(workers: ResolvedWorker[]): Promise<void>;
|
|
211
|
+
/** Builds every Worker, entry first, and writes the redirect `wrangler deploy` follows to the built configs. */
|
|
212
|
+
export declare function buildWorkers(options: {
|
|
213
|
+
compiler: Compiler;
|
|
214
|
+
workers: ResolvedWorker[];
|
|
215
|
+
pluginOptions: CloudflarePluginOptions;
|
|
216
|
+
}): Promise<void>;
|
package/dist/build.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import fs from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { unstable_getVarsForDev } from 'wrangler';
|
|
5
|
+
import { IMPORTED_ASSETS_DIR, createWorkerConfig } from './worker-config.js';
|
|
6
|
+
// readConfig bookkeeping and bundling settings that rspack has already applied, so deploy must not apply them again
|
|
7
|
+
const OMITTED_CONFIG_KEYS = new Set([
|
|
8
|
+
'configPath',
|
|
9
|
+
'userConfigPath',
|
|
10
|
+
'topLevelName',
|
|
11
|
+
'definedEnvironments',
|
|
12
|
+
'targetEnvironment',
|
|
13
|
+
'build',
|
|
14
|
+
'define',
|
|
15
|
+
'alias',
|
|
16
|
+
'tsconfig',
|
|
17
|
+
'find_additional_modules',
|
|
18
|
+
'base_dir',
|
|
19
|
+
'preserve_file_names',
|
|
20
|
+
]);
|
|
21
|
+
/** The Wrangler config `wrangler deploy` reads for the built Worker, with every path rebased onto the output directory. */
|
|
22
|
+
export function createOutputConfig(worker) {
|
|
23
|
+
const rebase = (filePath) => path.relative(worker.outputDir, path.resolve(worker.rootDir, filePath)).replaceAll('\\', '/') || '.';
|
|
24
|
+
const config = Object.fromEntries(Object.entries(worker.config).filter(([key]) => !OMITTED_CONFIG_KEYS.has(key)));
|
|
25
|
+
// readConfig always fills in `unsafe`, and wrangler warns about it being experimental even when empty
|
|
26
|
+
if (worker.config.unsafe && Object.keys(worker.config.unsafe).length === 0) {
|
|
27
|
+
delete config.unsafe;
|
|
28
|
+
}
|
|
29
|
+
// wrangler's defaults already cover .wasm and .bin beside the bundle; custom binary rules have to come along
|
|
30
|
+
const binaryRules = worker.config.rules.filter(rule => rule.type === 'CompiledWasm' || rule.type === 'Data');
|
|
31
|
+
const bundled = worker.main
|
|
32
|
+
? { main: 'index.js', no_bundle: true, rules: [{ type: 'ESModule', globs: ['**/*.js', '**/*.mjs'] }, ...binaryRules] }
|
|
33
|
+
: { main: undefined };
|
|
34
|
+
return {
|
|
35
|
+
...config,
|
|
36
|
+
...bundled,
|
|
37
|
+
assets: worker.assets ? { ...worker.config.assets, directory: rebase(worker.assets.directory) } : undefined,
|
|
38
|
+
containers: worker.config.containers?.map((container) => {
|
|
39
|
+
const rebased = { ...container };
|
|
40
|
+
// image is either a Dockerfile path or a registry reference; readConfig makes paths absolute, so only those need rebasing
|
|
41
|
+
if (container.image && path.isAbsolute(container.image)) {
|
|
42
|
+
rebased.image = rebase(container.image);
|
|
43
|
+
}
|
|
44
|
+
if (container.image_build_context) {
|
|
45
|
+
rebased.image_build_context = rebase(container.image_build_context);
|
|
46
|
+
}
|
|
47
|
+
return rebased;
|
|
48
|
+
}),
|
|
49
|
+
upload_source_maps: worker.config.upload_source_maps ?? true,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
async function runCompiler(compiler) {
|
|
53
|
+
try {
|
|
54
|
+
return await new Promise((resolve, reject) => {
|
|
55
|
+
compiler.run((error, stats) => {
|
|
56
|
+
if (error || !stats) {
|
|
57
|
+
reject(error ?? new Error('Worker build produced no stats'));
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
resolve(stats);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
finally {
|
|
66
|
+
await new Promise((resolve) => {
|
|
67
|
+
compiler.close(() => resolve());
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/** The local secrets for this environment, written beside the build so `wrangler dev` can preview it like the Vite plugin's `vite preview`. */
|
|
72
|
+
function createDevVars(worker) {
|
|
73
|
+
const vars = unstable_getVarsForDev(worker.configPath, undefined, {}, worker.environment, true, worker.config.secrets);
|
|
74
|
+
const lines = Object.entries(vars).map(([key, binding]) => `${key}=${JSON.stringify(String(binding.value))}`);
|
|
75
|
+
return lines.length > 0 ? `${lines.join('\n')}\n` : undefined;
|
|
76
|
+
}
|
|
77
|
+
/** Bundles one Worker into its output directory beside the `wrangler.json` that deploys it, returning that config's path. */
|
|
78
|
+
async function buildWorker(compiler, worker, pluginOptions) {
|
|
79
|
+
if (worker.main) {
|
|
80
|
+
const { config } = createWorkerConfig({
|
|
81
|
+
compiler,
|
|
82
|
+
worker,
|
|
83
|
+
pluginOptions,
|
|
84
|
+
outputDir: worker.outputDir,
|
|
85
|
+
entry: worker.main,
|
|
86
|
+
mode: compiler.options.mode === 'production' ? 'production' : 'development',
|
|
87
|
+
});
|
|
88
|
+
const workerCompiler = compiler.rspack.rspack(config);
|
|
89
|
+
const stats = await runCompiler(workerCompiler);
|
|
90
|
+
if (stats.hasErrors()) {
|
|
91
|
+
throw new Error(`Worker "${worker.name}" build failed:\n${stats.toString({ preset: 'errors-only', colors: false })}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
await fs.rm(worker.outputDir, { recursive: true, force: true });
|
|
96
|
+
await fs.mkdir(worker.outputDir, { recursive: true });
|
|
97
|
+
}
|
|
98
|
+
const outputConfigPath = path.join(worker.outputDir, 'wrangler.json');
|
|
99
|
+
await fs.writeFile(outputConfigPath, JSON.stringify(createOutputConfig(worker), null, '\t'));
|
|
100
|
+
const devVars = createDevVars(worker);
|
|
101
|
+
if (devVars) {
|
|
102
|
+
await fs.writeFile(path.join(worker.outputDir, '.dev.vars'), devVars);
|
|
103
|
+
}
|
|
104
|
+
return outputConfigPath;
|
|
105
|
+
}
|
|
106
|
+
/** Moves assets the Worker imported into its assets directory, once the client build has emitted (and maybe cleaned) it. */
|
|
107
|
+
export async function copyImportedAssets(workers) {
|
|
108
|
+
for (const worker of workers) {
|
|
109
|
+
const source = path.join(worker.outputDir, IMPORTED_ASSETS_DIR);
|
|
110
|
+
if (!worker.assets || !existsSync(source)) {
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
await fs.cp(source, worker.assets.directory, { recursive: true });
|
|
114
|
+
await fs.rm(source, { recursive: true, force: true });
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/** Builds every Worker, entry first, and writes the redirect `wrangler deploy` follows to the built configs. */
|
|
118
|
+
export async function buildWorkers(options) {
|
|
119
|
+
const { compiler, workers, pluginOptions } = options;
|
|
120
|
+
const [entry] = workers;
|
|
121
|
+
if (!entry) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const configPaths = [];
|
|
125
|
+
for (const worker of workers) {
|
|
126
|
+
configPaths.push(await buildWorker(compiler, worker, pluginOptions));
|
|
127
|
+
}
|
|
128
|
+
// the same redirect @cloudflare/vite-plugin writes, so a plain `wrangler deploy` next to the user config deploys the build
|
|
129
|
+
const deployConfigPath = path.join(entry.rootDir, '.wrangler', 'deploy', 'config.json');
|
|
130
|
+
const relativeToDeploy = (filePath) => path.relative(path.dirname(deployConfigPath), filePath).replaceAll('\\', '/');
|
|
131
|
+
const [entryConfigPath, ...auxiliaryConfigPaths] = configPaths;
|
|
132
|
+
await fs.mkdir(path.dirname(deployConfigPath), { recursive: true });
|
|
133
|
+
await fs.writeFile(deployConfigPath, JSON.stringify({
|
|
134
|
+
configPath: entryConfigPath && relativeToDeploy(entryConfigPath),
|
|
135
|
+
auxiliaryWorkers: auxiliaryConfigPaths.map((configPath) => {
|
|
136
|
+
return { configPath: relativeToDeploy(configPath) };
|
|
137
|
+
}),
|
|
138
|
+
}));
|
|
139
|
+
}
|
|
140
|
+
//# sourceMappingURL=build.js.map
|