1 | "use strict";
|
2 |
|
3 |
|
4 | process.env.BABEL_ENV = "development";
|
5 | process.env.NODE_ENV = "development";
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | process.on("unhandledRejection", err => {
|
11 | throw err;
|
12 | });
|
13 |
|
14 |
|
15 |
|
16 | const chalk = require("chalk");
|
17 | const webpack = require("webpack");
|
18 | const WebpackDevServer = require("webpack-dev-server");
|
19 | const clearConsole = require("react-dev-utils/clearConsole");
|
20 | const checkRequiredFiles = require("react-dev-utils/checkRequiredFiles");
|
21 | const {
|
22 | choosePort,
|
23 | createCompiler,
|
24 | prepareProxy,
|
25 | prepareUrls
|
26 | } = require("../utils/WebpackDevServerUtils");
|
27 | const openBrowser = require("react-dev-utils/openBrowser");
|
28 | const paths = require("../config/paths");
|
29 | const config = require("../config/webpack.dev.config");
|
30 | const createDevServerConfig = require("../config/devServer.config");
|
31 |
|
32 | const isInteractive = process.stdout.isTTY;
|
33 |
|
34 |
|
35 | if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
|
36 | process.exit(1);
|
37 | }
|
38 |
|
39 | const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
|
40 | const HOST = process.env.HOST || "0.0.0.0";
|
41 |
|
42 | if (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 |
|
59 | choosePort(HOST, DEFAULT_PORT)
|
60 | .then(port => {
|
61 | if (port == null) {
|
62 |
|
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 |
|
69 | const proxySetting = require(paths.appPackageJson).proxy;
|
70 | const proxyConfig = prepareProxy(proxySetting, paths.appPublic);
|
71 |
|
72 |
|
73 | const compiler = createCompiler(webpack, config, appName, urls, true,proxyConfig);
|
74 |
|
75 |
|
76 | const serverConfig = createDevServerConfig(
|
77 | proxyConfig,
|
78 | urls.lanUrlForConfig
|
79 | );
|
80 | const devServer = new WebpackDevServer(compiler, serverConfig);
|
81 |
|
82 |
|
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 | });
|