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 theconfig option. This is useful when the Cloudflare plugin runs inside another plugin or framework.
Programmatic configuration is primarily designed for use by frameworks and plugin developers. Users should normally use Wrangler config files instead. Configuration set via theconfig option will not be included when runningwrangler types or resource based Wrangler CLI commands such aswrangler kv orwrangler d1.
Without a configuration file, the plugin generates sensible defaults for an assets-only Worker. Thename comes frompackage.json or the project directory name. Thecompatibility_date uses the latest date supported by your installed Miniflare version.
Theconfig option offers three ways to programmatically configure your Worker. You can set any property from theWrangler configuration file, though some options areignored or replaced by Vite equivalents.
You cannot defineCloudflare environments viaconfig, as they are resolved before this option is applied.
Setconfig to an object to provide values that merge with defaults and Wrangler config file settings:
import{defineConfig} from"vite";import{cloudflare} from"@cloudflare/vite-plugin";exportdefaultdefineConfig({plugins: [cloudflare({config:{compatibility_date:"2025-01-01",vars:{API_URL:"https://api.example.com",},},}),],});These values merge with Wrangler config file values, with theconfig values taking precedence.
Use a function when configuration depends on existing config values or external data, or if you need to compute or conditionally set values:
import{defineConfig} from"vite";import{cloudflare} from"@cloudflare/vite-plugin";exportdefaultdefineConfig({plugins: [cloudflare({config:(userConfig)=> ({vars:{WORKER_NAME:userConfig.name,BUILD_TIME:newDate().toISOString(),},}),}),],});The function receives the current configuration (defaults or loaded config file). Return an object with values to merge.
Aconfig function can mutate the config object directly instead of returning overrides. This is useful for deleting properties or removing array items:
import{defineConfig} from"vite";import{cloudflare} from"@cloudflare/vite-plugin";exportdefaultdefineConfig({plugins: [cloudflare({config:(userConfig)=>{// Replace all existing compatibility flagsuserConfig.compatibility_flags= ["nodejs_compat"];},}),],});When editing in place, do not return a value from the function.
Auxiliary Workers also support theconfig option, enabling multi-Worker architectures without config files.
Define auxiliary Workers without config files usingconfig inside theauxiliaryWorkers array:
import{defineConfig} from"vite";import{cloudflare} from"@cloudflare/vite-plugin";exportdefaultdefineConfig({plugins: [cloudflare({config:{name:"entry-worker",main:"./src/entry.ts",compatibility_date:"2025-01-01",services: [{ binding:"API", service:"api-worker"}],},auxiliaryWorkers: [{config:{name:"api-worker",main:"./src/api.ts",compatibility_date:"2025-01-01",},},],}),],});Combine a config file withconfig to override specific values:
import{defineConfig} from"vite";import{cloudflare} from"@cloudflare/vite-plugin";exportdefaultdefineConfig({plugins: [cloudflare({configPath:"./wrangler.jsonc",auxiliaryWorkers: [{configPath:"./workers/api/wrangler.jsonc",config:{vars:{ENDPOINT:"https://api.example.com/v2",},},},],}),],});Auxiliary Workers receive the resolved entry Worker config in the second parameter to theconfig function. This makes it straightforward to inherit configuration from the entry Worker in auxiliary Workers.
import{defineConfig} from"vite";import{cloudflare} from"@cloudflare/vite-plugin";exportdefaultdefineConfig({plugins: [cloudflare({auxiliaryWorkers: [{config:(_,{entryWorkerConfig})=> ({name:"auxiliary-worker",main:"./src/auxiliary-worker.ts",// Inherit compatibility settings from entry Workercompatibility_date:entryWorkerConfig.compatibility_date,compatibility_flags:entryWorkerConfig.compatibility_flags,}),},],}),],});Theconfig option usesdefu ↗ for merging configuration objects.
- Object properties are recursively merged
- Arrays are concatenated (
configvalues first, then existing values) - Primitive values from
configoverride existing values undefinedvalues inconfigdo not override existing values