UNPKG

3.24 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.
15
16const chalk = require("chalk");
17const webpack = require("webpack");
18const WebpackDevServer = require("webpack-dev-server");
19const clearConsole = require("react-dev-utils/clearConsole");
20const checkRequiredFiles = require("react-dev-utils/checkRequiredFiles");
21const {
22 choosePort,
23 createCompiler,
24 prepareProxy,
25 prepareUrls
26} = require("../utils/WebpackDevServerUtils");
27const openBrowser = require("react-dev-utils/openBrowser");
28const paths = require("../config/paths");
29const config = require("../config/webpack.dev.config");
30const createDevServerConfig = require("../config/devServer.config");
31
32const isInteractive = process.stdout.isTTY;
33
34// Warn and crash if required files are missing
35if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
36 process.exit(1);
37}
38
39const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
40const HOST = process.env.HOST || "0.0.0.0";
41
42if (process.env.HOST) {
43 console.log(
44 chalk.cyan(
45 `Attempting to bind to HOST environment variable: ${chalk.yellow(
46 chalk.bold(process.env.HOST)
47 )}`
48 )
49 );
50 console.log(
51 `If this was unintentional, check that you haven't mistakenly set it in your shell.`
52 );
53 console.log(
54 `Learn more here: ${chalk.yellow("http://bit.ly/CRA-advanced-config")}`
55 );
56 console.log();
57}
58
59choosePort(HOST, DEFAULT_PORT)
60 .then(port => {
61 if (port == null) {
62 // We have not found a port.
63 return;
64 }
65 const protocol = process.env.HTTPS === "true" ? "https" : "http";
66 const appName = require(paths.appPackageJson).name;
67 const urls = prepareUrls(protocol, HOST, port);
68 // Load proxy config
69 const proxySetting = require(paths.appPackageJson).proxy;
70 const proxyConfig = prepareProxy(proxySetting, paths.appPublic);
71
72 // Create a webpack compiler that is configured with custom messages.
73 const compiler = createCompiler(webpack, config, appName, urls, true,proxyConfig);
74
75 // Serve webpack assets generated by the compiler over a web server.
76 const serverConfig = createDevServerConfig(
77 proxyConfig,
78 urls.lanUrlForConfig
79 );
80 const devServer = new WebpackDevServer(compiler, serverConfig);
81
82 // Launch WebpackDevServer.
83 devServer.listen(port, HOST, err => {
84 if (err) {
85 return console.log(err);
86 }
87 if (isInteractive) {
88 clearConsole();
89 }
90 console.log(chalk.cyan("Starting the development server...\n"));
91 openBrowser(urls.localUrlForBrowser);
92 });
93
94 ["SIGINT", "SIGTERM"].forEach(function(sig) {
95 process.on(sig, function() {
96 devServer.close();
97 process.exit();
98 });
99 });
100 })
101 .catch(err => {
102 if (err && err.message) {
103 console.log(err.message);
104 }
105 process.exit(1);
106 });