diff --git a/config/env.js b/config/env.js new file mode 100644 index 00000000000..654a4f94c62 --- /dev/null +++ b/config/env.js @@ -0,0 +1,24 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + + // Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be + // injected into the application via DefinePlugin in Webpack configuration. + +var REACT_APP = /^REACT_APP_/i; +var NODE_ENV = JSON.stringify(process.env.NODE_ENV || 'development'); + +module.exports = Object + .keys(process.env) + .filter(key => REACT_APP.test(key)) + .reduce((env, key) => { + env['process.env.' + key] = JSON.stringify(process.env[key]); + return env; + }, { + 'process.env.NODE_ENV': NODE_ENV + }); diff --git a/config/webpack.config.dev.js b/config/webpack.config.dev.js index 94cc5cff954..699eb14cd13 100644 --- a/config/webpack.config.dev.js +++ b/config/webpack.config.dev.js @@ -14,6 +14,7 @@ var HtmlWebpackPlugin = require('html-webpack-plugin'); var CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin'); var WatchMissingNodeModulesPlugin = require('../scripts/utils/WatchMissingNodeModulesPlugin'); var paths = require('./paths'); +var env = require('./env'); // This is the development configuration. // It is focused on developer experience and fast rebuilds. @@ -170,9 +171,9 @@ module.exports = { template: paths.appHtml, favicon: paths.appFavicon, }), - // Makes the environment available to the JS code, for example: + // Makes some environment variables available to the JS code, for example: // if (process.env.NODE_ENV === 'development') { ... }. See `env.js`. - new webpack.DefinePlugin({ 'process.env.NODE_ENV': '"development"' }), + new webpack.DefinePlugin(env), // Note: only CSS is currently hot reloaded // This is necessary to emit hot updates (currently CSS only): new webpack.HotModuleReplacementPlugin(), diff --git a/config/webpack.config.prod.js b/config/webpack.config.prod.js index 2a27d3044ce..70d24a87a43 100644 --- a/config/webpack.config.prod.js +++ b/config/webpack.config.prod.js @@ -14,6 +14,13 @@ var HtmlWebpackPlugin = require('html-webpack-plugin'); var ExtractTextPlugin = require('extract-text-webpack-plugin'); var url = require('url'); var paths = require('./paths'); +var env = require('./env'); + +// Assert this just to be safe. +// Development builds of React are slow and not intended for production. +if (env['process.env.NODE_ENV'] !== '"production"') { + throw new Error('Production builds must have NODE_ENV=production.'); +} // We use "homepage" field to infer "public path" at which the app is served. // Webpack needs to know it to put the right