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 |
|
47 |
|
48 | choosePort(HOST, DEFAULT_PORT)
|
49 | .then(port => {
|
50 | if (port == null) {
|
51 |
|
52 | return;
|
53 | }
|
54 | const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
|
55 | const appName = require(paths.appPackageJson).name;
|
56 | const urls = prepareUrls(protocol, HOST, port);
|
57 |
|
58 | const compiler = createCompiler(webpack, config, appName, urls, useYarn);
|
59 |
|
60 | const proxySetting = require(paths.appPackageJson).proxy;
|
61 | const proxyConfig = prepareProxy(proxySetting, paths.appPublic);
|
62 |
|
63 | const serverConfig = createDevServerConfig(
|
64 | proxyConfig,
|
65 | urls.lanUrlForConfig
|
66 | );
|
67 | const devServer = new WebpackDevServer(compiler, serverConfig);
|
68 |
|
69 | devServer.listen(port, HOST, err => {
|
70 | if (err) {
|
71 | return console.log(err);
|
72 | }
|
73 | if (isInteractive) {
|
74 | clearConsole();
|
75 | }
|
76 | console.log(chalk.cyan('Starting the development server...\n'));
|
77 | openBrowser(urls.localUrlForBrowser);
|
78 | });
|
79 |
|
80 | ['SIGINT', 'SIGTERM'].forEach(function(sig) {
|
81 | process.on(sig, function() {
|
82 | devServer.close();
|
83 | process.exit();
|
84 | });
|
85 | });
|
86 | })
|
87 | .catch(err => {
|
88 | if (err && err.message) {
|
89 | console.log(err.message);
|
90 | }
|
91 | process.exit(1);
|
92 | });
|