Skip to content

Commit de6fa18

Browse files
dario-piotrowiczjamesopstadvicbpetebacondarwin
authored
improve and test vite-plugin-cloudflare .dev.vars files support (#7864)
Co-authored-by: James Opstad <[email protected]> Co-authored-by: Victor Berchet <[email protected]> Co-authored-by: Pete Bacon Darwin <[email protected]>
1 parent f1ef4f1 commit de6fa18

25 files changed

+283
-16
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@cloudflare/vite-plugin": patch
3+
---
4+
5+
Add full support for `.dev.vars` files.
6+
7+
This change makes sure that `.dev.vars` files work when the environment is specified. It also
8+
copies the target `.dev.vars` file (which might be environment specific, e.g. `.dev.vars.prod`)
9+
to the worker's dist directory so that `vite preview` can pick it up.
10+
The copied file will always be named `.dev.vars`.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"wrangler": minor
3+
---
4+
5+
Update the `unstable_getMiniflareWorkerOptions` types to always include an `env` parameter.
6+
7+
The `unstable_getMiniflareWorkerOptions` types, when accepting a config object as the first argument,
8+
didn't accept a second `env` argument. The changes here make sure they do, since the `env` is still
9+
relevant for picking up variables from `.dev.vars` files.

packages/vite-plugin-cloudflare/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,13 @@ The value of `MY_VAR` will therefore be `'Production var'`.
466466
If you run `vite build --mode staging` then the 'staging' Vite mode will be used and the 'staging' Cloudflare environment will be selected.
467467
The value of `MY_VAR` will therefore be `'Staging var'`.
468468

469+
## Secrets
470+
471+
Secrets can be provided to your Worker in local development using a [`.dev.vars`](https://developers.cloudflare.com/workers/configuration/secrets/#local-development-with-secrets) file. If you are using [Cloudflare Environments](#cloudflare-environments) then the relevant `.dev.vars` file will be selected. For example, `CLOUDFLARE_ENV=staging vite dev` will load `.dev.vars.staging` if it exists and fall back to `.dev.vars`.
472+
473+
> [!NOTE]
474+
> The `vite build` command copies the relevant `.dev.vars[.env-name]` file to the output directory. This is only used when running `vite preview` and is not deployed with your Worker.
475+
469476
## Migrating from `wrangler dev`
470477

471478
Migrating from `wrangler dev` is a simple process and you can follow the instructions in the [Quick start](#quick-start) to get started.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ENV_NAME = ""
2+
MY_DEV_VAR_A = "my .dev.vars variable A"
3+
MY_DEV_VAR_B = "my .dev.vars variable B"
4+
MY_DEV_VAR_C = "my .dev.vars variable C"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ENV_NAME = "staging"
2+
MY_DEV_VAR_A = "my .dev.vars staging variable A"
3+
MY_DEV_VAR_B = "my .dev.vars staging variable B"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CLOUDFLARE_ENV=staging
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!.dev.vars*
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import fs from "node:fs";
2+
import { describe, expect, test } from "vitest";
3+
import { getJsonResponse, isBuild, testDir } from "../../__test-utils__";
4+
5+
test("reading variables from a standard .dev.vars file", async () => {
6+
expect(await getJsonResponse()).toEqual({
7+
"variables present in .dev.vars": {
8+
MY_DEV_VAR_A: "my .dev.vars variable A",
9+
MY_DEV_VAR_B: "my .dev.vars variable B",
10+
MY_DEV_VAR_C: "my .dev.vars variable C",
11+
},
12+
});
13+
});
14+
15+
describe.runIf(isBuild)("build output files", () => {
16+
test("the .dev.vars file has been copied over", async () => {
17+
const srcDevVarsPath = `${testDir}/.dev.vars`;
18+
const distDevVarsPath = `${testDir}/dist/worker/.dev.vars`;
19+
20+
const distDevVarsExists = fs.existsSync(distDevVarsPath);
21+
expect(distDevVarsExists).toBe(true);
22+
23+
const srcDevVarsContent = fs.readFileSync(srcDevVarsPath, "utf-8");
24+
const distDevVarsContent = fs.readFileSync(distDevVarsPath, "utf-8");
25+
expect(distDevVarsContent).toEqual(srcDevVarsContent);
26+
});
27+
28+
test("secrets from .dev.vars haven't been inlined in the js output file", async () => {
29+
const distIndexPath = `${testDir}/dist/worker/index.js`;
30+
31+
const distIndexContent = fs.readFileSync(distIndexPath, "utf-8");
32+
expect(distIndexContent).not.toContain("my .dev.vars variable");
33+
});
34+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import fs from "node:fs";
2+
import { describe, expect, test } from "vitest";
3+
import { getJsonResponse, isBuild, testDir } from "../../../__test-utils__";
4+
5+
test("reading variables from a staging .dev.vars file", async () => {
6+
expect(await getJsonResponse()).toEqual({
7+
"variables present in .dev.vars.staging": {
8+
MY_DEV_VAR_A: "my .dev.vars staging variable A",
9+
MY_DEV_VAR_B: "my .dev.vars staging variable B",
10+
},
11+
});
12+
});
13+
14+
describe.runIf(isBuild)("build output files", () => {
15+
test("the .dev.vars.staging file has been copied over as .dev.vars", async () => {
16+
const srcDevVarsStagingPath = `${testDir}/.dev.vars.staging`;
17+
const distDevVarsPath = `${testDir}/dist/worker/.dev.vars`;
18+
const distDevVarsStagingPath = `${distDevVarsPath}.staging`;
19+
20+
const distDevVarsExists = fs.existsSync(distDevVarsPath);
21+
expect(distDevVarsExists).toBe(true);
22+
23+
const srcDevVarsStagingContent = fs.readFileSync(
24+
srcDevVarsStagingPath,
25+
"utf-8"
26+
);
27+
const distDevVarsContent = fs.readFileSync(distDevVarsPath, "utf-8");
28+
expect(distDevVarsContent).toEqual(srcDevVarsStagingContent);
29+
30+
const distDevVarsStagingExists = fs.existsSync(distDevVarsStagingPath);
31+
expect(distDevVarsStagingExists).toBe(false);
32+
});
33+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "@playground/dev-vars",
3+
"private": true,
4+
"type": "module",
5+
"scripts": {
6+
"build": "vite build --app",
7+
"check:types": "tsc --build",
8+
"dev": "vite dev",
9+
"preview": "vite preview"
10+
},
11+
"devDependencies": {
12+
"@cloudflare/vite-plugin": "workspace:*",
13+
"@cloudflare/workers-tsconfig": "workspace:*",
14+
"@cloudflare/workers-types": "^4.20241230.0",
15+
"typescript": "catalog:vite-plugin",
16+
"vite": "catalog:vite-plugin",
17+
"wrangler": "catalog:vite-plugin"
18+
}
19+
}

0 commit comments

Comments
 (0)