rsbuild-plugin-cloudflare 0.0.6 → 0.0.8

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.
@@ -0,0 +1,73 @@
1
+ import "./constants-BSTDVDmV.mjs";
2
+ import { getDevMiniflareOptions as getDevMiniflareOptions$1 } from "./miniflare-options-CXTX1Ld-.mjs";
3
+ import { Response } from "miniflare";
4
+ // src/miniflare-options.ts
5
+ /**
6
+ * Gets the Vite plugin's dev Miniflare options with the user Workers running
7
+ * the Rsbuild bundles instead of Vite's module runner, and with the asset
8
+ * Worker reading HTML pages from Rsbuild instead of Vite.
9
+ *
10
+ * @param ctx The plugin context.
11
+ * @param viteDevServer The Vite dev server backed by Rsbuild's.
12
+ * @param options.manifests The Workers' bundles, keyed by Worker name.
13
+ * @param options.html Reads the dev server's HTML pages.
14
+ * @returns The Miniflare options and the Containers to prepare.
15
+ */
16
+ async function getDevMiniflareOptions(ctx, viteDevServer, { manifests, html }) {
17
+ const { miniflareOptions, containerOptions } = await getDevMiniflareOptions$1(ctx, viteDevServer);
18
+ const { unsafeModuleFallbackService: _, ...options } = miniflareOptions;
19
+ return {
20
+ containerOptions,
21
+ miniflareOptions: {
22
+ ...options,
23
+ workers: options.workers.map((worker) => {
24
+ if (worker.config.name === "__asset-worker__") return withHtmlHandlers(worker, html);
25
+ const manifest = manifests.get(worker.config.name ?? "");
26
+ if (!manifest) return worker;
27
+ const { __VITE_INVOKE_MODULE__: _invokeModule, __VITE_RUNNER_OBJECT__: _runnerBinding, ...env } = worker.config.env ?? {};
28
+ const { __VITE_RUNNER_OBJECT__: _runnerObject, ...exports } = worker.config.exports ?? {};
29
+ const { useModuleFallbackService: _useModuleFallbackService, unsafeEvalBinding: _unsafeEvalBinding, ...dev } = worker.dev ?? {};
30
+ return {
31
+ config: {
32
+ ...worker.config,
33
+ env,
34
+ exports,
35
+ manifest
36
+ },
37
+ dev
38
+ };
39
+ })
40
+ }
41
+ };
42
+ }
43
+ /** Replaces the asset Worker's bindings that read HTML pages from Vite. */
44
+ function withHtmlHandlers(worker, html) {
45
+ return {
46
+ ...worker,
47
+ config: {
48
+ ...worker.config,
49
+ env: {
50
+ ...worker.config.env,
51
+ __VITE_HTML_EXISTS__: {
52
+ type: "fetcher",
53
+ async handler(request) {
54
+ const { pathname } = new URL(request.url);
55
+ return Response.json(pathname.endsWith(".html") && await html.htmlExists(pathname) ? pathname : null);
56
+ }
57
+ },
58
+ __VITE_FETCH_HTML__: {
59
+ type: "fetcher",
60
+ async handler(request) {
61
+ const { pathname } = new URL(request.url);
62
+ try {
63
+ return new Response(await html.fetchHtml(pathname), { headers: { "Content-Type": "text/html" } });
64
+ } catch (error) {
65
+ throw new Error(`Unexpected error. Failed to load "${pathname}".`, { cause: error });
66
+ }
67
+ }
68
+ }
69
+ }
70
+ }
71
+ };
72
+ }
73
+ export { getDevMiniflareOptions };