env.js 3.3 KB

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