karolis-sh / electron-snowpack

Use Snowpack and esbuild for Electron app development
MIT License
50 stars 8 forks source link

Handle dynamic/destructured .dotenv variable access in main #57

Open jrmdayn opened 2 years ago

jrmdayn commented 2 years ago

Hey 👋 ! Great work, thank you for this project!

I noticed that .env files are correctly read in dev mode in the main, but not in production (using yarn try for ex). Any idea on how I could make this work?

Thanks a lot 🙏

karolis-sh commented 2 years ago

Hi, how exactly are you using env variables in main? I have a feeling there might be an issue with env variable destructuring

jrmdayn commented 2 years ago

In the main I look through process.env for variables.

I looked at the output of build --verbose and I do see the variables from my .env files being passed through the define prop to esbuild.

After further investigation, I realized that my logic for getting an env variable from the main was actually located under another folder shared/ where I call process.env etc.. If I log the content of process.env from a file within shared/ I do not see the variables coming from my dotenv files. But when I do this from a file in the main/ folder they are there...

I can work around this short term but it would be nice to understand what's going on..

karolis-sh commented 2 years ago

Yeah, if you don't use the variables directly, but rather dynamically or via destructuring it won't work (https://github.com/evanw/esbuild/issues/438#issuecomment-857500937). I might look later around for newer techniques to handle this better.

jrmdayn commented 2 years ago

ok thanks for the pointers. That's a shame that esbuild does not handle this correctly out of the box 😢