feat: add support for unsetEnv config option #19439
Open
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.
Description
This adds a new config option
unsetEnv
which controls how thevite build
command behaves when an unset environment variable is encountered.This aims to protect against missing variables when performing a production build. These issues could be caught by a schema validation approach but only at runtime after the new version has been deployed. If
unsetEnv
would be set toerror
this would fail the build and the whole pipeline preventing the deployment.This is backwards compatible, as the default value is
off
and doesn't alter the build process if left unchanged.I'm aware of the argument made by @bluwy in #8021 but I believe this isn't a concern here because:
off
by defaultoff
, it can be disabled in the config.undefined
variable would be caught at build time without risk of any production issues.undefined
env variable they can designate a special string value to the variable (such as'undefined'
) and cast it toundefined
in code.As a side note, I believe deliberately omitting an env variable isn't the best practice as it suggests a mistake in the configuration rather than an intentional action.
Dev build
While this PR adds unset env variable check in production build it does nothing during development. If you feel like extending this behaviour to the dev build is desired I can add it to the PR. I briefly tested the
Proxy
approach mentioned by @bluwy in #8021 and it can be done although it has two limitations:console.error
in the browser.The file using the missing env variable has to be loaded for it to catch the issue.The missing env variable has to be accessed for it to catch the issue.Further considerations
warn
by default? Still maintains BC with regard to output files and success/failure of the build but changes the build console output.