start.js 5.5 KB

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