UNPKG

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