start.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*eslint-disable no-console */
  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('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 { choosePort, createCompiler, prepareProxy, prepareUrls } = require('react-dev-utils/WebpackDevServerUtils');
  20. const openBrowser = require('react-dev-utils/openBrowser');
  21. const paths = require('../config/paths');
  22. const config = require('../config/webpack.config.dev');
  23. const createDevServerConfig = require('../config/webpack.config.devserver');
  24. const useYarn = fs.existsSync(paths.yarnLockFile);
  25. const isInteractive = process.stdout.isTTY;
  26. // Warn and crash if required files are missing
  27. if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
  28. process.exit(1);
  29. }
  30. // Tools like Cloud9 rely on this.
  31. const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
  32. const HOST = process.env.HOST || '0.0.0.0';
  33. if (process.env.HOST) {
  34. console.log(
  35. chalk.cyan(`Attempting to bind to HOST environment variable: ${chalk.yellow(chalk.bold(process.env.HOST))}`)
  36. );
  37. console.log(`If this was unintentional, check that you haven't mistakenly set it in your shell.`);
  38. console.log(`Learn more here: ${chalk.yellow('http://bit.ly/2mwWSwH')}`);
  39. console.log();
  40. }
  41. // We attempt to use the default port but if it is busy, we offer the user to
  42. // run on a different port. `choosePort()` Promise resolves to the next free port.
  43. choosePort(HOST, DEFAULT_PORT)
  44. .then(port => {
  45. if (port == null) {
  46. // We have not found a port.
  47. return;
  48. }
  49. const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
  50. const appName = require(paths.appPackageJson).name;
  51. const urls = prepareUrls(protocol, HOST, port);
  52. // Create a webpack compiler that is configured with custom messages.
  53. const compiler = createCompiler(webpack, config, appName, urls, useYarn);
  54. // Load proxy config
  55. const proxySetting = require(paths.appPackageJson).proxy;
  56. const proxyConfig = prepareProxy(proxySetting, paths.appPublic);
  57. // Serve webpack assets generated by the compiler over a web sever.
  58. const serverConfig = createDevServerConfig(proxyConfig, urls.lanUrlForConfig);
  59. const devServer = new WebpackDevServer(compiler, serverConfig);
  60. // Launch WebpackDevServer.
  61. devServer.listen(port, HOST, err => {
  62. if (err) {
  63. return console.log(err);
  64. }
  65. if (isInteractive) {
  66. // clearConsole();
  67. }
  68. console.log(chalk.cyan('Starting the development server...\n'));
  69. openBrowser(urls.localUrlForBrowser);
  70. });
  71. ['SIGINT', 'SIGTERM'].forEach(sig => {
  72. process.on(sig, () => {
  73. devServer.close();
  74. process.exit();
  75. });
  76. });
  77. })
  78. .catch(err => {
  79. if (err && err.message) {
  80. console.log(err.message);
  81. }
  82. process.exit(1);
  83. });