astro icon indicating copy to clipboard operation
astro copied to clipboard

🐛 BUG: Can't overwrite vite's MODE environment variable

Open caioferrarezi opened this issue 2 years ago • 4 comments

What version of astro are you using?

v1.0.0-beta.27

Are you using an SSR adapter? If so, which one?

None

What package manager are you using?

yarn

What operating system are you using?

Linux

Describe the Bug

I wanna open this issue saying that I wasn't able to find out if this really a bug or it's designed like that, but since the docs mentions this:

Astro uses Vite for environment variables, and allows you to use any of its methods to get and set environment variables.

I'm struggling to find a way to overwrite the MODE environment variable to any value other than production or development (in my case, I need staging). What I was able to find is that the build script already expects this option as seen here but the CLI never provides it.

It seems that a --mode flag to the build command like vite's already solves the problem, but I came to ask because as I said, I don't know if this is a bug or if it is desired.

Thanks!

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-ueaw7y?file=package.json,src%2Fpages%2Findex.astro&on=stackblitz

Participation

  • [X] I am willing to submit a pull request for this issue.

caioferrarezi avatar May 06 '22 19:05 caioferrarezi

I think this is a case where we are not providing the mode you passed in. https://github.com/withastro/astro/blob/1acbcaba9e05ef376ba8f2e550a0ab5bbad35efe/packages/astro/src/core/build/static-build.ts#L125

matthewp avatar May 10 '22 20:05 matthewp

Thanks, @matthewp! I didn't see this step!

If you don't mind I would like to give it a try :)

caioferrarezi avatar May 10 '22 21:05 caioferrarezi

This just came up on docs triage — may be something we want to revisit?

Tagging @bholmesdev as he had some good use cases where you’d want to pass custom modes to Vite.

delucis avatar Jul 20 '22 14:07 delucis

Yep! Just discovered some added utility from custom Vite modes beyond the import.meta.env.MODE too. Namely, allowing custom environment variable extensions like .env.staging. Would like to support everything documented on the Vite 3 page!

bholmesdev avatar Jul 20 '22 14:07 bholmesdev