vitejs / vite

Next generation frontend tooling. It's fast!
http://vitejs.dev
MIT License
66.26k stars 5.92k forks source link

Allow setting mode via environment variable #17513

Open rschamp opened 1 month ago

rschamp commented 1 month ago

Description

I am used to being able to configure my builds in CI via environment variables. E.g., if I have npm run build I would like to be able to use that same command, but have it behave differently based on the environment. Currently I can somewhat affect my build via NODE_ENV=production for a production build and NODE_ENV=development for a staging build. I would like to have the same control for the mode.

Suggested solution

Similarly to NODE_ENV we could have the VITE_MODE or MODE environment variable change the mode.

Alternative

In order to change import.meta.env.MODE, I need a separate command npm run build:staging, or I need to adjust the command I run like npm run build -- --mode staging. These workarounds make my CI config more complex than it could be.

Additional context

No response

Validations

sapphi-red commented 3 weeks ago

If you can put "build": "vite --mode ${VITE_MODE:-production}" (for bash), then I think you can run npm run build/MODE=staging npm run build.