env.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // @remove-on-eject-begin
  2. /**
  3. * Copyright (c) 2015-present, Facebook, Inc.
  4. *
  5. * This source code is licensed under the MIT license found in the
  6. * LICENSE file in the root directory of this source tree.
  7. */
  8. // @remove-on-eject-end
  9. 'use strict';
  10. const fs = require('fs');
  11. const path = require('path');
  12. const paths = require('./paths');
  13. // Make sure that including paths.js after env.js will read .env variables.
  14. delete require.cache[require.resolve('./paths')];
  15. const NODE_ENV = process.env.NODE_ENV;
  16. if (!NODE_ENV) {
  17. throw new Error(
  18. 'The NODE_ENV environment variable is required but was not specified.'
  19. );
  20. }
  21. // https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
  22. const dotenvFiles = [
  23. `${paths.dotenv}.${NODE_ENV}.local`,
  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}.${NODE_ENV}`,
  29. paths.dotenv,
  30. ].filter(Boolean);
  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. // We support resolving modules according to `NODE_PATH`.
  46. // This lets you use absolute paths in imports inside large monorepos:
  47. // https://github.com/facebook/create-react-app/issues/253.
  48. // It works similar to `NODE_PATH` in Node itself:
  49. // https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
  50. // Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored.
  51. // Otherwise, we risk importing Node.js core modules into an app instead of webpack shims.
  52. // https://github.com/facebook/create-react-app/issues/1023#issuecomment-265344421
  53. // We also resolve them to make sure all tools using them work consistently.
  54. const appDirectory = fs.realpathSync(process.cwd());
  55. process.env.NODE_PATH = (process.env.NODE_PATH || '')
  56. .split(path.delimiter)
  57. .filter(folder => folder && !path.isAbsolute(folder))
  58. .map(folder => path.resolve(appDirectory, folder))
  59. .join(path.delimiter);
  60. // Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
  61. // injected into the application via DefinePlugin in webpack configuration.
  62. const REACT_APP = /^REACT_APP_/i;
  63. function getClientEnvironment(publicUrl) {
  64. const raw = Object.keys(process.env)
  65. .filter(key => REACT_APP.test(key))
  66. .reduce(
  67. (env, key) => {
  68. env[key] = process.env[key];
  69. return env;
  70. },
  71. {
  72. // Useful for determining whether we’re running in production mode.
  73. // Most importantly, it switches React into the correct mode.
  74. NODE_ENV: process.env.NODE_ENV || 'development',
  75. // Useful for resolving the correct path to static assets in `public`.
  76. // For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />.
  77. // This should only be used as an escape hatch. Normally you would put
  78. // images into the `src` and `import` them in code to get their paths.
  79. PUBLIC_URL: publicUrl,
  80. // We support configuring the sockjs pathname during development.
  81. // These settings let a developer run multiple simultaneous projects.
  82. // They are used as the connection `hostname`, `pathname` and `port`
  83. // in webpackHotDevClient. They are used as the `sockHost`, `sockPath`
  84. // and `sockPort` options in webpack-dev-server.
  85. WDS_SOCKET_HOST: process.env.WDS_SOCKET_HOST,
  86. WDS_SOCKET_PATH: process.env.WDS_SOCKET_PATH,
  87. WDS_SOCKET_PORT: process.env.WDS_SOCKET_PORT,
  88. // Whether or not react-refresh is enabled.
  89. // react-refresh is not 100% stable at this time,
  90. // which is why it's disabled by default.
  91. // It is defined here so it is available in the webpackHotDevClient.
  92. FAST_REFRESH: process.env.FAST_REFRESH !== 'false',
  93. }
  94. );
  95. // Stringify all values so we can feed into webpack DefinePlugin
  96. const stringified = {
  97. 'process.env': Object.keys(raw).reduce((env, key) => {
  98. env[key] = JSON.stringify(raw[key]);
  99. return env;
  100. }, {}),
  101. };
  102. return { raw, stringified };
  103. }
  104. module.exports = getClientEnvironment;