-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve and test vite-plugin-cloudflare
.dev.vars
files support
the changes here add tests for making sure that the vite plugin correctly reads secrets from `.dev.vars` files with and without a specified environment as part of this wragler's `unstable_getMiniflareWorkerOptions` utility needed to also be updated to accept the environment name as its second parameter
- Loading branch information
1 parent
d7210ec
commit 8689cad
Showing
24 changed files
with
281 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
"@cloudflare/vite-plugin": patch | ||
--- | ||
|
||
add full support for `.dev.vars` files | ||
|
||
this change makes sure that `.dev.vars` files work for a specified environment, it also | ||
copies the target `.dev.vars` file (which might be environment specific, eg: `.dev.vars.prod`) | ||
to the worker's dist directory, always named `.dev.vars` (so that `vite preview` can pick it up) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
"wrangler": minor | ||
--- | ||
|
||
allow `unstable_getMiniflareWorkerOptions` to always accept an `env` argument | ||
|
||
the `unstable_getMiniflareWorkerOptions` utility, when accepting a config object as its first argument, | ||
doesn't accept an `env` one, the changes here make sure it does since even if a config object is passed | ||
the `env` one is still relevant for picking up variables from potential `.dev.vars` files |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
packages/vite-plugin-cloudflare/playground/dev-vars/.dev.vars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
ENV_NAME = "" | ||
MY_DEV_VAR_A = "my .dev.vars variable A" | ||
MY_DEV_VAR_B = "my .dev.vars variable B" | ||
MY_DEV_VAR_C = "my .dev.vars variable C" |
3 changes: 3 additions & 0 deletions
3
packages/vite-plugin-cloudflare/playground/dev-vars/.dev.vars.staging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
ENV_NAME = "staging" | ||
MY_DEV_VAR_A = "my .dev.vars staging variable A" | ||
MY_DEV_VAR_B = "my .dev.vars staging variable B" |
1 change: 1 addition & 0 deletions
1
packages/vite-plugin-cloudflare/playground/dev-vars/.env.with-specified-env
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CLOUDFLARE_ENV=staging |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
!.dev.vars |
34 changes: 34 additions & 0 deletions
34
packages/vite-plugin-cloudflare/playground/dev-vars/__tests__/dev-vars-loading.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import fs from "node:fs"; | ||
import { describe, expect, test } from "vitest"; | ||
import { getJsonResponse, isBuild, testDir } from "../../__test-utils__"; | ||
|
||
test("reading variables from a standard .dev.vars file", async () => { | ||
expect(await getJsonResponse()).toEqual({ | ||
"variables present in .dev.vars": { | ||
MY_DEV_VAR_A: "my .dev.vars variable A", | ||
MY_DEV_VAR_B: "my .dev.vars variable B", | ||
MY_DEV_VAR_C: "my .dev.vars variable C", | ||
}, | ||
}); | ||
}); | ||
|
||
describe.runIf(isBuild)("build output files", () => { | ||
test("the .dev.vars file has been copied over", async () => { | ||
const srcDevVarsPath = `${testDir}/.dev.vars`; | ||
const distDevVarsPath = `${testDir}/dist/worker/.dev.vars`; | ||
|
||
const distDevVarsExists = fs.existsSync(distDevVarsPath); | ||
expect(distDevVarsExists).toBe(true); | ||
|
||
const srcDevVarsContent = fs.readFileSync(srcDevVarsPath, "utf-8"); | ||
const distDevVarsContent = fs.readFileSync(distDevVarsPath, "utf-8"); | ||
expect(distDevVarsContent).toEqual(srcDevVarsContent); | ||
}); | ||
|
||
test("secrets from .dev.vars haven't been inlined in the js output file", async () => { | ||
const distIndexPath = `${testDir}/dist/worker/index.js`; | ||
|
||
const distIndexContent = fs.readFileSync(distIndexPath, "utf-8"); | ||
expect(distIndexContent).not.toContain("my .dev.vars variable"); | ||
}); | ||
}); |
33 changes: 33 additions & 0 deletions
33
...ugin-cloudflare/playground/dev-vars/__tests__/with-specified-env/dev-vars-loading.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import fs from "node:fs"; | ||
import { describe, expect, test } from "vitest"; | ||
import { getJsonResponse, isBuild, testDir } from "../../../__test-utils__"; | ||
|
||
test("reading variables from a staging .dev.vars file", async () => { | ||
expect(await getJsonResponse()).toEqual({ | ||
"variables present in .dev.vars.staging": { | ||
MY_DEV_VAR_A: "my .dev.vars staging variable A", | ||
MY_DEV_VAR_B: "my .dev.vars staging variable B", | ||
}, | ||
}); | ||
}); | ||
|
||
describe.runIf(isBuild)("build output files", () => { | ||
test("the .dev.vars.staging file has been copied over as .dev.vars", async () => { | ||
const srcDevVarsStagingPath = `${testDir}/.dev.vars.staging`; | ||
const distDevVarsPath = `${testDir}/dist/worker/.dev.vars`; | ||
const distDevVarsStagingPath = `${distDevVarsPath}.staging`; | ||
|
||
const distDevVarsExists = fs.existsSync(distDevVarsPath); | ||
expect(distDevVarsExists).toBe(true); | ||
|
||
const srcDevVarsStagingContent = fs.readFileSync( | ||
srcDevVarsStagingPath, | ||
"utf-8" | ||
); | ||
const distDevVarsContent = fs.readFileSync(distDevVarsPath, "utf-8"); | ||
expect(distDevVarsContent).toEqual(srcDevVarsStagingContent); | ||
|
||
const distDevVarsStagingExists = fs.existsSync(distDevVarsStagingPath); | ||
expect(distDevVarsStagingExists).toBe(false); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
packages/vite-plugin-cloudflare/playground/dev-vars/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "@playground/dev-vars", | ||
"private": true, | ||
"type": "module", | ||
"scripts": { | ||
"build": "vite build --app", | ||
"check:types": "tsc --build", | ||
"dev": "vite dev", | ||
"prepreview": "node ./copy-dev-vars-to-dist.mjs", | ||
"preview": "vite preview" | ||
}, | ||
"devDependencies": { | ||
"@cloudflare/vite-plugin": "workspace:*", | ||
"@cloudflare/workers-tsconfig": "workspace:*", | ||
"@cloudflare/workers-types": "^4.20241230.0", | ||
"typescript": "catalog:vite-plugin", | ||
"vite": "catalog:vite-plugin", | ||
"wrangler": "catalog:vite-plugin" | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/vite-plugin-cloudflare/playground/dev-vars/src/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export default { | ||
async fetch(_req, env) { | ||
const { ENV_NAME, MY_DEV_VAR_A, MY_DEV_VAR_B, MY_DEV_VAR_C } = env; | ||
const dotDevDotVarsVariables = { MY_DEV_VAR_A, MY_DEV_VAR_B, MY_DEV_VAR_C }; | ||
const extension = ENV_NAME ? `.${ENV_NAME}` : ""; | ||
return Response.json({ | ||
[`variables present in .dev.vars${extension}`]: dotDevDotVarsVariables, | ||
}); | ||
}, | ||
} satisfies ExportedHandler<Env>; |
7 changes: 7 additions & 0 deletions
7
packages/vite-plugin-cloudflare/playground/dev-vars/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"files": [], | ||
"references": [ | ||
{ "path": "./tsconfig.node.json" }, | ||
{ "path": "./tsconfig.worker.json" } | ||
] | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/vite-plugin-cloudflare/playground/dev-vars/tsconfig.node.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": ["@cloudflare/workers-tsconfig/base.json"], | ||
"include": ["vite.config.ts", "__tests__"] | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/vite-plugin-cloudflare/playground/dev-vars/tsconfig.worker.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": ["@cloudflare/workers-tsconfig/worker.json"], | ||
"include": ["src", "worker-configuration.d.ts"] | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/vite-plugin-cloudflare/playground/dev-vars/turbo.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"$schema": "http://turbo.build/schema.json", | ||
"extends": ["//"], | ||
"tasks": { | ||
"build": { | ||
"outputs": ["dist/**"] | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/vite-plugin-cloudflare/playground/dev-vars/vite.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { cloudflare } from "@cloudflare/vite-plugin"; | ||
import { defineConfig } from "vite"; | ||
|
||
export default defineConfig({ | ||
plugins: [cloudflare({ persistState: false })], | ||
}); |
7 changes: 7 additions & 0 deletions
7
packages/vite-plugin-cloudflare/playground/dev-vars/vite.config.with-specified-env.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { cloudflare } from "@cloudflare/vite-plugin"; | ||
import { defineConfig } from "vite"; | ||
|
||
export default defineConfig({ | ||
mode: "with-specified-env", | ||
plugins: [cloudflare({ persistState: false })], | ||
}); |
8 changes: 8 additions & 0 deletions
8
packages/vite-plugin-cloudflare/playground/dev-vars/worker-configuration.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Generated by Wrangler by running `wrangler types` | ||
|
||
interface Env { | ||
ENV_NAME: string; | ||
MY_DEV_VAR_A: string; | ||
MY_DEV_VAR_B: string; | ||
MY_DEV_VAR_C: string; | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/vite-plugin-cloudflare/playground/dev-vars/wrangler.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
name = "worker" | ||
main = "./src/index.ts" | ||
compatibility_date = "2024-12-30" | ||
|
||
[env.staging] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.