Skip to content

Commit e05bf64

Browse files
jihchirandycoulman
authored andcommitted
Make all react app vars accessible in index.html (facebook#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
1 parent 86c4ed8 commit e05bf64

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

packages/react-scripts/config/env.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,33 @@
1515
var REACT_APP = /^REACT_APP_/i;
1616

1717
function getClientEnvironment(publicUrl) {
18-
var processEnv = Object
18+
var raw = Object
1919
.keys(process.env)
2020
.filter(key => REACT_APP.test(key))
2121
.reduce((env, key) => {
22-
env[key] = JSON.stringify(process.env[key]);
22+
env[key] = process.env[key];
2323
return env;
2424
}, {
2525
// Useful for determining whether we’re running in production mode.
2626
// Most importantly, it switches React into the correct mode.
27-
'NODE_ENV': JSON.stringify(
28-
process.env.NODE_ENV || 'development'
29-
),
27+
'NODE_ENV': process.env.NODE_ENV || 'development',
3028
// Useful for resolving the correct path to static assets in `public`.
3129
// For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />.
3230
// This should only be used as an escape hatch. Normally you would put
3331
// images into the `src` and `import` them in code to get their paths.
34-
'PUBLIC_URL': JSON.stringify(publicUrl)
32+
'PUBLIC_URL': publicUrl
3533
});
36-
return {'process.env': processEnv};
34+
// Stringify all values so we can feed into Webpack DefinePlugin
35+
var stringified = {
36+
'process.env': Object
37+
.keys(raw)
38+
.reduce((env, key) => {
39+
env[key] = JSON.stringify(raw[key]);
40+
return env;
41+
}, {})
42+
};
43+
44+
return { raw, stringified };
3745
}
3846

3947
module.exports = getClientEnvironment;

packages/react-scripts/config/webpack.config.dev.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -225,20 +225,19 @@ module.exports = function(publicPath) {
225225
];
226226
},
227227
plugins: [
228-
// Makes the public URL available as %PUBLIC_URL% in index.html, e.g.:
228+
// Makes some environment variables available in index.html.
229+
// The public URL is available as %PUBLIC_URL% in index.html, e.g.:
229230
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
230231
// In development, this will be an empty string.
231-
new InterpolateHtmlPlugin({
232-
PUBLIC_URL: publicUrl
233-
}),
232+
new InterpolateHtmlPlugin(env.raw),
234233
// Generates an `index.html` file with the <script> injected.
235234
new HtmlWebpackPlugin({
236235
inject: true,
237236
template: paths.appHtml,
238237
}),
239238
// Makes some environment variables available to the JS code, for example:
240239
// if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`.
241-
new webpack.DefinePlugin(env),
240+
new webpack.DefinePlugin(env.stringified),
242241
// This is necessary to emit hot updates (currently CSS only):
243242
new webpack.HotModuleReplacementPlugin(),
244243
// Watcher doesn't work well if you mistype casing in a path so we use

packages/react-scripts/config/webpack.config.prod.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ var env = getClientEnvironment(publicUrl);
5454

5555
// Assert this just to be safe.
5656
// Development builds of React are slow and not intended for production.
57-
if (env['process.env'].NODE_ENV !== '"production"') {
57+
if (env.stringified['process.env'].NODE_ENV !== '"production"') {
5858
throw new Error('Production builds must have NODE_ENV=production.');
5959
}
6060

@@ -230,13 +230,12 @@ module.exports = {
230230
];
231231
},
232232
plugins: [
233-
// Makes the public URL available as %PUBLIC_URL% in index.html, e.g.:
233+
// Makes some environment variables available in index.html.
234+
// The public URL is available as %PUBLIC_URL% in index.html, e.g.:
234235
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
235236
// In production, it will be an empty string unless you specify "homepage"
236237
// in `package.json`, in which case it will be the pathname of that URL.
237-
new InterpolateHtmlPlugin({
238-
PUBLIC_URL: publicUrl
239-
}),
238+
new InterpolateHtmlPlugin(env.raw),
240239
// Generates an `index.html` file with the <script> injected.
241240
new HtmlWebpackPlugin({
242241
inject: true,
@@ -258,7 +257,7 @@ module.exports = {
258257
// if (process.env.NODE_ENV === 'production') { ... }. See `./env.js`.
259258
// It is absolutely essential that NODE_ENV was set to production here.
260259
// Otherwise React will be compiled in the very slow development mode.
261-
new webpack.DefinePlugin(env),
260+
new webpack.DefinePlugin(env.stringified),
262261
// This helps ensure the builds are consistent if source hasn't changed:
263262
new webpack.optimize.OccurrenceOrderPlugin(),
264263
// Try to dedupe duplicated modules, if any:

0 commit comments

Comments
 (0)