Skip to content

Commit

Permalink
improve and test vite-plugin-cloudflare .dev.vars files support
Browse files Browse the repository at this point in the history
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
dario-piotrowicz committed Jan 22, 2025
1 parent a2f695b commit e3e9d3a
Show file tree
Hide file tree
Showing 22 changed files with 190 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-camels-hunt_vite-plugin.md
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
9 changes: 9 additions & 0 deletions .changeset/orange-camels-hunt_wrangler.md
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 packages/vite-plugin-cloudflare/playground/dev-vars/.dev.vars
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"
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"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!.dev.vars
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",
},
});
}
);
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 packages/vite-plugin-cloudflare/playground/dev-vars/package.json
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 packages/vite-plugin-cloudflare/playground/dev-vars/src/index.ts
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>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"files": [],
"references": [
{ "path": "./tsconfig.node.json" },
{ "path": "./tsconfig.worker.json" }
]
}
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__"]
}
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"]
}
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/**"]
}
}
}
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 })],
});
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 })],
});
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;
}
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]
3 changes: 2 additions & 1 deletion packages/vite-plugin-cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin {
const miniflare = new Miniflare(
getPreviewMiniflareOptions(
vitePreviewServer,
pluginConfig.persistState ?? true
pluginConfig.persistState ?? true,
resolvedPluginConfig
)
);

Expand Down
19 changes: 13 additions & 6 deletions packages/vite-plugin-cloudflare/src/miniflare-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,13 @@ export function getDevMiniflareOptions(
resolvedPluginConfig.type === "workers"
? Object.entries(resolvedPluginConfig.workers).map(
([environmentName, workerConfig]) => {
const miniflareWorkerOptions = unstable_getMiniflareWorkerOptions({
...workerConfig,
assets: undefined,
});
const miniflareWorkerOptions = unstable_getMiniflareWorkerOptions(
{
...workerConfig,
assets: undefined,
},
resolvedPluginConfig.cloudflareEnv
);

const { ratelimits, ...workerOptions } =
miniflareWorkerOptions.workerOptions;
Expand Down Expand Up @@ -443,7 +446,8 @@ export function getDevMiniflareOptions(

export function getPreviewMiniflareOptions(
vitePreviewServer: vite.PreviewServer,
persistState: PersistState
persistState: PersistState,
resolvedPluginConfig: ResolvedPluginConfig
): MiniflareOptions {
const resolvedViteConfig = vitePreviewServer.config;
const configPaths = getWorkerConfigPaths(resolvedViteConfig.root);
Expand All @@ -452,7 +456,10 @@ export function getPreviewMiniflareOptions(
);

const workers: Array<WorkerOptions> = workerConfigs.map((config) => {
const miniflareWorkerOptions = unstable_getMiniflareWorkerOptions(config);
const miniflareWorkerOptions = unstable_getMiniflareWorkerOptions(
config,
resolvedPluginConfig.cloudflareEnv
);

const { ratelimits, ...workerOptions } =
miniflareWorkerOptions.workerOptions;
Expand Down
22 changes: 12 additions & 10 deletions packages/vite-plugin-cloudflare/src/plugin-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface WorkerConfig extends BaseConfig {
interface BasePluginConfig {
configPaths: Set<string>;
persistState: PersistState;
cloudflareEnv?: string;
}

interface AssetsOnlyPluginConfig extends BasePluginConfig {
Expand Down Expand Up @@ -82,7 +83,11 @@ export function resolvePluginConfig(
const configPaths = new Set<string>();
const persistState = pluginConfig.persistState ?? true;
const root = userConfig.root ? path.resolve(userConfig.root) : process.cwd();
const { CLOUDFLARE_ENV } = vite.loadEnv(viteEnv.mode, root, "");
const { CLOUDFLARE_ENV: cloudflareEnv } = vite.loadEnv(
viteEnv.mode,
root,
""
);

const configPath = pluginConfig.configPath
? path.resolve(root, pluginConfig.configPath)
Expand All @@ -93,14 +98,10 @@ export function resolvePluginConfig(
`Config not found. Have you created a wrangler.json(c) or wrangler.toml file?`
);

const entryWorkerResolvedConfig = getWorkerConfig(
configPath,
CLOUDFLARE_ENV,
{
visitedConfigPaths: configPaths,
isEntryWorker: true,
}
);
const entryWorkerResolvedConfig = getWorkerConfig(configPath, cloudflareEnv, {
visitedConfigPaths: configPaths,
isEntryWorker: true,
});

if (entryWorkerResolvedConfig.type === "assets-only") {
return {
Expand Down Expand Up @@ -129,7 +130,7 @@ export function resolvePluginConfig(
for (const auxiliaryWorker of pluginConfig.auxiliaryWorkers ?? []) {
const workerResolvedConfig = getWorkerConfig(
path.resolve(root, auxiliaryWorker.configPath),
CLOUDFLARE_ENV,
cloudflareEnv,
{
visitedConfigPaths: configPaths,
}
Expand Down Expand Up @@ -167,5 +168,6 @@ export function resolvePluginConfig(
entryWorker: entryWorkerResolvedConfig,
auxiliaryWorkers: auxiliaryWorkersResolvedConfigs,
},
cloudflareEnv,
};
}
3 changes: 2 additions & 1 deletion packages/wrangler/src/api/integrations/platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ export function unstable_getMiniflareWorkerOptions(
env?: string
): Unstable_MiniflareWorkerOptions;
export function unstable_getMiniflareWorkerOptions(
config: Config
config: Config,
env?: string
): Unstable_MiniflareWorkerOptions;
export function unstable_getMiniflareWorkerOptions(
configOrConfigPath: string | Config,
Expand Down
21 changes: 21 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e3e9d3a

Please sign in to comment.