Skip to content

Commit 8b925c9

Browse files
committed
fix(vite-plugin-cloudflare): add experimental local mode support for image bindings
1 parent b59e3e1 commit 8b925c9

File tree

9 files changed

+53
-1
lines changed

9 files changed

+53
-1
lines changed

.changeset/wild-paws-occur.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@cloudflare/vite-plugin": patch
3+
---
4+
5+
fix: add experimental local mode support for image bindings
6+
7+
You can now enable experimental local image binding support in the Vite plugin by setting the `experimental.imagesLocalMode` option to `true`. This provides the same functionality as running `wrangler dev --experimental-images-local-mode`.

packages/vite-plugin-cloudflare/playground/bindings/__tests__/worker.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ test("kv_namspaces support", async () => {
66
expect(response).toBe("KV binding works");
77
});
88

9+
test("experimental images local mode support", async () => {
10+
const response = await getTextResponse("/images-local-mode");
11+
expect(response).toBe("Local images binding works");
12+
});
13+
914
test("unsafe_hello_world support", async () => {
1015
const response = await getTextResponse("/hello-world");
1116
expect(response).toBe("Hello World binding works");
94.3 KB
Loading

packages/vite-plugin-cloudflare/playground/bindings/src/index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import image from "./image.png?inline";
2+
13
export default {
24
async fetch(request, env) {
35
const url = new URL(request.url);
@@ -18,6 +20,30 @@ export default {
1820
status: 200,
1921
});
2022
}
23+
case "/images-local-mode": {
24+
const request = await fetch(image);
25+
26+
if (!request.body) {
27+
return new Response("Failed to fetch image", { status: 500 });
28+
}
29+
30+
try {
31+
await env.IMAGES.input(request.body).output({ format: "image/gif" });
32+
} catch (e) {
33+
if (
34+
e instanceof Error &&
35+
e.message.includes("GIF output is not supported in local mode")
36+
) {
37+
return new Response("Local images binding works", {
38+
status: 200,
39+
});
40+
}
41+
}
42+
43+
return new Response("Local images binding is not configured properly", {
44+
status: 500,
45+
});
46+
}
2147
case "/ae": {
2248
await env.WAE.writeDataPoint({ doubles: [2, 3] });
2349

packages/vite-plugin-cloudflare/playground/bindings/vite.config.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,11 @@ import { cloudflare } from "@cloudflare/vite-plugin";
22
import { defineConfig } from "vite";
33

44
export default defineConfig({
5-
plugins: [cloudflare({ inspectorPort: false, persistState: false })],
5+
plugins: [
6+
cloudflare({
7+
inspectorPort: false,
8+
persistState: false,
9+
experimental: { imagesLocalMode: true },
10+
}),
11+
],
612
});

packages/vite-plugin-cloudflare/playground/bindings/worker-configuration.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ declare namespace Cloudflare {
44
interface Env {
55
KV: KVNamespace;
66
HELLO_WORLD: HelloWorldBinding;
7+
IMAGES: ImagesBinding;
78
WAE: AnalyticsEngineDataset;
89
RATE_LIMITER: RateLimit;
910
}

packages/vite-plugin-cloudflare/playground/bindings/wrangler.jsonc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
"id": "test-kv-id",
1010
},
1111
],
12+
"images": {
13+
"binding": "IMAGES",
14+
},
1215
"unsafe_hello_world": [
1316
{
1417
"binding": "HELLO_WORLD",

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,8 @@ export async function getDevMiniflareOptions(config: {
421421
},
422422
resolvedPluginConfig.cloudflareEnv,
423423
{
424+
imagesLocalMode:
425+
resolvedPluginConfig.experimental.imagesLocalMode,
424426
remoteProxyConnectionString:
425427
remoteProxySessionData?.session
426428
?.remoteProxyConnectionString,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ interface Experimental {
3737
headersAndRedirectsDevModeSupport?: boolean;
3838
/** Experimental support for remote bindings (where bindings configured with `remote: true` access remote resources). */
3939
remoteBindings?: boolean;
40+
/** Experimental support for local images bindings */
41+
imagesLocalMode?: boolean;
4042
}
4143

4244
export interface PluginConfig extends EntryWorkerConfig {

0 commit comments

Comments
 (0)