start.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. // Warn and crash if required files are missing
  36. if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
  37. process.exit(1);
  38. }
  39. // Tools like Cloud9 rely on this.
  40. const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
  41. const HOST = process.env.HOST || '0.0.0.0';
  42. if (process.env.HOST) {
  43. console.log(
  44. chalk.cyan(
  45. `Attempting to bind to HOST environment variable: ${chalk.yellow(
  46. chalk.bold(process.env.HOST)
  47. )}`
  48. )
  49. );
  50. console.log(
  51. `If this was unintentional, check that you haven't mistakenly set it in your shell.`
  52. );
  53. console.log(
  54. `Learn more here: ${chalk.yellow('https://cra.link/advanced-config')}`
  55. );
  56. console.log();
  57. }
  58. // We require that you explicitly set browsers and do not fall back to
  59. // browserslist defaults.
  60. const { checkBrowsers } = require('react-dev-utils/browsersHelper');
  61. checkBrowsers(paths.appPath, isInteractive)
  62. .then(() => {
  63. // We attempt to use the default port but if it is busy, we offer the user to
  64. // run on a different port. `choosePort()` Promise resolves to the next free port.
  65. return choosePort(HOST, DEFAULT_PORT);
  66. })
  67. .then(port => {
  68. if (port == null) {
  69. // We have not found a port.
  70. return;
  71. }
  72. const config = configFactory('development');
  73. const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
  74. const appName = require(paths.appPackageJson).name;
  75. const useTypeScript = fs.existsSync(paths.appTsConfig);
  76. const tscCompileOnError = process.env.TSC_COMPILE_ON_ERROR === 'true';
  77. const urls = prepareUrls(
  78. protocol,
  79. HOST,
  80. port,
  81. paths.publicUrlOrPath.slice(0, -1)
  82. );
  83. const devSocket = {
  84. warnings: warnings =>
  85. devServer.sockWrite(devServer.sockets, 'warnings', warnings),
  86. errors: errors =>
  87. devServer.sockWrite(devServer.sockets, 'errors', errors),
  88. };
  89. // Create a webpack compiler that is configured with custom messages.
  90. const compiler = createCompiler({
  91. appName,
  92. config,
  93. devSocket,
  94. urls,
  95. useYarn,
  96. useTypeScript,
  97. tscCompileOnError,
  98. webpack,
  99. });
  100. // Load proxy config
  101. const proxySetting = require(paths.appPackageJson).proxy;
  102. const proxyConfig = prepareProxy(
  103. proxySetting,
  104. paths.appPublic,
  105. paths.publicUrlOrPath
  106. );
  107. // Serve webpack assets generated by the compiler over a web server.
  108. const serverConfig = createDevServerConfig(
  109. proxyConfig,
  110. urls.lanUrlForConfig
  111. );
  112. const devServer = new WebpackDevServer(compiler, serverConfig);
  113. // Launch WebpackDevServer.
  114. devServer.listen(port, HOST, err => {
  115. if (err) {
  116. return console.log(err);
  117. }
  118. if (isInteractive) {
  119. clearConsole();
  120. }
  121. if (env.raw.FAST_REFRESH && semver.lt(react.version, '16.10.0')) {
  122. console.log(
  123. chalk.yellow(
  124. `Fast Refresh requires React 16.10 or higher. You are using React ${react.version}.`
  125. )
  126. );
  127. }
  128. console.log(chalk.cyan('Starting the development server...\n'));
  129. openBrowser(urls.localUrlForBrowser);
  130. });
  131. ['SIGINT', 'SIGTERM'].forEach(function (sig) {
  132. process.on(sig, function () {
  133. devServer.close();
  134. process.exit();
  135. });
  136. });
  137. if (process.env.CI !== 'true') {
  138. // Gracefully exit when stdin ends
  139. process.stdin.on('end', function () {
  140. devServer.close();
  141. process.exit();
  142. });
  143. }
  144. })
  145. .catch(err => {
  146. if (err && err.message) {
  147. console.log(err.message);
  148. }
  149. process.exit(1);
  150. });