Keine Beschreibung

env.js 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. Variable expansion is supported in .env files.
  26. // https://github.com/motdotla/dotenv
  27. // https://github.com/motdotla/dotenv-expand
  28. dotenvFiles.forEach(dotenvFile => {
  29. if (fs.existsSync(dotenvFile)) {
  30. require('dotenv-expand')(
  31. require('dotenv').config({
  32. path: dotenvFile,
  33. })
  34. );
  35. }
  36. });
  37. // We support resolving modules according to `NODE_PATH`.
  38. // This lets you use absolute paths in imports inside large monorepos:
  39. // https://github.com/facebookincubator/create-react-app/issues/253.
  40. // It works similar to `NODE_PATH` in Node itself:
  41. // https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
  42. // Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored.
  43. // Otherwise, we risk importing Node.js core modules into an app instead of Webpack shims.
  44. // https://github.com/facebookincubator/create-react-app/issues/1023#issuecomment-265344421
  45. // We also resolve them to make sure all tools using them work consistently.
  46. const appDirectory = fs.realpathSync(process.cwd());
  47. process.env.NODE_PATH = (process.env.NODE_PATH || '')
  48. .split(path.delimiter)
  49. .filter(folder => folder && !path.isAbsolute(folder))
  50. .map(folder => path.resolve(appDirectory, folder))
  51. .join(path.delimiter);
  52. // Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
  53. // injected into the application via DefinePlugin in Webpack configuration.
  54. const REACT_APP = /^REACT_APP_/i;
  55. function getClientEnvironment(publicUrl) {
  56. const raw = Object.keys(process.env)
  57. .filter(key => REACT_APP.test(key))
  58. .reduce(
  59. (env, key) => {
  60. env[key] = process.env[key];
  61. return env;
  62. },
  63. {
  64. // Useful for determining whether we’re running in production mode.
  65. // Most importantly, it switches React into the correct mode.
  66. NODE_ENV: process.env.NODE_ENV || 'development',
  67. // Useful for resolving the correct path to static assets in `public`.
  68. // For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />.
  69. // This should only be used as an escape hatch. Normally you would put
  70. // images into the `src` and `import` them in code to get their paths.
  71. PUBLIC_URL: publicUrl,
  72. }
  73. );
  74. // Stringify all values so we can feed into Webpack DefinePlugin
  75. const stringified = {
  76. 'process.env': Object.keys(raw).reduce((env, key) => {
  77. env[key] = JSON.stringify(raw[key]);
  78. return env;
  79. }, {}),
  80. };
  81. return { raw, stringified };
  82. }
  83. module.exports = getClientEnvironment;