start.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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('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 paths = require('../config/paths');
  27. const config = require('../config/webpack.config.dev');
  28. const createDevServerConfig = require('../config/webpackDevServer.config');
  29. const useYarn = fs.existsSync(paths.yarnLockFile);
  30. const isInteractive = process.stdout.isTTY;
  31. // Warn and crash if required files are missing
  32. if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
  33. process.exit(1);
  34. }
  35. // Tools like Cloud9 rely on this.
  36. const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
  37. const HOST = process.env.HOST || '0.0.0.0';
  38. // We attempt to use the default port but if it is busy, we offer the user to
  39. // run on a different port. `detect()` Promise resolves to the next free port.
  40. choosePort(HOST, DEFAULT_PORT)
  41. .then(port => {
  42. if (port == null) {
  43. // We have not found a port.
  44. return;
  45. }
  46. const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
  47. const appName = require(paths.appPackageJson).name;
  48. const urls = prepareUrls(protocol, HOST, port);
  49. // Create a webpack compiler that is configured with custom messages.
  50. const compiler = createCompiler(webpack, config, appName, urls, useYarn);
  51. // Load proxy config
  52. const proxySetting = require(paths.appPackageJson).proxy;
  53. const proxyConfig = prepareProxy(proxySetting, paths.appPublic);
  54. // Serve webpack assets generated by the compiler over a web sever.
  55. const serverConfig = createDevServerConfig(
  56. proxyConfig,
  57. urls.lanUrlForConfig
  58. );
  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(function(sig) {
  72. process.on(sig, function() {
  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. });