Problem
The @cloudflare/vite-plugin supports a config callback that lets integrations programmatically inject vars into the worker config during vite dev:
cloudflare({
config(workerConfig) {
return {
vars: { ...workerConfig.vars, MY_VAR: 'value' },
};
},
});
This works great during vite dev — the callback runs and the vars are passed to miniflare. However, during vite preview, the callback is not invoked. In resolvePluginConfig, when viteEnv.isPreview is true, the function returns early with type: "preview" and calls getWorkerConfigs() — which reads the built wrangler.json from disk and skips resolveWorkerConfig (where configCustomizer is passed).
Instead, preview reads vars exclusively from the .dev.vars file emitted into the build output during vite build.
This forces integrations that need to inject vars at preview time (e.g., env management tools like varlock) to create a .dev.vars file on the filesystem. We currently work around this using a FIFO (named pipe) so secrets don't touch disk, but this is fragile, platform-specific (no FIFOs on Windows), and adds significant complexity.
Proposed solution
Any of the following would work:
- Call the existing
config callback during preview (same as dev) — simplest change, most consistent behavior
- Add a
previewConfig callback option — if there's a reason to keep preview config separate
- Expose a hook/API in
configurePreviewServer that allows injecting additional miniflare bindings before the preview worker starts
Option 1 seems ideal — integrations already expect the config callback to control worker vars, and having it also apply during preview would be the most intuitive behavior.
Current workaround
Create a FIFO named pipe at the .dev.vars path in the build output directory before the CF plugin's configurePreviewServer hook reads it. On Windows, fall back to a temporary file. Both require cleanup on exit and orphan protection for the FIFO server process.
Context
This came up while building the varlock Cloudflare integration, which wraps @cloudflare/vite-plugin and injects resolved env vars. The config callback works perfectly for dev, but preview requires the FIFO workaround described above. Having a programmatic API for preview would significantly simplify integrations that manage env vars.
Problem
The
@cloudflare/vite-pluginsupports aconfigcallback that lets integrations programmatically inject vars into the worker config duringvite dev:This works great during
vite dev— the callback runs and the vars are passed to miniflare. However, duringvite preview, the callback is not invoked. InresolvePluginConfig, whenviteEnv.isPreviewis true, the function returns early withtype: "preview"and callsgetWorkerConfigs()— which reads the builtwrangler.jsonfrom disk and skipsresolveWorkerConfig(whereconfigCustomizeris passed).Instead, preview reads vars exclusively from the
.dev.varsfile emitted into the build output duringvite build.This forces integrations that need to inject vars at preview time (e.g., env management tools like varlock) to create a
.dev.varsfile on the filesystem. We currently work around this using a FIFO (named pipe) so secrets don't touch disk, but this is fragile, platform-specific (no FIFOs on Windows), and adds significant complexity.Proposed solution
Any of the following would work:
configcallback during preview (same as dev) — simplest change, most consistent behaviorpreviewConfigcallback option — if there's a reason to keep preview config separateconfigurePreviewServerthat allows injecting additional miniflare bindings before the preview worker startsOption 1 seems ideal — integrations already expect the
configcallback to control worker vars, and having it also apply during preview would be the most intuitive behavior.Current workaround
Create a FIFO named pipe at the
.dev.varspath in the build output directory before the CF plugin'sconfigurePreviewServerhook reads it. On Windows, fall back to a temporary file. Both require cleanup on exit and orphan protection for the FIFO server process.Context
This came up while building the varlock Cloudflare integration, which wraps
@cloudflare/vite-pluginand injects resolved env vars. Theconfigcallback works perfectly for dev, but preview requires the FIFO workaround described above. Having a programmatic API for preview would significantly simplify integrations that manage env vars.