start.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Do this as the first thing so that any code reading it knows the right env.
  2. process.env.BABEL_ENV = 'development'
  3. process.env.NODE_ENV = 'development'
  4. // Makes the script crash on unhandled rejections instead of silently
  5. // ignoring them. In the future, promise rejections that are not handled will
  6. // terminate the Node.js process with a non-zero exit code.
  7. process.on('unhandledRejection', err => {
  8. throw err
  9. })
  10. // Ensure environment variables are read.
  11. require('../config/env')
  12. const fs = require('fs')
  13. const chalk = require('chalk')
  14. const webpack = require('webpack')
  15. const WebpackDevServer = require('webpack-dev-server')
  16. const clearConsole = require('react-dev-utils/clearConsole')
  17. const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles')
  18. const {
  19. choosePort,
  20. createCompiler,
  21. prepareProxy,
  22. prepareUrls
  23. } = require('react-dev-utils/WebpackDevServerUtils')
  24. const openBrowser = require('react-dev-utils/openBrowser')
  25. const paths = require('../config/paths')
  26. const config = require('../config/webpack.config.dev')
  27. const createDevServerConfig = require('../config/webpackDevServer.config')
  28. const useYarn = fs.existsSync(paths.yarnLockFile)
  29. const isInteractive = process.stdout.isTTY
  30. // Warn and crash if required files are missing
  31. if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
  32. process.exit(1)
  33. }
  34. // Tools like Cloud9 rely on this.
  35. const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000
  36. const HOST = process.env.HOST || '0.0.0.0'
  37. // We attempt to use the default port but if it is busy, we offer the user to
  38. // run on a different port. `detect()` Promise resolves to the next free port.
  39. choosePort(HOST, DEFAULT_PORT)
  40. .then(port => {
  41. if (port == null) {
  42. // We have not found a port.
  43. return
  44. }
  45. const protocol = process.env.HTTPS === 'true' ? 'https' : 'http'
  46. const appName = require(paths.appPackageJson).name
  47. const urls = prepareUrls(protocol, HOST, port)
  48. // Create a webpack compiler that is configured with custom messages.
  49. const compiler = createCompiler(webpack, config, appName, urls, useYarn)
  50. // Load proxy config
  51. const proxySetting = require(paths.appPackageJson).proxy
  52. const proxyConfig = prepareProxy(proxySetting, paths.appPublic)
  53. // Serve webpack assets generated by the compiler over a web sever.
  54. const serverConfig = createDevServerConfig(
  55. proxyConfig,
  56. urls.lanUrlForConfig
  57. )
  58. const devServer = new WebpackDevServer(compiler, serverConfig)
  59. // Launch WebpackDevServer.
  60. devServer.listen(port, HOST, err => {
  61. if (err) {
  62. return console.log(err)
  63. }
  64. if (isInteractive) {
  65. clearConsole()
  66. }
  67. console.log(chalk.cyan('Starting the development server...\n'))
  68. openBrowser(urls.localUrlForBrowser)
  69. })
  70. ;['SIGINT', 'SIGTERM'].forEach(sig => {
  71. process.on(sig, () => {
  72. devServer.close()
  73. process.exit()
  74. })
  75. })
  76. })
  77. .catch(err => {
  78. if (err && err.message) {
  79. console.log(err.message)
  80. }
  81. process.exit(1)
  82. })