UNPKG

3.36 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
54// We attempt to use the default port but if it is busy, we offer the user to
55// run on a different port. `detect()` Promise resolves to the next free port.
56choosePort(HOST, DEFAULT_PORT)
57 .then(port => {
58 if (port == null) {
59 // We have not found a port.
60 return;
61 }
62 const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
63 const appName = require(paths.appPackageJson).name;
64 const urls = prepareUrls(protocol, HOST, port);
65 // Create a webpack compiler that is configured with custom messages.
66 const compiler = createCompiler(webpack, config, appName, urls, useYarn);
67 // Load proxy config
68 const proxySetting = require(paths.appPackageJson).proxy;
69 const proxyConfig = prepareProxy(proxySetting, paths.appPublic);
70 // Serve webpack assets generated by the compiler over a web sever.
71 const serverConfig = createDevServerConfig(
72 proxyConfig,
73 urls.lanUrlForConfig
74 );
75 const devServer = new WebpackDevServer(compiler, serverConfig);
76 // Launch WebpackDevServer.
77 devServer.listen(port, HOST, err => {
78 if (err) {
79 return console.log(err);
80 }
81 if (isInteractive) {
82 clearConsole();
83 }
84 console.log(chalk.cyan('Starting the development server...\n'));
85 openBrowser(urls.localUrlForBrowser);
86 });
87
88 ['SIGINT', 'SIGTERM'].forEach(function(sig) {
89 process.on(sig, function() {
90 devServer.close();
91 process.exit();
92 });
93 });
94 })
95 .catch(err => {
96 if (err && err.message) {
97 console.log(err.message);
98 }
99 process.exit(1);
100 });