Skip to content

Commit 6e378d4

Browse files
author
Archie Lee
committed
Make all vars accessiable in index.html
1 parent 3bb31cc commit 6e378d4

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

packages/react-scripts/config/env.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,27 @@ function getClientEnvironment(publicUrl) {
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+
35+
processEnv['process.env'] = Object
36+
.keys(processEnv)
37+
.reduce((env, key) => {
38+
env[key] = JSON.stringify(processEnv[key]);
39+
return env;
40+
}, {});
41+
42+
return processEnv;
3743
}
3844

3945
module.exports = getClientEnvironment;

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,17 +205,15 @@ module.exports = {
205205
// Makes the public URL available as %PUBLIC_URL% in index.html, e.g.:
206206
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
207207
// In development, this will be an empty string.
208-
new InterpolateHtmlPlugin({
209-
PUBLIC_URL: publicUrl
210-
}),
208+
new InterpolateHtmlPlugin(env),
211209
// Generates an `index.html` file with the <script> injected.
212210
new HtmlWebpackPlugin({
213211
inject: true,
214212
template: paths.appHtml,
215213
}),
216214
// Makes some environment variables available to the JS code, for example:
217215
// if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`.
218-
new webpack.DefinePlugin(env),
216+
new webpack.DefinePlugin(env['process.env']),
219217
// This is necessary to emit hot updates (currently CSS only):
220218
new webpack.HotModuleReplacementPlugin(),
221219
// Watcher doesn't work well if you mistype casing in a path so we use

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,7 @@ module.exports = {
218218
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
219219
// In production, it will be an empty string unless you specify "homepage"
220220
// in `package.json`, in which case it will be the pathname of that URL.
221-
new InterpolateHtmlPlugin({
222-
PUBLIC_URL: publicUrl
223-
}),
221+
new InterpolateHtmlPlugin(env),
224222
// Generates an `index.html` file with the <script> injected.
225223
new HtmlWebpackPlugin({
226224
inject: true,
@@ -242,7 +240,7 @@ module.exports = {
242240
// if (process.env.NODE_ENV === 'production') { ... }. See `./env.js`.
243241
// It is absolutely essential that NODE_ENV was set to production here.
244242
// Otherwise React will be compiled in the very slow development mode.
245-
new webpack.DefinePlugin(env),
243+
new webpack.DefinePlugin(env['process.env']),
246244
// This helps ensure the builds are consistent if source hasn't changed:
247245
new webpack.optimize.OccurrenceOrderPlugin(),
248246
// Try to dedupe duplicated modules, if any:

0 commit comments

Comments
 (0)