From 964055cef6a11e822f7ac309533a18e5a012a0b8 Mon Sep 17 00:00:00 2001 From: Edmund Hung Date: Tue, 16 Sep 2025 16:25:34 +0100 Subject: [PATCH 1/7] fix(vite-plugin-cloudflare): populate build time env var from .env --- .../vite-plugin-cloudflare/src/plugin-config.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/vite-plugin-cloudflare/src/plugin-config.ts b/packages/vite-plugin-cloudflare/src/plugin-config.ts index d57093164388..eb346a2dd5b2 100644 --- a/packages/vite-plugin-cloudflare/src/plugin-config.ts +++ b/packages/vite-plugin-cloudflare/src/plugin-config.ts @@ -122,12 +122,16 @@ export function resolvePluginConfig( } const configPaths = new Set(); - const { CLOUDFLARE_ENV: cloudflareEnv } = vite.loadEnv( - viteEnv.mode, - root, - /* prefixes */ "" - ); + const buildTimeEnv = vite.loadEnv(viteEnv.mode, root, [ + "CLOUDFLARE_", + "WRANGLER_", + ]); + + // Merge the loaded env variables into process.env so that they are available to + // wrangler when it loads the worker configuration files. + Object.assign(process.env, buildTimeEnv); + const cloudflareEnv = buildTimeEnv.CLOUDFLARE_ENV; const entryWorkerConfigPath = getValidatedWranglerConfigPath( root, pluginConfig.configPath From 2be3a17516fcf0f638d23798f23986a17efbcdfc Mon Sep 17 00:00:00 2001 From: Edmund Hung Date: Tue, 16 Sep 2025 16:43:45 +0100 Subject: [PATCH 2/7] populate env var earlier to support vite preview --- .../vite-plugin-cloudflare/src/plugin-config.ts | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/packages/vite-plugin-cloudflare/src/plugin-config.ts b/packages/vite-plugin-cloudflare/src/plugin-config.ts index eb346a2dd5b2..115fccb1888e 100644 --- a/packages/vite-plugin-cloudflare/src/plugin-config.ts +++ b/packages/vite-plugin-cloudflare/src/plugin-config.ts @@ -112,6 +112,14 @@ export function resolvePluginConfig( experimental: pluginConfig.experimental ?? {}, }; const root = userConfig.root ? path.resolve(userConfig.root) : process.cwd(); + const buildTimeEnv = vite.loadEnv(viteEnv.mode, root, [ + "CLOUDFLARE_", + "WRANGLER_", + ]); + + // Merge the loaded env variables into process.env so that they are available to + // wrangler when it loads the worker configuration files. + Object.assign(process.env, buildTimeEnv); if (viteEnv.isPreview) { return { @@ -122,15 +130,6 @@ export function resolvePluginConfig( } const configPaths = new Set(); - const buildTimeEnv = vite.loadEnv(viteEnv.mode, root, [ - "CLOUDFLARE_", - "WRANGLER_", - ]); - - // Merge the loaded env variables into process.env so that they are available to - // wrangler when it loads the worker configuration files. - Object.assign(process.env, buildTimeEnv); - const cloudflareEnv = buildTimeEnv.CLOUDFLARE_ENV; const entryWorkerConfigPath = getValidatedWranglerConfigPath( root, From 8584d7b13be25f774737e43ea3234c56900f27c8 Mon Sep 17 00:00:00 2001 From: Edmund Hung Date: Tue, 16 Sep 2025 18:24:52 +0100 Subject: [PATCH 3/7] add test --- .../vite-plugin-cloudflare/playground/bindings/.env | 2 ++ .../playground/bindings/__tests__/worker.spec.ts | 5 +++++ .../playground/bindings/src/index.ts | 12 ++++++++++++ .../playground/bindings/worker-configuration.d.ts | 1 + .../playground/bindings/wrangler.jsonc | 6 ++++++ 5 files changed, 26 insertions(+) create mode 100644 packages/vite-plugin-cloudflare/playground/bindings/.env diff --git a/packages/vite-plugin-cloudflare/playground/bindings/.env b/packages/vite-plugin-cloudflare/playground/bindings/.env new file mode 100644 index 000000000000..71d52fc5fe38 --- /dev/null +++ b/packages/vite-plugin-cloudflare/playground/bindings/.env @@ -0,0 +1,2 @@ +# To test whether vite plugin could configure the hyperdrive binding properly +WRANGLER_HYPERDRIVE_LOCAL_CONNECTION_STRING_HYPERDRIVE="postgres://user:password@example.com:3456/testdb" \ No newline at end of file diff --git a/packages/vite-plugin-cloudflare/playground/bindings/__tests__/worker.spec.ts b/packages/vite-plugin-cloudflare/playground/bindings/__tests__/worker.spec.ts index dd54c1f5c9a4..b3bc399c571d 100644 --- a/packages/vite-plugin-cloudflare/playground/bindings/__tests__/worker.spec.ts +++ b/packages/vite-plugin-cloudflare/playground/bindings/__tests__/worker.spec.ts @@ -25,3 +25,8 @@ test("ratelimit support", async () => { const response = await getTextResponse("/rate-limit"); expect(response).toBe("Rate limit binding works: first: true, second: false"); }); + +test("hyperdrive support", async () => { + const response = await getTextResponse("/hyperdrive"); + expect(response).toBe("Hyperdrive binding works"); +}); diff --git a/packages/vite-plugin-cloudflare/playground/bindings/src/index.ts b/packages/vite-plugin-cloudflare/playground/bindings/src/index.ts index a4e5283ce1ea..4520a0cdebb5 100644 --- a/packages/vite-plugin-cloudflare/playground/bindings/src/index.ts +++ b/packages/vite-plugin-cloudflare/playground/bindings/src/index.ts @@ -61,6 +61,18 @@ export default { } ); } + case "/hyperdrive": { + if ( + typeof env.HYPERDRIVE.connect !== "function" || + typeof env.HYPERDRIVE.connectionString !== "string" + ) { + return new Response("Hyperdrive binding is not configured properly", { + status: 500, + }); + } + + return new Response("Hyperdrive binding works"); + } case "/hello-world": { const value = Math.floor(Date.now() * Math.random()).toString(36); await env.HELLO_WORLD.set(value); diff --git a/packages/vite-plugin-cloudflare/playground/bindings/worker-configuration.d.ts b/packages/vite-plugin-cloudflare/playground/bindings/worker-configuration.d.ts index 5ff75b054ab0..69686c53bb66 100644 --- a/packages/vite-plugin-cloudflare/playground/bindings/worker-configuration.d.ts +++ b/packages/vite-plugin-cloudflare/playground/bindings/worker-configuration.d.ts @@ -7,6 +7,7 @@ declare namespace Cloudflare { IMAGES: ImagesBinding; WAE: AnalyticsEngineDataset; RATE_LIMITER: RateLimit; + HYPERDRIVE: Hyperdrive; } } interface Env extends Cloudflare.Env {} diff --git a/packages/vite-plugin-cloudflare/playground/bindings/wrangler.jsonc b/packages/vite-plugin-cloudflare/playground/bindings/wrangler.jsonc index e1f3f91354f8..ec364f8ce90d 100644 --- a/packages/vite-plugin-cloudflare/playground/bindings/wrangler.jsonc +++ b/packages/vite-plugin-cloudflare/playground/bindings/wrangler.jsonc @@ -23,6 +23,12 @@ "binding": "WAE", }, ], + "hyperdrive": [ + { + "binding": "HYPERDRIVE", + "id": "test-hyperdrive-id", + }, + ], "unsafe": { "bindings": [ { From 1004c0de789ee2bc7621b9d652083f14febd2f50 Mon Sep 17 00:00:00 2001 From: Edmund Hung Date: Tue, 16 Sep 2025 18:27:57 +0100 Subject: [PATCH 4/7] fixup! add test --- packages/vite-plugin-cloudflare/playground/bindings/.env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite-plugin-cloudflare/playground/bindings/.env b/packages/vite-plugin-cloudflare/playground/bindings/.env index 71d52fc5fe38..2b39ae7b41fb 100644 --- a/packages/vite-plugin-cloudflare/playground/bindings/.env +++ b/packages/vite-plugin-cloudflare/playground/bindings/.env @@ -1,2 +1,2 @@ # To test whether vite plugin could configure the hyperdrive binding properly -WRANGLER_HYPERDRIVE_LOCAL_CONNECTION_STRING_HYPERDRIVE="postgres://user:password@example.com:3456/testdb" \ No newline at end of file +WRANGLER_HYPERDRIVE_LOCAL_CONNECTION_STRING_HYPERDRIVE="postgres://user:password@example.com:3456/testdb" From 25fe5b5a2f0665632be9484405b025e04ca0d738 Mon Sep 17 00:00:00 2001 From: Edmund Hung Date: Wed, 17 Sep 2025 11:47:34 +0100 Subject: [PATCH 5/7] add changeset --- .changeset/beige-results-flow.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/beige-results-flow.md diff --git a/.changeset/beige-results-flow.md b/.changeset/beige-results-flow.md new file mode 100644 index 000000000000..835dd03949ec --- /dev/null +++ b/.changeset/beige-results-flow.md @@ -0,0 +1,5 @@ +--- +"@cloudflare/vite-plugin": patch +--- + +fix: populate build time env var from `.env` From d2ab32d64ed45f194dff81f999e683e2e6797e45 Mon Sep 17 00:00:00 2001 From: Edmund Hung Date: Fri, 19 Sep 2025 14:43:39 +0100 Subject: [PATCH 6/7] address feedback --- .changeset/beige-results-flow.md | 8 +++++++- packages/vite-plugin-cloudflare/src/plugin-config.ts | 9 +++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/.changeset/beige-results-flow.md b/.changeset/beige-results-flow.md index 835dd03949ec..3425c18594ad 100644 --- a/.changeset/beige-results-flow.md +++ b/.changeset/beige-results-flow.md @@ -2,4 +2,10 @@ "@cloudflare/vite-plugin": patch --- -fix: populate build time env var from `.env` +Support Hyperdrive local connection strings from `.env` files + +You can now define your Hyperdrive local connection string in a `.env` file using the `CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_` variable. + +```sh +CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_PROD_DB="postgres://user:password@127.0.0.1:5432/testdb" +``` diff --git a/packages/vite-plugin-cloudflare/src/plugin-config.ts b/packages/vite-plugin-cloudflare/src/plugin-config.ts index 115fccb1888e..136c2200704f 100644 --- a/packages/vite-plugin-cloudflare/src/plugin-config.ts +++ b/packages/vite-plugin-cloudflare/src/plugin-config.ts @@ -112,14 +112,15 @@ export function resolvePluginConfig( experimental: pluginConfig.experimental ?? {}, }; const root = userConfig.root ? path.resolve(userConfig.root) : process.cwd(); - const buildTimeEnv = vite.loadEnv(viteEnv.mode, root, [ + const prefixedEnv = vite.loadEnv(viteEnv.mode, root, [ "CLOUDFLARE_", - "WRANGLER_", + // TODO: Remove deprecated WRANGLER prefix support in next major version + "WRANGLER_HYPERDRIVE_LOCAL_CONNECTION_STRING_", ]); // Merge the loaded env variables into process.env so that they are available to // wrangler when it loads the worker configuration files. - Object.assign(process.env, buildTimeEnv); + Object.assign(process.env, prefixedEnv); if (viteEnv.isPreview) { return { @@ -130,7 +131,7 @@ export function resolvePluginConfig( } const configPaths = new Set(); - const cloudflareEnv = buildTimeEnv.CLOUDFLARE_ENV; + const cloudflareEnv = prefixedEnv.CLOUDFLARE_ENV; const entryWorkerConfigPath = getValidatedWranglerConfigPath( root, pluginConfig.configPath From 942fd7e0b45d6d4567ba2db09e1cf2140783a351 Mon Sep 17 00:00:00 2001 From: Edmund Hung Date: Mon, 22 Sep 2025 13:09:23 +0100 Subject: [PATCH 7/7] Update packages/vite-plugin-cloudflare/playground/bindings/.env Co-authored-by: James Opstad <13586373+jamesopstad@users.noreply.github.com> --- packages/vite-plugin-cloudflare/playground/bindings/.env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite-plugin-cloudflare/playground/bindings/.env b/packages/vite-plugin-cloudflare/playground/bindings/.env index 2b39ae7b41fb..435034d90afa 100644 --- a/packages/vite-plugin-cloudflare/playground/bindings/.env +++ b/packages/vite-plugin-cloudflare/playground/bindings/.env @@ -1,2 +1,2 @@ # To test whether vite plugin could configure the hyperdrive binding properly -WRANGLER_HYPERDRIVE_LOCAL_CONNECTION_STRING_HYPERDRIVE="postgres://user:password@example.com:3456/testdb" +CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_HYPERDRIVE="postgres://user:password@example.com:3456/testdb"