start.js 4.8 KB

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