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