Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changeset/beige-results-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@cloudflare/vite-plugin": patch
---

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_<BINDING_NAME>` variable.

```sh
CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_PROD_DB="postgres://user:[email protected]:5432/testdb"
```
2 changes: 2 additions & 0 deletions packages/vite-plugin-cloudflare/playground/bindings/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# To test whether vite plugin could configure the hyperdrive binding properly
WRANGLER_HYPERDRIVE_LOCAL_CONNECTION_STRING_HYPERDRIVE="postgres://user:[email protected]:3456/testdb"
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ declare namespace Cloudflare {
IMAGES: ImagesBinding;
WAE: AnalyticsEngineDataset;
RATE_LIMITER: RateLimit;
HYPERDRIVE: Hyperdrive;
}
}
interface Env extends Cloudflare.Env {}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
"binding": "WAE",
},
],
"hyperdrive": [
{
"binding": "HYPERDRIVE",
"id": "test-hyperdrive-id",
},
],
"unsafe": {
"bindings": [
{
Expand Down
16 changes: 10 additions & 6 deletions packages/vite-plugin-cloudflare/src/plugin-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ export function resolvePluginConfig(
experimental: pluginConfig.experimental ?? {},
};
const root = userConfig.root ? path.resolve(userConfig.root) : process.cwd();
const prefixedEnv = vite.loadEnv(viteEnv.mode, root, [
"CLOUDFLARE_",
// 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, prefixedEnv);

if (viteEnv.isPreview) {
return {
Expand All @@ -122,12 +131,7 @@ export function resolvePluginConfig(
}

const configPaths = new Set<string>();
const { CLOUDFLARE_ENV: cloudflareEnv } = vite.loadEnv(
viteEnv.mode,
root,
/* prefixes */ ""
);

const cloudflareEnv = prefixedEnv.CLOUDFLARE_ENV;
const entryWorkerConfigPath = getValidatedWranglerConfigPath(
root,
pluginConfig.configPath
Expand Down
Loading