Skip to content

feat(vite): select env from VITE_ENV var instead of requiring --mode#26

Merged
rqbazan merged 2 commits into
mainfrom
feat/vite-env-var
Jun 16, 2026
Merged

feat(vite): select env from VITE_ENV var instead of requiring --mode#26
rqbazan merged 2 commits into
mainfrom
feat/vite-env-var

Conversation

@rqbazan

@rqbazan rqbazan commented Jun 16, 2026

Copy link
Copy Markdown
Member

What

The envConfig() Vite plugin can now select the env from a VITE_ENV env var instead of requiring --mode. Purely additive and backward-compatible.

# These are now equivalent:
VITE_ENV=staging vite build
vite build --mode staging

Why

Threading --mode <env> through every command is friction, especially in CI/CD or when the env is already expressed as an env var (or lives in a .env* file). This lets a single VITE_ENV drive both the #config discovery and the __ENV_NAME__ build constant — aligning the build side with envName()'s precedence chain, which already understood VITE_ENV.

How

  • resolveEnvName() uses Vite's loadEnv(mode, cwd, envVar), so VITE_ENV is read from process.env and .env* files (inline/shell values win over file values). Falls back to Vite's mode when unset or empty — so vite build --mode staging keeps working unchanged.
  • Both the #config alias discovery and the __ENV_NAME__ define now use the resolved env (renamed resolvedModeresolvedEnv; error message now says env).
  • New envVar option (default "VITE_ENV") to rename the var.

Tests

Added a VITE_ENV resolution block: precedence over mode, fallback when unset, empty string treated as unset, and a custom envVar. 93/93 pass, typecheck + lint clean.

Docs & release

  • Minor changeset (0.4.00.5.0).
  • Updated guides (custom-modes, spa-vite-plugin), concepts/env-name, and the vite API reference.
  • Bumped the landing version to v0.5.0 (anticipating the release).

🤖 Generated with Claude Code

The envConfig() plugin now reads VITE_ENV — from process.env and .env*
files via Vite's loadEnv, inline values winning over file values — to
pick the #config file and the __ENV_NAME__ build constant, falling back
to Vite's mode when unset or empty. The var name is configurable via the
new envVar option (default "VITE_ENV"). Additive and backward-compatible:
--mode keeps working unchanged.

Includes tests, a minor changeset, docs, and the landing version bump to
v0.5.0.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@vland-bot

vland-bot Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Preview release

Latest commit: 1da7d1c

Some packages have been released:

Package Version Install
@vlandoss/env 0.4.1-git-1da7d1c.0 @vlandoss/env@0.4.1-git-1da7d1c.0

Note

Use the PR number as tag to install any package. For instance:

pnpm add @vlandoss/env@pr-26

…rding

The envConfig error message changed from `...for mode "x"` to `...for
env "x"`; update the assertion to match. Also add an e2e test proving
VITE_ENV overrides --mode through the real packed plugin.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@rqbazan rqbazan merged commit d355e6f into main Jun 16, 2026
12 checks passed
@rqbazan rqbazan deleted the feat/vite-env-var branch June 16, 2026 00:44
@vland-bot vland-bot Bot mentioned this pull request Jun 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant