start.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. 'use strict';
  2. // Do this as the first thing so that any code reading it knows the right env.
  3. process.env.BABEL_ENV = 'development';
  4. process.env.NODE_ENV = 'development';
  5. // Makes the script crash on unhandled rejections instead of silently
  6. // ignoring them. In the future, promise rejections that are not handled will
  7. // terminate the Node.js process with a non-zero exit code.
  8. process.on('unhandledRejection', err => {
  9. throw err;
  10. });
  11. // Ensure environment variables are read.
  12. require('../config/env');
  13. const fs = require('fs');
  14. const chalk = require('react-dev-utils/chalk');
  15. const webpack = require('webpack');
  16. const WebpackDevServer = require('webpack-dev-server');
  17. const clearConsole = require('react-dev-utils/clearConsole');
  18. const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
  19. const {
  20. choosePort,
  21. createCompiler,
  22. prepareProxy,
  23. prepareUrls,
  24. } = require('react-dev-utils/WebpackDevServerUtils');
  25. const openBrowser = require('react-dev-utils/openBrowser');
  26. const semver = require('semver');
  27. const paths = require('../config/paths');
  28. const configFactory = require('../config/webpack.config');
  29. const createDevServerConfig = require('../config/webpackDevServer.config');
  30. const getClientEnvironment = require('../config/env');
  31. const react = require(require.resolve('react', { paths: [paths.appPath] }));
  32. const env = getClientEnvironment(paths.publicUrlOrPath.slice(0, -1));
  33. const useYarn = fs.existsSync(paths.yarnLockFile);
  34. const isInteractive = process.stdout.isTTY;
  35. // Tools like Cloud9 rely on this.
  36. const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 4000;
  37. const HOST = process.env.HOST || '0.0.0.0';
  38. if (process.env.HOST) {
  39. console.log(
  40. chalk.cyan(
  41. `Attempting to bind to HOST environment variable: ${chalk.yellow(
  42. chalk.bold(process.env.HOST)
  43. )}`
  44. )
  45. );
  46. console.log(
  47. `If this was unintentional, check that you haven't mistakenly set it in your shell.`
  48. );
  49. console.log(
  50. `Learn more here: ${chalk.yellow('https://cra.link/advanced-config')}`
  51. );
  52. console.log();
  53. }
  54. // We require that you explicitly set browsers and do not fall back to
  55. // browserslist defaults.
  56. const { checkBrowsers } = require('react-dev-utils/browsersHelper');
  57. checkBrowsers(paths.appPath, isInteractive)
  58. .then(() => {
  59. // We attempt to use the default port but if it is busy, we offer the user to
  60. // run on a different port. `choosePort()` Promise resolves to the next free port.
  61. return choosePort(HOST, DEFAULT_PORT);
  62. })
  63. .then(port => {
  64. if (port == null) {
  65. // We have not found a port.
  66. return;
  67. }
  68. const config = configFactory('development');
  69. const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
  70. const appName = require(paths.appPackageJson).name;
  71. const useTypeScript = fs.existsSync(paths.appTsConfig);
  72. const urls = prepareUrls(
  73. protocol,
  74. HOST,
  75. port,
  76. paths.publicUrlOrPath.slice(0, -1)
  77. );
  78. // Create a webpack compiler that is configured with custom messages.
  79. const compiler = createCompiler({
  80. appName,
  81. config,
  82. urls,
  83. useYarn,
  84. useTypeScript,
  85. webpack,
  86. });
  87. // Load proxy config
  88. const proxySetting = require(paths.appPackageJson).proxy;
  89. const proxyConfig = prepareProxy(
  90. proxySetting,
  91. paths.appPublic,
  92. paths.publicUrlOrPath
  93. );
  94. // Serve webpack assets generated by the compiler over a web server.
  95. const serverConfig = {
  96. ...createDevServerConfig(proxyConfig, urls.lanUrlForConfig),
  97. host: HOST,
  98. port,
  99. };
  100. const devServer = new WebpackDevServer(serverConfig, compiler);
  101. // Launch WebpackDevServer.
  102. devServer.startCallback(() => {
  103. if (isInteractive) {
  104. clearConsole();
  105. }
  106. if (env.raw.FAST_REFRESH && semver.lt(react.version, '16.10.0')) {
  107. console.log(
  108. chalk.yellow(
  109. `Fast Refresh requires React 16.10 or higher. You are using React ${react.version}.`
  110. )
  111. );
  112. }
  113. console.log(chalk.cyan('Starting the development server...\n'));
  114. // openBrowser(urls.localUrlForBrowser);
  115. });
  116. ['SIGINT', 'SIGTERM'].forEach(function (sig) {
  117. process.on(sig, function () {
  118. devServer.close();
  119. process.exit();
  120. });
  121. });
  122. if (process.env.CI !== 'true') {
  123. // Gracefully exit when stdin ends
  124. process.stdin.on('end', function () {
  125. devServer.close();
  126. process.exit();
  127. });
  128. }
  129. })
  130. .catch(err => {
  131. if (err && err.message) {
  132. console.log(err.message);
  133. }
  134. process.exit(1);
  135. });