Skip to content

Commit

Permalink
Add _ for global build-time variables
Browse files Browse the repository at this point in the history
  • Loading branch information
dav-is committed Feb 7, 2019
1 parent ccff353 commit 355ce3f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
7 changes: 4 additions & 3 deletions packages/next/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1551,17 +1551,18 @@ The `modules` option on `"preset-env"` should be kept to `false` otherwise webpa
### Exposing configuration to the server / client side
Configuration values can be replaced by webpack at build time.
Configuration values can be replaced by Webpack at build time.
```js
// next.config.js
module.exports = {
env: {
customKey: "value"
customKey: "value",
_globalVar: "value"
}
}
```
This will be set to `proccess.env.customKey`.
This will be set to `proccess.env.customKey` and `globalVar`, respectively.
#### When configuration must be dynamic
Expand Down
11 changes: 7 additions & 4 deletions packages/next/build/webpack-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,13 @@ export default async function getBaseWebpackConfig (dir, {dev = false, isServer
new webpack.DefinePlugin(Object.assign(
{},
config.env ? Object.keys(config.env)
.reduce((acc, key) => ({
...acc,
...{ [`process.env.${key}`]: JSON.stringify(config.env[key]) }
}), {}) : {},
.reduce((acc, key) => {
const selector = key.match(/^(_?)(.+)$/)
return {
...acc,
...{ [`${selector[1] ? '' : 'process.env.'}${selector[2]}`]: JSON.stringify(config.env[key]) }
}
}, {}) : {},
{
'process.crossOrigin': JSON.stringify(config.crossOrigin),
'process.browser': JSON.stringify(!isServer)
Expand Down

0 comments on commit 355ce3f

Please sign in to comment.