diff --git a/packages/next/README.md b/packages/next/README.md index 6cab569627ed1..b80638fdaaf30 100644 --- a/packages/next/README.md +++ b/packages/next/README.md @@ -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 diff --git a/packages/next/build/webpack-config.js b/packages/next/build/webpack-config.js index f801a5f683b97..20e5014355ae3 100644 --- a/packages/next/build/webpack-config.js +++ b/packages/next/build/webpack-config.js @@ -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)