From caaeacb85cff8a2bccf92a07d6bd8253ab8afa91 Mon Sep 17 00:00:00 2001 From: Jih-Chi Lee Date: Fri, 10 Feb 2017 11:41:03 +0800 Subject: [PATCH] Make all react app vars accessible in index.html (#1440) * Make all vars accessiable in index.html * Fix wrong env provieded to DefinePlugin * Separate results from getClientEnvironment * The `string` should be object instead of string * Fix accessing wrong field * Changed variables naming to `raw` and `stringified` * Remove trailing commas --- config/env.js | 22 +++++++++++++++------- config/webpack.config.dev.js | 9 ++++----- config/webpack.config.prod.js | 11 +++++------ 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/config/env.js b/config/env.js index 66ba341b358..168e1e644f1 100644 --- a/config/env.js +++ b/config/env.js @@ -15,25 +15,33 @@ var REACT_APP = /^REACT_APP_/i; function getClientEnvironment(publicUrl) { - var processEnv = Object + var raw = Object .keys(process.env) .filter(key => REACT_APP.test(key)) .reduce((env, key) => { - env[key] = JSON.stringify(process.env[key]); + env[key] = process.env[key]; return env; }, { // Useful for determining whether we’re running in production mode. // Most importantly, it switches React into the correct mode. - 'NODE_ENV': JSON.stringify( - process.env.NODE_ENV || 'development' - ), + 'NODE_ENV': process.env.NODE_ENV || 'development', // Useful for resolving the correct path to static assets in `public`. // For example, . // This should only be used as an escape hatch. Normally you would put // images into the `src` and `import` them in code to get their paths. - 'PUBLIC_URL': JSON.stringify(publicUrl) + 'PUBLIC_URL': publicUrl }); - return {'process.env': processEnv}; + // Stringify all values so we can feed into Webpack DefinePlugin + var stringified = { + 'process.env': Object + .keys(raw) + .reduce((env, key) => { + env[key] = JSON.stringify(raw[key]); + return env; + }, {}) + }; + + return { raw, stringified }; } module.exports = getClientEnvironment; diff --git a/config/webpack.config.dev.js b/config/webpack.config.dev.js index ac63ba2f38c..b984ec3e4e7 100644 --- a/config/webpack.config.dev.js +++ b/config/webpack.config.dev.js @@ -198,12 +198,11 @@ module.exports = { ]; }, plugins: [ - // Makes the public URL available as %PUBLIC_URL% in index.html, e.g.: + // Makes some environment variables available in index.html. + // The public URL is available as %PUBLIC_URL% in index.html, e.g.: // // In development, this will be an empty string. - new InterpolateHtmlPlugin({ - PUBLIC_URL: publicUrl - }), + new InterpolateHtmlPlugin(env.raw), // Generates an `index.html` file with the