fix: support bracket notation for env var replacement in Vite define#696
Open
RobSchilderr wants to merge 2 commits into
Open
fix: support bracket notation for env var replacement in Vite define#696RobSchilderr wants to merge 2 commits into
RobSchilderr wants to merge 2 commits into
Conversation
Vite's `define` config performs literal text replacement. The current `clientEnvDefine` only generates dot-notation keys (e.g. `process.env.FOO`), so bracket notation (`process.env["FOO"]`) is never replaced and resolves to `undefined` at runtime. This adds bracket-notation entries (both double and single quotes) for `process.env` and `import.meta.env` so that either access style works in client bundles.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Vite's
defineconfig performs literal text replacement on source code. TheclientEnvDefineobject inloadEnv.tscurrently only generates dot-notation keys:This means bracket notation access — which is common in TypeScript codebases where
noPropertyAccessFromIndexSignatureis enabled — is never replaced and resolves toundefinedat runtime:TypeScript's
noPropertyAccessFromIndexSignaturecompiler option (part of strict configs) requires bracket notation for index signatures, which makesprocess.env["KEY"]the natural way to access env vars. This creates a subtle bug where env vars silently resolve toundefinedin client bundles.Fix
Add bracket-notation entries (both double and single quotes) to the
clientEnvDefineoutput for bothprocess.envandimport.meta.env:Note: Metro's Babel plugin (
import-meta-env-plugin.ts) already handles both notations via AST analysis, so this only affects the Vite/web path.