You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We can skip all the complex part of that operation as we'll only need to modify the second argument to the reduce function, in other words, we'll update the object:
Next, let's concatenate all these variables together as well as include our `NODE_ENV` option in this object. The `Object.assign()` method creates a _new_ object and merges each object from right to left. This way, the environment config variable
314
340
315
341
```javascript
316
-
constallVars=Object.assign({}, {
317
-
'NODE_ENV':NODE_ENV
318
-
}, globalDotEnv, envDotEnv);
342
+
constallVars=Object.assign(
343
+
{},
344
+
{
345
+
NODE_ENV:NODE_ENV
346
+
},
347
+
globalDotEnv.parsed,
348
+
envDotEnv.parsed
349
+
);
319
350
```
320
351
321
352
With our current setup, the `allVars` variable will look like:
@@ -327,39 +358,34 @@ With our current setup, the `allVars` variable will look like:
327
358
}
328
359
```
329
360
330
-
Finally, let's create an object that puts these variables on `process.env` and ensures they are valid strings (using `JSON.stringify`).
331
-
332
-
```javascript
333
-
constinitialVariableObject=
334
-
Object.keys(allVars)
335
-
.reduce((memo, key) => {
336
-
memo['process.env.'+key.toUpperCase()] =
337
-
JSON.stringify(allVars[key]);
338
-
return memo;
339
-
}, {});
340
-
```
341
-
342
-
With our current setup (with the `.env` file in the root), this creates the `initialVariableObject` to be the following object:
361
+
Now we can add this `allVars` as an argument to the reduce function initial value called in the `raw` variable in the `getClientEnvironment` function. Let's update it to use this object:
343
362
344
363
```javascript
345
-
{
346
-
'process.env.NODE_ENV':'"development"',
347
-
'process.env.APP_NAME':'"30days"'
364
+
functiongetClientEnvironment(publicUrl) {
365
+
// ...
366
+
constraw=Object.keys(process.env)
367
+
.filter(key=>REACT_APP.test(key))
368
+
.reduce(
369
+
(env, key) => {
370
+
env[key] =process.env[key];
371
+
return env;
372
+
},
373
+
{
374
+
// Useful for determining whether we’re running in production mode.
375
+
// Most importantly, it switches React into the correct mode.
376
+
NODE_ENV:process.env.NODE_ENV||"development",
377
+
// Useful for resolving the correct path to static assets in `public`.
378
+
// For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />.
379
+
// This should only be used as an escape hatch. Normally you would put
380
+
// images into the `src` and `import` them in code to get their paths.
381
+
PUBLIC_URL: publicUrl,
382
+
...allVars
383
+
}
384
+
);
385
+
// ...
348
386
}
349
387
```
350
388
351
-
Now we can use this `initialVariableObject` as the second argument for the original `module.exports` like. Let's update it to use this object:
Now, for our production deployment, we'll use the heroku app, so let's create a copy of the `development.config.env` file as `production.config.env` in the `config/` directory:
Now that we have the `process.env.NODE_ENV` available to us in our files, we can update the `middleware` array depending upon the environment we're running in. Let's update it to only add the logging if we are in the development environment:
0 commit comments