Skip to content

Commit 1d9159d

Browse files
jihchiTimer
authored andcommitted
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
1 parent b999405 commit 1d9159d

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
@@ -198,20 +198,19 @@ module.exports = {
198198
];
199199
},
200200
plugins: [
201-
// Makes the public URL available as %PUBLIC_URL% in index.html, e.g.:
201+
// Makes some environment variables available in index.html.
202+
// The public URL is available as %PUBLIC_URL% in index.html, e.g.:
202203
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
203204
// In development, this will be an empty string.
204-
new InterpolateHtmlPlugin({
205-
PUBLIC_URL: publicUrl
206-
}),
205+
new InterpolateHtmlPlugin(env.raw),
207206
// Generates an `index.html` file with the <script> injected.
208207
new HtmlWebpackPlugin({
209208
inject: true,
210209
template: paths.appHtml,
211210
}),
212211
// Makes some environment variables available to the JS code, for example:
213212
// if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`.
214-
new webpack.DefinePlugin(env),
213+
new webpack.DefinePlugin(env.stringified),
215214
// This is necessary to emit hot updates (currently CSS only):
216215
new webpack.HotModuleReplacementPlugin(),
217216
// 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

@@ -212,13 +212,12 @@ module.exports = {
212212
];
213213
},
214214
plugins: [
215-
// Makes the public URL available as %PUBLIC_URL% in index.html, e.g.:
215+
// Makes some environment variables available in index.html.
216+
// The public URL is available as %PUBLIC_URL% in index.html, e.g.:
216217
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
217218
// In production, it will be an empty string unless you specify "homepage"
218219
// in `package.json`, in which case it will be the pathname of that URL.
219-
new InterpolateHtmlPlugin({
220-
PUBLIC_URL: publicUrl
221-
}),
220+
new InterpolateHtmlPlugin(env.raw),
222221
// Generates an `index.html` file with the <script> injected.
223222
new HtmlWebpackPlugin({
224223
inject: true,
@@ -240,7 +239,7 @@ module.exports = {
240239
// if (process.env.NODE_ENV === 'production') { ... }. See `./env.js`.
241240
// It is absolutely essential that NODE_ENV was set to production here.
242241
// Otherwise React will be compiled in the very slow development mode.
243-
new webpack.DefinePlugin(env),
242+
new webpack.DefinePlugin(env.stringified),
244243
// This helps ensure the builds are consistent if source hasn't changed:
245244
new webpack.optimize.OccurrenceOrderPlugin(),
246245
// Try to dedupe duplicated modules, if any:

0 commit comments

Comments
 (0)