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