UNPKG

4.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// @remove-on-eject-begin
25// Do the preflight check (only happens before eject).
26const verifyPackageTree = require('./utils/verifyPackageTree');
27if (process.env.SKIP_PREFLIGHT_CHECK !== 'true') {
28 verifyPackageTree();
29}
30const verifyTypeScriptSetup = require('./utils/verifyTypeScriptSetup');
31verifyTypeScriptSetup();
32// @remove-on-eject-end
33
34const fs = require('fs');
35const chalk = require('chalk');
36const webpack = require('webpack');
37const WebpackDevServer = require('webpack-dev-server');
38const clearConsole = require('react-dev-utils/clearConsole');
39const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
40const {
41 choosePort,
42 createCompiler,
43 prepareProxy,
44 prepareUrls,
45} = require('react-dev-utils/WebpackDevServerUtils');
46const openBrowser = require('react-dev-utils/openBrowser');
47const paths = require('../config/paths');
48const config = require('../config/webpack.config.dev');
49const createDevServerConfig = require('../config/webpackDevServer.config');
50
51const useYarn = fs.existsSync(paths.yarnLockFile);
52const isInteractive = process.stdout.isTTY;
53
54// Warn and crash if required files are missing
55if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
56 process.exit(1);
57}
58
59// Tools like Cloud9 rely on this.
60const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
61const HOST = process.env.HOST || '0.0.0.0';
62
63if (process.env.HOST) {
64 console.log(
65 chalk.cyan(
66 `Attempting to bind to HOST environment variable: ${chalk.yellow(
67 chalk.bold(process.env.HOST)
68 )}`
69 )
70 );
71 console.log(
72 `If this was unintentional, check that you haven't mistakenly set it in your shell.`
73 );
74 console.log(
75 `Learn more here: ${chalk.yellow('http://bit.ly/CRA-advanced-config')}`
76 );
77 console.log();
78}
79
80// We require that you explictly set browsers and do not fall back to
81// browserslist defaults.
82const { checkBrowsers } = require('react-dev-utils/browsersHelper');
83checkBrowsers(paths.appPath, isInteractive)
84 .then(() => {
85 // We attempt to use the default port but if it is busy, we offer the user to
86 // run on a different port. `choosePort()` Promise resolves to the next free port.
87 return choosePort(HOST, DEFAULT_PORT);
88 })
89 .then(port => {
90 if (port == null) {
91 // We have not found a port.
92 return;
93 }
94 const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
95 const appName = require(paths.appPackageJson).name;
96 const urls = prepareUrls(protocol, HOST, port);
97 // Create a webpack compiler that is configured with custom messages.
98 const compiler = createCompiler(webpack, config, appName, urls, useYarn);
99 // Load proxy config
100 const proxySetting = require(paths.appPackageJson).proxy;
101 const proxyConfig = prepareProxy(proxySetting, paths.appPublic);
102 // Serve webpack assets generated by the compiler over a web server.
103 const serverConfig = createDevServerConfig(
104 proxyConfig,
105 urls.lanUrlForConfig
106 );
107 const devServer = new WebpackDevServer(compiler, serverConfig);
108 // Launch WebpackDevServer.
109 devServer.listen(port, HOST, err => {
110 if (err) {
111 return console.log(err);
112 }
113 if (isInteractive) {
114 clearConsole();
115 }
116 console.log(chalk.cyan('Starting the development server...\n'));
117 openBrowser(urls.localUrlForBrowser);
118 });
119
120 ['SIGINT', 'SIGTERM'].forEach(function(sig) {
121 process.on(sig, function() {
122 devServer.close();
123 process.exit();
124 });
125 });
126 })
127 .catch(err => {
128 if (err && err.message) {
129 console.log(err.message);
130 }
131 process.exit(1);
132 });