Skip to content

Commit d7aa0ae

Browse files
authored
fix(vite-plugin-cloudflare): populate Hyperdrive local connection strings from .env (#10677)
1 parent a434352 commit d7aa0ae

File tree

7 files changed

+47
-6
lines changed

7 files changed

+47
-6
lines changed

.changeset/beige-results-flow.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"@cloudflare/vite-plugin": patch
3+
---
4+
5+
Support Hyperdrive local connection strings from `.env` files
6+
7+
You can now define your Hyperdrive local connection string in a `.env` file using the `CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_<BINDING_NAME>` variable.
8+
9+
```sh
10+
CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_PROD_DB="postgres://user:[email protected]:5432/testdb"
11+
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# To test whether vite plugin could configure the hyperdrive binding properly
2+
CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_HYPERDRIVE="postgres://user:[email protected]:3456/testdb"

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,8 @@ test("ratelimit support", async () => {
2525
const response = await getTextResponse("/rate-limit");
2626
expect(response).toBe("Rate limit binding works: first: true, second: false");
2727
});
28+
29+
test("hyperdrive support", async () => {
30+
const response = await getTextResponse("/hyperdrive");
31+
expect(response).toBe("Hyperdrive binding works");
32+
});

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ export default {
6161
}
6262
);
6363
}
64+
case "/hyperdrive": {
65+
if (
66+
typeof env.HYPERDRIVE.connect !== "function" ||
67+
typeof env.HYPERDRIVE.connectionString !== "string"
68+
) {
69+
return new Response("Hyperdrive binding is not configured properly", {
70+
status: 500,
71+
});
72+
}
73+
74+
return new Response("Hyperdrive binding works");
75+
}
6476
case "/hello-world": {
6577
const value = Math.floor(Date.now() * Math.random()).toString(36);
6678
await env.HELLO_WORLD.set(value);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ declare namespace Cloudflare {
77
IMAGES: ImagesBinding;
88
WAE: AnalyticsEngineDataset;
99
RATE_LIMITER: RateLimit;
10+
HYPERDRIVE: Hyperdrive;
1011
}
1112
}
1213
interface Env extends Cloudflare.Env {}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
"binding": "WAE",
2424
},
2525
],
26+
"hyperdrive": [
27+
{
28+
"binding": "HYPERDRIVE",
29+
"id": "test-hyperdrive-id",
30+
},
31+
],
2632
"unsafe": {
2733
"bindings": [
2834
{

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ export function resolvePluginConfig(
112112
experimental: pluginConfig.experimental ?? {},
113113
};
114114
const root = userConfig.root ? path.resolve(userConfig.root) : process.cwd();
115+
const prefixedEnv = vite.loadEnv(viteEnv.mode, root, [
116+
"CLOUDFLARE_",
117+
// TODO: Remove deprecated WRANGLER prefix support in next major version
118+
"WRANGLER_HYPERDRIVE_LOCAL_CONNECTION_STRING_",
119+
]);
120+
121+
// Merge the loaded env variables into process.env so that they are available to
122+
// wrangler when it loads the worker configuration files.
123+
Object.assign(process.env, prefixedEnv);
115124

116125
if (viteEnv.isPreview) {
117126
return {
@@ -122,12 +131,7 @@ export function resolvePluginConfig(
122131
}
123132

124133
const configPaths = new Set<string>();
125-
const { CLOUDFLARE_ENV: cloudflareEnv } = vite.loadEnv(
126-
viteEnv.mode,
127-
root,
128-
/* prefixes */ ""
129-
);
130-
134+
const cloudflareEnv = prefixedEnv.CLOUDFLARE_ENV;
131135
const entryWorkerConfigPath = getValidatedWranglerConfigPath(
132136
root,
133137
pluginConfig.configPath

0 commit comments

Comments
 (0)