UNPKG

3.75 kBJavaScriptView Raw
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
11// Do this as the first thing so that any code reading it knows the right env.
12process.env.BABEL_ENV = 'development';
13process.env.NODE_ENV = 'development';
14
15// Makes the script crash on unhandled rejections instead of silently
16// ignoring them. In the future, promise rejections that are not handled will
17// terminate the Node.js process with a non-zero exit code.
18process.on('unhandledRejection', err => {
19 throw err;
20});
21
22// Ensure environment variables are read.
23require('../config/env');
24
25const fs = require('fs');
26const chalk = require('chalk');
27const webpack = require('webpack');
28const WebpackDevServer = require('webpack-dev-server');
29const clearConsole = require('react-dev-utils/clearConsole');
30const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
31const {
32 choosePort,
33 createCompiler,
34 prepareProxy,
35 prepareUrls,
36} = require('react-dev-utils/WebpackDevServerUtils');
37const openBrowser = require('react-dev-utils/openBrowser');
38const paths = require('../config/paths');
39const config = require('../config/webpack.config.dev');
40const createDevServerConfig = require('../config/webpackDevServer.config');
41
42const useYarn = fs.existsSync(paths.yarnLockFile);
43const isInteractive = process.stdout.isTTY;
44
45// Warn and crash if required files are missing
46if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
47 process.exit(1);
48}
49
50// Tools like Cloud9 rely on this.
51const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
52const HOST = process.env.HOST || '0.0.0.0';
53
54if (process.env.HOST) {
55 console.log(
56 chalk.cyan(
57 `Attempting to bind to HOST environment variable: ${chalk.yellow(
58 chalk.bold(process.env.HOST)
59 )}`
60 )
61 );
62 console.log(
63 `If this was unintentional, check that you haven't mistakenly set it in your shell.`
64 );
65 console.log(`Learn more here: ${chalk.yellow('http://bit.ly/2mwWSwH')}`);
66 console.log();
67}
68
69// We attempt to use the default port but if it is busy, we offer the user to
70// run on a different port. `choosePort()` Promise resolves to the next free port.
71choosePort(HOST, DEFAULT_PORT)
72 .then(port => {
73 if (port == null) {
74 // We have not found a port.
75 return;
76 }
77 const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
78 const appName = require(paths.appPackageJson).name;
79 const urls = prepareUrls(protocol, HOST, port);
80 // Create a webpack compiler that is configured with custom messages.
81 const compiler = createCompiler(webpack, config, appName, urls, useYarn);
82 // Load proxy config
83 const proxySetting = require(paths.appPackageJson).proxy;
84 const proxyConfig = prepareProxy(proxySetting, paths.appPublic);
85 // Serve webpack assets generated by the compiler over a web sever.
86 const serverConfig = createDevServerConfig(
87 proxyConfig,
88 urls.lanUrlForConfig
89 );
90 const devServer = new WebpackDevServer(compiler, serverConfig);
91 // Launch WebpackDevServer.
92 devServer.listen(port, HOST, err => {
93 if (err) {
94 return console.log(err);
95 }
96 if (isInteractive) {
97 clearConsole();
98 }
99 console.log(chalk.cyan('Starting the development server...\n'));
100 openBrowser(urls.localUrlForBrowser);
101 });
102
103 ['SIGINT', 'SIGTERM'].forEach(function(sig) {
104 process.on(sig, function() {
105 devServer.close();
106 process.exit();
107 });
108 });
109 })
110 .catch(err => {
111 if (err && err.message) {
112 console.log(err.message);
113 }
114 process.exit(1);
115 });