Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Contributor

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_handling still needs to be set to single-page-application for SPA? This is likely to trip people up otherwise.

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/).
23 changes: 21 additions & 2 deletions src/content/docs/workers/vite-plugin/reference/api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ It accepts an optional `PluginConfig` parameter.

For more information about the Worker configuration, see [Configuration](/workers/wrangler/configuration/).

- `configure` <Type text='Partial<Config> | ((config: Config) => Partial<Config> | void)' /> <MetaInfo text='optional' />

Customize or override Worker configuration programmatically.
Accepts a partial configuration object or a function that receives the current config.

Applied after any config file loads. Use it to override values, or define Workers entirely in code.

See [Programmatic configuration](/workers/vite-plugin/reference/programmatic-configuration/) for details.

- `viteEnvironment` <Type text='{ name?: string }' /> <MetaInfo text='optional' />

Optional Vite environment options.
Expand Down Expand Up @@ -75,12 +84,22 @@ It accepts an optional `PluginConfig` parameter.

## `interface AuxiliaryWorkerConfig`

- `configPath` <Type text='string' />
Auxiliary Workers require a `configPath`, a `configure` option, or both.

A required path to your Worker config file.
- `configPath` <Type text='string' /> <MetaInfo text='optional' />

The path to your Worker config file.
This field is required unless `configure` is provided.

For more information about the Worker configuration, see [Configuration](/workers/wrangler/configuration/).

- `configure` <Type text='object | function' /> <MetaInfo text='optional' />

Customize or override Worker configuration programmatically.
When used without `configPath`, this allows defining auxiliary Workers entirely in code.

See [Programmatic configuration](/workers/vite-plugin/reference/programmatic-configuration/) for usage examples.

- `viteEnvironment` <Type text='{ name?: string }' /> <MetaInfo text='optional' />

Optional Vite environment options.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
---
pcx_content_type: reference
title: Programmatic configuration
sidebar:
order: 4
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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
:::note
Configuration provided via the `config` option will not be included when running `wrangler types` or when running resource based Wrangler CLI commands such as `wrangler kv`, `wrangler d1` etc.
:::

I think we need something to this effect to discourage users from defining configuration this way by default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The 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: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first time I read this I didn't quite follow:

  1. the relationship between the object you pass into configure and wrangler.jsonc (I can intuit it, but it didn't jump off the page to me)
  2. if configure could basically be passed anything from wrangler.jsonc or if it only supported a subset of values

I think I know with context from working on Workers what the answers to these are, but wonder if it's worth making extra explicit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@irvinebroque good point. Added here 6e798b0

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
Loading