env.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. // Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
  3. // injected into the application via DefinePlugin in Webpack configuration.
  4. var REACT_APP = /^REACT_APP_/i;
  5. function getClientEnvironment(publicUrl) {
  6. var raw = Object
  7. .keys(process.env)
  8. .filter(key => REACT_APP.test(key))
  9. .reduce((env, key) => {
  10. env[key] = process.env[key];
  11. return env;
  12. }, {
  13. // Useful for determining whether we’re running in production mode.
  14. // Most importantly, it switches React into the correct mode.
  15. 'NODE_ENV': process.env.NODE_ENV || 'development',
  16. // Useful for resolving the correct path to static assets in `public`.
  17. // For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />.
  18. // This should only be used as an escape hatch. Normally you would put
  19. // images into the `src` and `import` them in code to get their paths.
  20. 'PUBLIC_URL': publicUrl
  21. });
  22. // Stringify all values so we can feed into Webpack DefinePlugin
  23. var stringified = {
  24. 'process.env': Object
  25. .keys(raw)
  26. .reduce((env, key) => {
  27. env[key] = JSON.stringify(raw[key]);
  28. return env;
  29. }, {})
  30. };
  31. return { raw, stringified };
  32. }
  33. module.exports = getClientEnvironment;