Skip to content

Commit 1ca12a6

Browse files
jihchiDaniel Figueiredo
authored and
Daniel Figueiredo
committed
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 be83486 commit 1ca12a6

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
@@ -190,20 +190,19 @@ module.exports = {
190190
// We use PostCSS for autoprefixing only.
191191
postcss: postcss,
192192
plugins: [
193-
// Makes the public URL available as %PUBLIC_URL% in index.html, e.g.:
193+
// Makes some environment variables available in index.html.
194+
// The public URL is available as %PUBLIC_URL% in index.html, e.g.:
194195
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
195196
// In development, this will be an empty string.
196-
new InterpolateHtmlPlugin({
197-
PUBLIC_URL: publicUrl
198-
}),
197+
new InterpolateHtmlPlugin(env.raw),
199198
// Generates an `index.html` file with the <script> injected.
200199
new HtmlWebpackPlugin({
201200
inject: true,
202201
template: paths.appHtml,
203202
}),
204203
// Makes some environment variables available to the JS code, for example:
205204
// if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`.
206-
new webpack.DefinePlugin(env),
205+
new webpack.DefinePlugin(env.stringified),
207206
// This is necessary to emit hot updates (currently CSS only):
208207
new webpack.HotModuleReplacementPlugin(),
209208
// 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
@@ -57,7 +57,7 @@ var postcss = require('./rangle/postcss');
5757

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

@@ -204,13 +204,12 @@ module.exports = {
204204
// We use PostCSS for autoprefixing only.
205205
postcss: postcss,
206206
plugins: [
207-
// Makes the public URL available as %PUBLIC_URL% in index.html, e.g.:
207+
// Makes some environment variables available in index.html.
208+
// The public URL is available as %PUBLIC_URL% in index.html, e.g.:
208209
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
209210
// In production, it will be an empty string unless you specify "homepage"
210211
// in `package.json`, in which case it will be the pathname of that URL.
211-
new InterpolateHtmlPlugin({
212-
PUBLIC_URL: publicUrl
213-
}),
212+
new InterpolateHtmlPlugin(env.raw),
214213
// Generates an `index.html` file with the <script> injected.
215214
new HtmlWebpackPlugin({
216215
inject: true,
@@ -232,7 +231,7 @@ module.exports = {
232231
// if (process.env.NODE_ENV === 'production') { ... }. See `./env.js`.
233232
// It is absolutely essential that NODE_ENV was set to production here.
234233
// Otherwise React will be compiled in the very slow development mode.
235-
new webpack.DefinePlugin(env),
234+
new webpack.DefinePlugin(env.stringified),
236235
// This helps ensure the builds are consistent if source hasn't changed:
237236
new webpack.optimize.OccurrenceOrderPlugin(),
238237
// Try to dedupe duplicated modules, if any:

0 commit comments

Comments
 (0)