-
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
a2f695b
commit e3e9d3a
Showing
22 changed files
with
190 additions
and
18 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,5 @@ | ||
--- | ||
"@cloudflare/vite-plugin": patch | ||
--- | ||
|
||
make sure that `.dev.vars` files work for a specified environment |
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 |
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" |
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 |
19 changes: 19 additions & 0 deletions
19
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,19 @@ | ||
import { expect, test } from "vitest"; | ||
import { getJsonResponse, isBuild } from "../../__test-utils__"; | ||
|
||
// TODO: currently this does not work on previews because after the config redirection | ||
// wrangler looses track of the original .dev.vars, we need to somehow address such | ||
// use case (so that people are able to preview their workers using secrets) | ||
|
||
test.skipIf(isBuild)( | ||
"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", | ||
}, | ||
}); | ||
} | ||
); |
18 changes: 18 additions & 0 deletions
18
...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,18 @@ | ||
import { expect, test } from "vitest"; | ||
import { getJsonResponse, isBuild } from "../../../__test-utils__"; | ||
|
||
// TODO: currently this does not work on previews because after the config redirection | ||
// wrangler looses track of the original .dev.vars, we need to somehow address such | ||
// use case (so that people are able to preview their workers using secrets) | ||
|
||
test.skipIf(isBuild)( | ||
"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", | ||
}, | ||
}); | ||
} | ||
); |
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 })], | ||
}); |
8 changes: 8 additions & 0 deletions
8
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,8 @@ | ||
import { cloudflare } from "@cloudflare/vite-plugin"; | ||
import { defineConfig } from "vite"; | ||
|
||
process.env.CLOUDFLARE_ENV = "staging"; | ||
|
||
export default defineConfig({ | ||
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.