UNPKG

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