|
1 | | -'use strict'; |
| 1 | + |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | +const paths = require('./paths'); |
| 6 | + |
| 7 | +// Make sure that including paths.js after env.js will read .env variables. |
| 8 | +delete require.cache[require.resolve('./paths')]; |
| 9 | + |
| 10 | +let NODE_ENV = process.env.NODE_ENV; |
| 11 | +if (!NODE_ENV) { |
| 12 | + process.env.NODE_ENV = 'development'; |
| 13 | + NODE_ENV = 'development'; |
| 14 | +} |
| 15 | + |
| 16 | +// https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use |
| 17 | +var dotenvFiles = [ |
| 18 | + `${paths.dotenv}.${NODE_ENV}.local`, |
| 19 | + `${paths.dotenv}.${NODE_ENV}`, |
| 20 | + // Don't include `.env.local` for `test` environment |
| 21 | + // since normally you expect tests to produce the same |
| 22 | + // results for everyone |
| 23 | + NODE_ENV !== 'test' && `${paths.dotenv}.local`, |
| 24 | + paths.dotenv |
| 25 | +].filter(Boolean); |
| 26 | + |
| 27 | +// Load environment variables from .env* files. Suppress warnings using silent |
| 28 | +// if this file is missing. dotenv will never modify any environment variables |
| 29 | +// that have already been set. |
| 30 | +// https://github.com/motdotla/dotenv |
| 31 | +dotenvFiles.forEach((dotenvFile) => { |
| 32 | + if (fs.existsSync(dotenvFile)) { |
| 33 | + require('dotenv').config({ |
| 34 | + path: dotenvFile |
| 35 | + }); |
| 36 | + } |
| 37 | +}); |
| 38 | + |
| 39 | +// We support resolving modules according to `NODE_PATH`. |
| 40 | +// This lets you use absolute paths in imports inside large monorepos: |
| 41 | +// https://github.com/facebookincubator/create-react-app/issues/253. |
| 42 | +// It works similar to `NODE_PATH` in Node itself: |
| 43 | +// https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders |
| 44 | +// Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored. |
| 45 | +// Otherwise, we risk importing Node.js core modules into an app instead of Webpack shims. |
| 46 | +// https://github.com/facebookincubator/create-react-app/issues/1023#issuecomment-265344421 |
| 47 | +// We also resolve them to make sure all tools using them work consistently. |
| 48 | +const appDirectory = fs.realpathSync(process.cwd()); |
| 49 | +process.env.NODE_PATH = (process.env.NODE_PATH || '') |
| 50 | + .split(path.delimiter) |
| 51 | + .filter(folder => folder && !path.isAbsolute(folder)) |
| 52 | + .map(folder => path.resolve(appDirectory, folder)) |
| 53 | + .join(path.delimiter); |
2 | 54 |
|
3 | 55 | // Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be |
4 | 56 | // injected into the application via DefinePlugin in Webpack configuration. |
5 | | - |
6 | | -var REACT_APP = /^REACT_APP_/i; |
| 57 | +const REACT_APP = /^REACT_APP_/i; |
7 | 58 |
|
8 | 59 | function getClientEnvironment(publicUrl) { |
9 | | - var raw = Object |
10 | | - .keys(process.env) |
| 60 | + const raw = Object.keys(process.env) |
11 | 61 | .filter(key => REACT_APP.test(key)) |
12 | | - .reduce((env, key) => { |
13 | | - env[key] = process.env[key]; |
14 | | - return env; |
15 | | - }, { |
16 | | - // Useful for determining whether we’re running in production mode. |
17 | | - // Most importantly, it switches React into the correct mode. |
18 | | - 'NODE_ENV': process.env.NODE_ENV || 'development', |
19 | | - // Useful for resolving the correct path to static assets in `public`. |
20 | | - // For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />. |
21 | | - // This should only be used as an escape hatch. Normally you would put |
22 | | - // images into the `src` and `import` them in code to get their paths. |
23 | | - 'PUBLIC_URL': publicUrl |
24 | | - }); |
25 | | - // Stringify all values so we can feed into Webpack DefinePlugin |
26 | | - var stringified = { |
27 | | - 'process.env': Object |
28 | | - .keys(raw) |
29 | | - .reduce((env, key) => { |
30 | | - env[key] = JSON.stringify(raw[key]); |
| 62 | + .reduce( |
| 63 | + (env, key) => { |
| 64 | + env[key] = process.env[key]; |
31 | 65 | return env; |
32 | | - }, {}) |
| 66 | + }, |
| 67 | + { |
| 68 | + // Useful for determining whether we’re running in production mode. |
| 69 | + // Most importantly, it switches React into the correct mode. |
| 70 | + NODE_ENV: process.env.NODE_ENV || 'development', |
| 71 | + // Useful for resolving the correct path to static assets in `public`. |
| 72 | + // For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />. |
| 73 | + // This should only be used as an escape hatch. Normally you would put |
| 74 | + // images into the `src` and `import` them in code to get their paths. |
| 75 | + PUBLIC_URL: publicUrl |
| 76 | + } |
| 77 | + ); |
| 78 | + // Stringify all values so we can feed into Webpack DefinePlugin |
| 79 | + const stringified = { |
| 80 | + 'process.env': Object.keys(raw).reduce((env, key) => { |
| 81 | + env[key] = JSON.stringify(raw[key]); |
| 82 | + return env; |
| 83 | + }, {}) |
33 | 84 | }; |
34 | 85 |
|
35 | 86 | return { raw, stringified }; |
|
0 commit comments