Skip to content

Commit 9c5601a

Browse files
authored
fix(vite-plugin-cloudflare): allow "invoke" other than fetchModule (#11123)
* fix(vite-plugin-cloudflare): allow "invoke" other than `fetchModule` * chore: add changeset * refactor: remove unused `FetchFunctionOptions`
1 parent 14eb3d0 commit 9c5601a

File tree

3 files changed

+22
-26
lines changed

3 files changed

+22
-26
lines changed

.changeset/dry-women-cry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/vite-plugin": patch
3+
---
4+
5+
Fix Vite 7.2 compatibility.

packages/vite-plugin-cloudflare/src/cloudflare-environment.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import assert from "node:assert";
22
import * as util from "node:util";
33
import * as vite from "vite";
4+
import { additionalModuleRE } from "./plugins/additional-modules";
45
import { VIRTUAL_WORKER_ENTRY } from "./plugins/virtual-modules";
56
import {
67
INIT_PATH,
@@ -17,6 +18,7 @@ import type {
1718
ReplaceWorkersTypes,
1819
WebSocket,
1920
} from "miniflare";
21+
import type { FetchFunctionOptions } from "vite/module-runner";
2022

2123
export const MAIN_ENTRY_NAME = "index";
2224

@@ -121,6 +123,21 @@ export class CloudflareDevEnvironment extends vite.DevEnvironment {
121123
webSocket.accept();
122124
this.#webSocketContainer.webSocket = webSocket;
123125
}
126+
127+
override async fetchModule(
128+
id: string,
129+
importer?: string,
130+
options?: FetchFunctionOptions
131+
): Promise<vite.FetchResult> {
132+
// Additional modules (CompiledWasm, Data, Text)
133+
if (additionalModuleRE.test(id)) {
134+
return {
135+
externalize: id,
136+
type: "module",
137+
};
138+
}
139+
return super.fetchModule(id, importer, options);
140+
}
124141
}
125142

126143
export const cloudflareBuiltInModules = [

packages/vite-plugin-cloudflare/src/miniflare-options.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import type {
4646
WorkerdStructuredLog,
4747
WorkerOptions,
4848
} from "miniflare";
49-
import type { FetchFunctionOptions } from "vite/module-runner";
5049
import type {
5150
Binding,
5251
RemoteProxySession,
@@ -506,36 +505,11 @@ export async function getDevMiniflareOptions(config: {
506505
__VITE_INVOKE_MODULE__: async (request) => {
507506
const payload =
508507
(await request.json()) as vite.CustomPayload;
509-
const invokePayloadData = payload.data as {
510-
id: string;
511-
name: string;
512-
data: [string, string, FetchFunctionOptions];
513-
};
514-
515-
assert(
516-
invokePayloadData.name === "fetchModule",
517-
`Invalid invoke event: ${invokePayloadData.name}`
518-
);
519-
520-
const [moduleId] = invokePayloadData.data;
521-
522-
// Additional modules (CompiledWasm, Data, Text)
523-
if (additionalModuleRE.test(moduleId)) {
524-
const result = {
525-
externalize: moduleId,
526-
type: "module",
527-
} satisfies vite.FetchResult;
528-
529-
return MiniflareResponse.json({ result });
530-
}
531-
532508
const devEnvironment = viteDevServer.environments[
533509
environmentName
534510
] as CloudflareDevEnvironment;
535-
536511
const result =
537512
await devEnvironment.hot.handleInvoke(payload);
538-
539513
return MiniflareResponse.json(result);
540514
},
541515
},

0 commit comments

Comments
 (0)