重构xuchang-screen,从umi->cra, 计划引入redux
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

105 lines
4.1 KiB

  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. const dotenvFiles = [
  15. `${paths.dotenv}.${NODE_ENV}.local`,
  16. // Don't include `.env.local` for `test` environment
  17. // since normally you expect tests to produce the same
  18. // results for everyone
  19. NODE_ENV !== 'test' && `${paths.dotenv}.local`,
  20. `${paths.dotenv}.${NODE_ENV}`,
  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/facebook/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/facebook/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. // We support configuring the sockjs pathname during development.
  73. // These settings let a developer run multiple simultaneous projects.
  74. // They are used as the connection `hostname`, `pathname` and `port`
  75. // in webpackHotDevClient. They are used as the `sockHost`, `sockPath`
  76. // and `sockPort` options in webpack-dev-server.
  77. WDS_SOCKET_HOST: process.env.WDS_SOCKET_HOST,
  78. WDS_SOCKET_PATH: process.env.WDS_SOCKET_PATH,
  79. WDS_SOCKET_PORT: process.env.WDS_SOCKET_PORT,
  80. // Whether or not react-refresh is enabled.
  81. // It is defined here so it is available in the webpackHotDevClient.
  82. FAST_REFRESH: process.env.FAST_REFRESH !== 'false',
  83. }
  84. );
  85. // Stringify all values so we can feed into webpack DefinePlugin
  86. const stringified = {
  87. 'process.env': Object.keys(raw).reduce((env, key) => {
  88. env[key] = JSON.stringify(raw[key]);
  89. return env;
  90. }, {}),
  91. };
  92. return { raw, stringified };
  93. }
  94. module.exports = getClientEnvironment;