Feature: Allow programmatic var injection during vite preview (like config callback does for dev)
#13885
theoephraim
started this conversation in
Feature Requests
Replies: 1 comment
-
|
yeah the early-return for preview-mode is exactly what makes the couple of paths from here:
worth raising as a feature request if you havent already — feels like its something they'd want first-class now that varlock and dotenv-style integrations are pretty common. @petebacondarwin's been responsive on similar plugin stuff lately, worth tagging. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
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.Beta Was this translation helpful? Give feedback.
All reactions