Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for env files #1462

Merged
merged 2 commits into from Jul 13, 2017
Merged

Allow for env files #1462

merged 2 commits into from Jul 13, 2017

Conversation

okcoker
Copy link
Contributor

@okcoker okcoker commented Jul 10, 2017

Usually dotenv is used to throw variables on process.env but in this case I'm just using the parse method and adding those variables to webpack's DefinePlugin. This way process.env never gets altered.

This also make sure not to overwrite NODE_ENV or PUBLIC_DIR.

To use, you'd just put an .env.development or .env.production file in your root folder and those variables will be provided at dev/build time.

Could fix #660

@gatsbybot
Copy link
Collaborator

Deploy preview ready!

Built with commit 4dff300

https://deploy-preview-1462--using-drupal.netlify.com

@gatsbybot
Copy link
Collaborator

Deploy preview ready!

Built with commit 4dff300

https://deploy-preview-1462--gatsbyjs.netlify.com

@gatsbybot
Copy link
Collaborator

Deploy preview ready!

Built with commit 4dff300

https://deploy-preview-1462--gatsbygram.netlify.com

@gatsbybot
Copy link
Collaborator

gatsbybot commented Jul 11, 2017

Deploy preview ready!

Built with commit 520b8d6

https://deploy-preview-1462--using-drupal.netlify.com

@gatsbybot
Copy link
Collaborator

gatsbybot commented Jul 11, 2017

Deploy preview failed.

Built with commit 520b8d6

https://app.netlify.com/sites/gatsbygram/deploys/5966675b6f4c504fd2c6eabd

@gatsbybot
Copy link
Collaborator

gatsbybot commented Jul 11, 2017

Deploy preview failed.

Built with commit 520b8d6

https://app.netlify.com/sites/gatsbyjs/deploys/5966675b6f4c504fd2c6eabb

@KyleAMathews
Copy link
Contributor

This looks great! Could you start a docs page discussing using .env files? Really glad to see this pattern supported. Would be great too to get GATSBY_* support in as well someday.

@okcoker
Copy link
Contributor Author

okcoker commented Jul 12, 2017

Sure thing.

I could easily add GATSBY_* only functionality but I'm not sure of the benefit.

From my understanding, the reason that create-react-app went the REACT_APP_* route, (and someone can correct me if I'm wrong), is because they didn't want to potentially expose private variables on the environment. They are similarly using dotenv to load environment variables but the difference is they are using dotenv.config here which by default alters the process.env object. I'm just using dotenv.parse which does not alter the environment. I'm just parses the variables from your .env.development or .env.production file and providing them to webpack at build time.

Am I missing something from Dan's comment here? I feel one could equally do REACT_APP_WHATEVER=some secret that shouldnt be exposed

Thoughts?

@KyleAMathews
Copy link
Contributor

I want Gatsby to work with other ways of setting environment variables other than .env files. So for .env, yeah you've done a great job there not overriding things so that's perhaps fine. But it'd be great to be able to run GATSBY_API=https://example.com gatsby develop. Also many hosting platforms just expose a way to add runtime environment variables not .env files.

@okcoker
Copy link
Contributor Author

okcoker commented Jul 12, 2017

Ah, I see. Baby steps 👶 🍼 😬

Just added a docs file. Let me know what you think!

@KyleAMathews
Copy link
Contributor

No yeah, definitely don't feel like you have to solve everything in this PR :-) this is a great improvement!

@okcoker
Copy link
Contributor Author

okcoker commented Jul 12, 2017

Fixed the docs typo

@KyleAMathews
Copy link
Contributor

Great PR! Thanks for helping improve Gatsby's abilities around env variables. That's something people have had to hand-code before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pass GATSBY* environment variables into app
3 participants