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