-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[Vite plugin]: add programmatic worker config #26819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: production
Are you sure you want to change the base?
Changes from 2 commits
ef2a042
afa9d0f
e23f955
6e798b0
80298ed
dcf3b40
2724e6d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- | ||
| title: Wrangler config is optional when using Vite plugin | ||
| description: When using the Cloudflare Vite plugin in an assets-only (static) site, a Wrangler configuration file is now optional. | ||
| products: | ||
| - workers | ||
| date: 2025-12-02 | ||
| --- | ||
|
|
||
| When using the [Cloudflare Vite plugin](/workers/vite-plugin/) to build and deploy Workers, a Wrangler configuration file is now optional for assets-only (static) sites. If no `wrangler.toml`, `wrangler.json`, or `wrangler.jsonc` file is found, the plugin generates sensible defaults for an assets-only site. The `name` is based on the `package.json` or the project directory name, and the `compatibility_date` uses the latest date supported by your installed Miniflare version. | ||
|
|
||
| This allows easier setup for static sites and Vite-based single-page applications (SPAs) that do not require custom Worker configuration. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| --- | ||
| title: Configure Workers programmatically using the Vite plugin | ||
| description: The Cloudflare Vite plugic can now programmatically configure Workers without a Wrangler config file, or modify existing configuration. | ||
| products: | ||
| - workers | ||
| date: 2025-12-02 | ||
| --- | ||
|
|
||
| The [Cloudflare Vite plugin](/workers/vite-plugin/) now supports programmatic configuration of Workers without a Wrangler configuration file. You can use the `configure` option to define Worker settings directly in your Vite configuration, or to modify existing configuration loaded from a Wrangler config file. This is particularly useful when integrating with other build tools or frameworks, as it allows them to control Worker configuration without needing users to manage a separate config file. | ||
|
|
||
| ## The `configure` option | ||
|
|
||
| The Vite plugin's new `configure` option accepts either a partial configuration object or a function that receives the current configuration and returns overrides. This option is applied after any config file is loaded, allowing the plugin to override specific values or define Workers entirely in code. | ||
|
|
||
| ## Example usage | ||
|
|
||
| Setting `configure` to an object to provide configuration values that merge with defaults and config file settings: | ||
|
|
||
| ```ts title="vite.config.ts" | ||
| import { defineConfig } from "vite"; | ||
| import { cloudflare } from "@cloudflare/vite-plugin"; | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [ | ||
| cloudflare({ | ||
| configure: { | ||
| name: "my-worker", | ||
| compatibility_flags: ["nodejs_compat"], | ||
| send_email: [ | ||
| { | ||
| name: "EMAIL", | ||
| }, | ||
| ], | ||
| }, | ||
| }), | ||
| ], | ||
| }); | ||
| ``` | ||
|
|
||
| Use a function to modify the existing configuration: | ||
|
|
||
| ```ts title="vite.config.ts" | ||
| import { defineConfig } from "vite"; | ||
| import { cloudflare } from "@cloudflare/vite-plugin"; | ||
| export default defineConfig({ | ||
| plugins: [ | ||
| cloudflare({ | ||
| configure: (config) => { | ||
| delete config.compatibility_flags; | ||
| }, | ||
| }), | ||
| ], | ||
| }); | ||
| ``` | ||
|
|
||
| Return an object with values to merge: | ||
|
|
||
| ```ts title="vite.config.ts" | ||
| import { defineConfig } from "vite"; | ||
| import { cloudflare } from "@cloudflare/vite-plugin"; | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [ | ||
| cloudflare({ | ||
| configure: (config) => { | ||
| if (!config.compatibility_flags.includes("no_nodejs_compat")) { | ||
| return { compatibility_flags: ["nodejs_compat"] }; | ||
| } | ||
| }, | ||
| }), | ||
| ], | ||
| }); | ||
| ``` | ||
|
|
||
| ### Auxiliary Workers | ||
|
|
||
| Auxiliary Workers also support the `configure` option, enabling multi-Worker architectures without config files. | ||
|
|
||
| Define auxiliary Workers without config files using `configure` inside the `auxiliaryWorkers` array: | ||
|
|
||
| ```ts title="vite.config.ts" | ||
| import { defineConfig } from "vite"; | ||
| import { cloudflare } from "@cloudflare/vite-plugin"; | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [ | ||
| cloudflare({ | ||
| configure: { | ||
| name: "entry-worker", | ||
| main: "./src/entry.ts", | ||
| services: [{ binding: "API", service: "api-worker" }], | ||
| }, | ||
| auxiliaryWorkers: [ | ||
| { | ||
| configure: { | ||
| name: "api-worker", | ||
| main: "./src/api.ts", | ||
| }, | ||
| }, | ||
| ], | ||
| }), | ||
| ], | ||
| }); | ||
| ``` | ||
|
|
||
| For more details and examples, see [Programmatic configuration](/workers/vite-plugin/reference/programmatic-configuration/). |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,158 @@ | ||||||||||||||
| --- | ||||||||||||||
| pcx_content_type: reference | ||||||||||||||
| title: Programmatic configuration | ||||||||||||||
| sidebar: | ||||||||||||||
| order: 4 | ||||||||||||||
ascorbic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||
| description: Configure Workers programmatically using the Vite plugin | ||||||||||||||
| --- | ||||||||||||||
|
|
||||||||||||||
| import { WranglerConfig } from "~/components"; | ||||||||||||||
|
|
||||||||||||||
| The Wrangler configuration file is optional when using the Cloudflare Vite plugin. Without one, the plugin uses default values. You can customize Worker configuration programmatically with the `configure` option. This is useful when the Cloudflare plugin runs inside another plugin or framework. | ||||||||||||||
|
|
||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think we need something to this effect to discourage users from defining configuration this way by default.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I actually wrote something but never committed it! I was wondering if it was worth saying how to manually add types, but I don't think we document that anywhere do we?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed in chat, I don't think this is documented but it can be done via declaration merging. |
||||||||||||||
| ## Default configuration | ||||||||||||||
|
|
||||||||||||||
| Without a configuration file, the plugin generates sensible defaults for an assets-only Worker. The `name` comes from `package.json` or the project directory name. The `compatibility_date` uses the latest date supported by your installed Miniflare version. | ||||||||||||||
|
|
||||||||||||||
| ## The `configure` option | ||||||||||||||
|
|
||||||||||||||
| The `configure` option offers three ways to programmatically configure your Worker: | ||||||||||||||
|
|
||||||||||||||
| ### Configuration object | ||||||||||||||
|
|
||||||||||||||
| Set `configure` to an object to provide values that merge with defaults and config file settings: | ||||||||||||||
|
|
||||||||||||||
| ```ts title="vite.config.ts" | ||||||||||||||
| import { defineConfig } from "vite"; | ||||||||||||||
| import { cloudflare } from "@cloudflare/vite-plugin"; | ||||||||||||||
|
|
||||||||||||||
| export default defineConfig({ | ||||||||||||||
| plugins: [ | ||||||||||||||
| cloudflare({ | ||||||||||||||
| configure: { | ||||||||||||||
|
||||||||||||||
| compatibility_date: "2025-01-01", | ||||||||||||||
| vars: { | ||||||||||||||
| API_URL: "https://api.example.com", | ||||||||||||||
| }, | ||||||||||||||
| }, | ||||||||||||||
| }), | ||||||||||||||
| ], | ||||||||||||||
| }); | ||||||||||||||
| ``` | ||||||||||||||
|
|
||||||||||||||
| These values merge with config file values. The `configure` values take precedence. | ||||||||||||||
|
|
||||||||||||||
| ### Dynamic configuration function | ||||||||||||||
|
|
||||||||||||||
| Use a function when configuration depends on existing config values or external data: | ||||||||||||||
|
|
||||||||||||||
| ```ts title="vite.config.ts" | ||||||||||||||
| import { defineConfig } from "vite"; | ||||||||||||||
| import { cloudflare } from "@cloudflare/vite-plugin"; | ||||||||||||||
|
|
||||||||||||||
| export default defineConfig({ | ||||||||||||||
| plugins: [ | ||||||||||||||
| cloudflare({ | ||||||||||||||
| configure: (config) => ({ | ||||||||||||||
| vars: { | ||||||||||||||
| WORKER_NAME: config.name, | ||||||||||||||
| BUILD_TIME: new Date().toISOString(), | ||||||||||||||
| }, | ||||||||||||||
| }), | ||||||||||||||
| }), | ||||||||||||||
| ], | ||||||||||||||
| }); | ||||||||||||||
| ``` | ||||||||||||||
|
|
||||||||||||||
| The function receives the current configuration (defaults or loaded config file). Return an object with values to merge. | ||||||||||||||
|
|
||||||||||||||
| ### In-place editing | ||||||||||||||
|
|
||||||||||||||
| A `configure` function can mutate the config object directly instead of returning overrides. This is useful for deleting properties or removing array items: | ||||||||||||||
|
|
||||||||||||||
| ```ts title="vite.config.ts" | ||||||||||||||
| import { defineConfig } from "vite"; | ||||||||||||||
| import { cloudflare } from "@cloudflare/vite-plugin"; | ||||||||||||||
|
|
||||||||||||||
| export default defineConfig({ | ||||||||||||||
| plugins: [ | ||||||||||||||
| cloudflare({ | ||||||||||||||
| configure: (config) => { | ||||||||||||||
| // Replace all existing compatibility flags | ||||||||||||||
| config.compatibility_flags = ["nodejs_compat"]; | ||||||||||||||
| }, | ||||||||||||||
| }), | ||||||||||||||
| ], | ||||||||||||||
| }); | ||||||||||||||
| ``` | ||||||||||||||
|
|
||||||||||||||
| When editing in place, do not return a value from the function. | ||||||||||||||
|
|
||||||||||||||
| ## Auxiliary Workers | ||||||||||||||
|
|
||||||||||||||
| Auxiliary Workers also support the `configure` option, enabling multi-Worker architectures without config files. | ||||||||||||||
|
|
||||||||||||||
| Define auxiliary Workers without config files using `configure` inside the `auxiliaryWorkers` array: | ||||||||||||||
|
|
||||||||||||||
| ```ts title="vite.config.ts" | ||||||||||||||
| import { defineConfig } from "vite"; | ||||||||||||||
| import { cloudflare } from "@cloudflare/vite-plugin"; | ||||||||||||||
|
|
||||||||||||||
| export default defineConfig({ | ||||||||||||||
| plugins: [ | ||||||||||||||
| cloudflare({ | ||||||||||||||
| configure: { | ||||||||||||||
| name: "entry-worker", | ||||||||||||||
| main: "./src/entry.ts", | ||||||||||||||
| compatibility_date: "2025-01-01", | ||||||||||||||
| services: [{ binding: "API", service: "api-worker" }], | ||||||||||||||
| }, | ||||||||||||||
| auxiliaryWorkers: [ | ||||||||||||||
| { | ||||||||||||||
| configure: { | ||||||||||||||
| name: "api-worker", | ||||||||||||||
| main: "./src/api.ts", | ||||||||||||||
| compatibility_date: "2025-01-01", | ||||||||||||||
| }, | ||||||||||||||
| }, | ||||||||||||||
| ], | ||||||||||||||
| }), | ||||||||||||||
| ], | ||||||||||||||
| }); | ||||||||||||||
| ``` | ||||||||||||||
|
|
||||||||||||||
| ### Configuration overrides | ||||||||||||||
|
|
||||||||||||||
| Combine a config file with `configure` to override specific values: | ||||||||||||||
|
|
||||||||||||||
| ```ts title="vite.config.ts" | ||||||||||||||
| import { defineConfig } from "vite"; | ||||||||||||||
| import { cloudflare } from "@cloudflare/vite-plugin"; | ||||||||||||||
|
|
||||||||||||||
| export default defineConfig({ | ||||||||||||||
| plugins: [ | ||||||||||||||
| cloudflare({ | ||||||||||||||
| configPath: "./wrangler.jsonc", | ||||||||||||||
| auxiliaryWorkers: [ | ||||||||||||||
| { | ||||||||||||||
| configPath: "./workers/api/wrangler.jsonc", | ||||||||||||||
| configure: { | ||||||||||||||
| vars: { | ||||||||||||||
| ENDPOINT: "https://api.example.com/v2", | ||||||||||||||
| }, | ||||||||||||||
| }, | ||||||||||||||
| }, | ||||||||||||||
| ], | ||||||||||||||
| }), | ||||||||||||||
| ], | ||||||||||||||
| }); | ||||||||||||||
| ``` | ||||||||||||||
|
|
||||||||||||||
| ## Configuration merging behavior | ||||||||||||||
|
|
||||||||||||||
| The `configure` option uses [defu](https://github.com/unjs/defu) for merging configuration objects. | ||||||||||||||
|
|
||||||||||||||
| - Object properties are recursively merged | ||||||||||||||
| - Arrays are concatenated (configure values first, then existing values) | ||||||||||||||
| - Primitive values from `configure` override existing values | ||||||||||||||
| - `undefined` values in `configure` do not override existing values | ||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add a note here that
assets.not_found_handlingstill needs to be set tosingle-page-applicationfor SPA? This is likely to trip people up otherwise.