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