Skip to content

Commit

Permalink
read GATSBY_* env vars and feed to webpack (gatsbyjs#2057)
Browse files Browse the repository at this point in the history
* read GATSBY_* env vars and feed to webpack

* explanation of GATSBY* env vars

* typo

* added syntax highlighting to doc
  • Loading branch information
dan-weaver authored and KyleAMathews committed Sep 9, 2017
1 parent 58f2bf3 commit b39c279
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
21 changes: 21 additions & 0 deletions docs/docs/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ API_URL=https://example.com/api

These variables will be available to your site as `process.env.API_URL`.

In addition any variable in the environment prefixed with `GATSBY_` will be available.

## Example

```
GATSBY_ASSETS_URL=http://s3.amazonaws.com/bucketname
```

```jsx
// usage
render() {
return (
<div>
<img src={`${process.env.GATSBY_ASSETS_URL}/logo.png`} />
</div>
)
}
```



> You can not override certain environment variables as some are used internally for optimizations during build
Reserved environment variables:
Expand Down
9 changes: 8 additions & 1 deletion packages/gatsby/src/utils/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,18 @@ module.exports = async (
return acc
}, {})

const gatsbyVarObject = Object.keys(process.env).reduce((acc, key) => {
if (key.match(/^GATSBY_/)) {
acc[key] = JSON.stringify(process.env[key])
}
return acc
}, {})

// Don't allow overwriting of NODE_ENV, PUBLIC_DIR as to not break gatsby things
envObject.NODE_ENV = JSON.stringify(env)
envObject.PUBLIC_DIR = JSON.stringify(`${process.cwd()}/public`)

return envObject
return Object.assign(envObject, gatsbyVarObject)
}

debug(`Loading webpack config for stage "${stage}"`)
Expand Down

0 comments on commit b39c279

Please sign in to comment.