1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 |
|
12 | process.env.BABEL_ENV = 'development';
|
13 | process.env.NODE_ENV = 'development';
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | process.on('unhandledRejection', err => {
|
19 | throw err;
|
20 | });
|
21 |
|
22 |
|
23 | require('../config/env');
|
24 |
|
25 | const fs = require('fs');
|
26 | const chalk = require('chalk');
|
27 | const webpack = require('webpack');
|
28 | const WebpackDevServer = require('webpack-dev-server');
|
29 | const clearConsole = require('react-dev-utils/clearConsole');
|
30 | const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
|
31 | const {
|
32 | choosePort,
|
33 | createCompiler,
|
34 | prepareProxy,
|
35 | prepareUrls,
|
36 | } = require('react-dev-utils/WebpackDevServerUtils');
|
37 | const openBrowser = require('react-dev-utils/openBrowser');
|
38 | const paths = require('../config/paths');
|
39 | const config = require('../config/webpack.config.dev');
|
40 | const createDevServerConfig = require('../config/webpackDevServer.config');
|
41 |
|
42 | const useYarn = fs.existsSync(paths.yarnLockFile);
|
43 | const isInteractive = process.stdout.isTTY;
|
44 |
|
45 |
|
46 | if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
|
47 | process.exit(1);
|
48 | }
|
49 |
|
50 |
|
51 | const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
|
52 | const HOST = process.env.HOST || '0.0.0.0';
|
53 |
|
54 | if (process.env.HOST) {
|
55 | console.log(
|
56 | chalk.cyan(
|
57 | `Attempting to bind to HOST environment variable: ${chalk.yellow(
|
58 | chalk.bold(process.env.HOST)
|
59 | )}`
|
60 | )
|
61 | );
|
62 | console.log(
|
63 | `If this was unintentional, check that you haven't mistakenly set it in your shell.`
|
64 | );
|
65 | console.log(`Learn more here: ${chalk.yellow('http://bit.ly/2mwWSwH')}`);
|
66 | console.log();
|
67 | }
|
68 |
|
69 |
|
70 |
|
71 | choosePort(HOST, DEFAULT_PORT)
|
72 | .then(port => {
|
73 | if (port == null) {
|
74 |
|
75 | return;
|
76 | }
|
77 | const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
|
78 | const appName = require(paths.appPackageJson).name;
|
79 | const urls = prepareUrls(protocol, HOST, port);
|
80 |
|
81 | const compiler = createCompiler(webpack, config, appName, urls, useYarn);
|
82 |
|
83 | const proxySetting = require(paths.appPackageJson).proxy;
|
84 | const proxyConfig = prepareProxy(proxySetting, paths.appPublic);
|
85 |
|
86 | const serverConfig = createDevServerConfig(
|
87 | proxyConfig,
|
88 | urls.lanUrlForConfig
|
89 | );
|
90 | const devServer = new WebpackDevServer(compiler, serverConfig);
|
91 |
|
92 | devServer.listen(port, HOST, err => {
|
93 | if (err) {
|
94 | return console.log(err);
|
95 | }
|
96 | if (isInteractive) {
|
97 | clearConsole();
|
98 | }
|
99 | console.log(chalk.cyan('Starting the development server...\n'));
|
100 | openBrowser(urls.localUrlForBrowser);
|
101 | });
|
102 |
|
103 | ['SIGINT', 'SIGTERM'].forEach(function(sig) {
|
104 | process.on(sig, function() {
|
105 | devServer.close();
|
106 | process.exit();
|
107 | });
|
108 | });
|
109 | })
|
110 | .catch(err => {
|
111 | if (err && err.message) {
|
112 | console.log(err.message);
|
113 | }
|
114 | process.exit(1);
|
115 | });
|