A Vite plugin that transforms import.meta.env references to globalThis.env placeholders for runtime substitution via envsubst or Caddy's templating engine. Current stable version 0.3.0. Released in April 2025. Solves the problem of environment-specific builds by deferring variable injection to runtime, suitable for Docker/Kubernetes deployments. Key differentiators: only transforms declared variables from vite-env.d.ts, respects Vite's envPrefix, supports both envsubst and Caddy template syntax, and uses sourcemap-preserving transforms with a single runtime dependency (rolldown-string). Unlike other env injection plugins, it enables true build-once-deploy-anywhere without build-time environment knowledge.
npm install vite-plugin-envsubstVerified import paths — ran on the pinned version, not inferred.
Configures Vite to transform declared import.meta.env variables into globalThis.env placeholders for runtime substitution.
Add interface ImportMetaEnv with readonly string fields for each variable in src/vite-env.d.ts
Set envPrefix in Vite config to include non-VITE_ variables or declare them in vite-env.d.ts (still only VITE_ transformed unless prefix changed)
Use Caddy template engine or install envsubst in Docker image explicitly
For dev server, provide actual env vars via .env files or command line. The plugin skips transform in dev mode.
Use import syntax and set module resolution to ESM or 'bundler' in tsconfig.json
Add "type": "module" to package.json or use .mts extension for vite.config.ts
Add interface ImportMetaEnv with readonly string fields in src/vite-env.d.ts
Use import { envSubstPlugin } from 'vite-plugin-envsubst'