Skip to content

Commit a061a10

Browse files
add remoteBindings option to vite plugin
1 parent 0777e3b commit a061a10

File tree

11 files changed

+149
-14
lines changed

11 files changed

+149
-14
lines changed

.changeset/cyan-pets-prove.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/vite-plugin": minor
3+
---
4+
5+
add a `remoteBindings` option to allow the disabling of remote bindings
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "@cloudflare/vite-plugin-e2e-remote-bindings-disabled",
3+
"version": "0.0.0",
4+
"private": true,
5+
"type": "module",
6+
"scripts": {
7+
"build": "vite build",
8+
"buildAndPreview": "vite build && vite preview",
9+
"dev": "vite",
10+
"lint": "eslint .",
11+
"preview": "vite preview"
12+
},
13+
"devDependencies": {
14+
"@cloudflare/vite-plugin": "*",
15+
"@cloudflare/workers-types": "^4.20250204.0",
16+
"@eslint/js": "^9.19.0",
17+
"eslint": "^9.19.0",
18+
"eslint-plugin-react-hooks": "^5.0.0",
19+
"eslint-plugin-react-refresh": "^0.4.18",
20+
"globals": "^15.14.0",
21+
"typescript": "~5.7.2",
22+
"typescript-eslint": "^8.22.0",
23+
"vite": "^6.1.0"
24+
}
25+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default {
2+
async fetch(_req, env) {
3+
const content = await env.AI.run("@cf/meta/llama-2-7b-chat-fp16", {
4+
messages: [],
5+
});
6+
7+
return Response.json({
8+
response: content.response,
9+
});
10+
},
11+
} satisfies ExportedHandler<{ AI: Ai }>;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"files": [],
3+
"references": [
4+
{ "path": "./tsconfig.node.json" },
5+
{ "path": "./tsconfig.worker.json" }
6+
]
7+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"compilerOptions": {
3+
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
4+
"target": "ES2022",
5+
"lib": ["ES2023"],
6+
"module": "ESNext",
7+
"skipLibCheck": true,
8+
9+
/* Bundler mode */
10+
"moduleResolution": "bundler",
11+
"allowImportingTsExtensions": true,
12+
"isolatedModules": true,
13+
"moduleDetection": "force",
14+
"noEmit": true,
15+
16+
/* Linting */
17+
"strict": true,
18+
"noUnusedLocals": true,
19+
"noUnusedParameters": true,
20+
"noFallthroughCasesInSwitch": true,
21+
"noUncheckedSideEffectImports": true
22+
},
23+
"include": ["vite.config.ts"]
24+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "./tsconfig.node.json",
3+
"compilerOptions": {
4+
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.worker.tsbuildinfo",
5+
"types": ["@cloudflare/workers-types/2023-07-01", "vite/client"]
6+
},
7+
"include": ["src"]
8+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { cloudflare } from "@cloudflare/vite-plugin";
2+
import { defineConfig } from "vite";
3+
4+
export default defineConfig({
5+
plugins: [
6+
cloudflare({
7+
configPath: "./wrangler.jsonc",
8+
inspectorPort: false,
9+
remoteBindings: false,
10+
persistState: false,
11+
}),
12+
],
13+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "cloudflare-vite-e2e-remote-bindings-disabled",
3+
"main": "./src/index.ts",
4+
"compatibility_date": "2024-12-30",
5+
"compatibility_flags": ["nodejs_compat"],
6+
"ai": {
7+
"binding": "AI",
8+
"remote": true,
9+
},
10+
}

packages/vite-plugin-cloudflare/e2e/remote-bindings.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,24 @@ if (!process.env.CLOUDFLARE_ACCOUNT_ID || !process.env.CLOUDFLARE_API_TOKEN) {
138138
});
139139
});
140140
}
141+
142+
describe("remote bindings disabled", () => {
143+
const projectPath = seed("remote-bindings-disabled", "pnpm");
144+
145+
describe.each(commands)('with "%s" command', (command) => {
146+
test.skipIf(isBuildAndPreviewOnWindows(command))(
147+
"cannot connect to remote bindings",
148+
async ({ expect }) => {
149+
const proc = await runLongLived("pnpm", command, projectPath);
150+
const url = await waitForReady(proc);
151+
152+
const response = await fetch(url);
153+
154+
const responseText = await response.text();
155+
156+
expect(responseText).toContain("Error");
157+
expect(responseText).toContain("Binding AI needs to be run remotely");
158+
}
159+
);
160+
});
161+
});

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

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -413,13 +413,16 @@ export async function getDevMiniflareOptions(config: {
413413
: undefined;
414414

415415
const remoteProxySessionData =
416-
await maybeStartOrUpdateRemoteProxySession(
417-
{
418-
name: workerConfig.name,
419-
bindings: bindings ?? {},
420-
},
421-
preExistingRemoteProxySession ?? null
422-
);
416+
!resolvedPluginConfig.remoteBindingsEnabled
417+
? // if remote bindings are not enabled then the proxy session can simply be null
418+
null
419+
: await maybeStartOrUpdateRemoteProxySession(
420+
{
421+
name: workerConfig.name,
422+
bindings: bindings ?? {},
423+
},
424+
preExistingRemoteProxySession ?? null
425+
);
423426

424427
if (workerConfig.configPath && remoteProxySessionData) {
425428
remoteProxySessionsDataMap.set(
@@ -739,13 +742,16 @@ export async function getPreviewMiniflareOptions(config: {
739742
: undefined;
740743

741744
const remoteProxySessionData =
742-
await maybeStartOrUpdateRemoteProxySession(
743-
{
744-
name: workerConfig.name,
745-
bindings: bindings ?? {},
746-
},
747-
preExistingRemoteProxySessionData ?? null
748-
);
745+
!resolvedPluginConfig.remoteBindingsEnabled
746+
? // if remote bindings are not enabled then the proxy session can simply be null
747+
null
748+
: await maybeStartOrUpdateRemoteProxySession(
749+
{
750+
name: workerConfig.name,
751+
bindings: bindings ?? {},
752+
},
753+
preExistingRemoteProxySessionData ?? null
754+
);
749755

750756
if (workerConfig.configPath && remoteProxySessionData) {
751757
remoteProxySessionsDataMap.set(

0 commit comments

Comments
 (0)