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 spawn = require('cross-spawn');
|
20 | const webpack = require('webpack');
|
21 | const exec = require('child_process').exec;
|
22 | const WebpackDevServer = require('webpack-dev-server');
|
23 | const clearConsole = require('react-dev-utils/clearConsole');
|
24 | const {
|
25 | choosePort,
|
26 | createCompiler,
|
27 | prepareProxy,
|
28 | prepareUrls,
|
29 | } = require('react-dev-utils/WebpackDevServerUtils');
|
30 | const openBrowser = require('react-dev-utils/openBrowser');
|
31 | const paths = require('../config/paths');
|
32 | const config = require('../config/webpack.config.dev');
|
33 | const createDevServerConfig = require('../config/webpackDevServer.config');
|
34 | const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
|
35 | const _ = require('lodash');
|
36 |
|
37 | const checkPagesRequired = require('../tools').checkPagesRequired;
|
38 | const resolveApp = require('../tools').resolveApp;
|
39 | const localMockPort = require('../tools').getLocalMockPort(require(paths.appPackageJson).proxy);
|
40 |
|
41 | const useYarn = fs.existsSync(paths.yarnLockFile);
|
42 | const isInteractive = process.stdout.isTTY;
|
43 |
|
44 |
|
45 | if (!checkPagesRequired(paths.allPages)) {
|
46 | process.exit(1);
|
47 | }
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
|
53 | const HOST = process.env.HOST || '0.0.0.0';
|
54 |
|
55 |
|
56 | const isStopmock = process.argv.slice(2).find((v) => v === 'stopmock' );
|
57 | if (!isStopmock) {
|
58 | const customerMock = require(paths.appPackageJson).mockRoot;
|
59 | if (customerMock) {
|
60 | const customerMockPath = resolveApp(`mock/${customerMock}`);
|
61 | if (checkRequiredFiles([customerMockPath])) {
|
62 | console.log(chalk.green('\n custom mock is running! \n'));
|
63 | require('./mock').start(customerMockPath);
|
64 | } else {
|
65 | console.log(chalk.yellow(`\n mock warning: \n missing mock/${customerMock}, start default mockServer\n\n`));
|
66 | require('./mock').start();
|
67 | }
|
68 | } else {
|
69 | if (localMockPort) {
|
70 | console.log(chalk.green('\n default mock is running! \n'));
|
71 | require('./mock').start();
|
72 | }
|
73 | }
|
74 | }
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | choosePort(HOST, DEFAULT_PORT)
|
90 | .then(port => {
|
91 | if (port == null) {
|
92 |
|
93 | return;
|
94 | }
|
95 | const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
|
96 | const appName = require(paths.appPackageJson).name;
|
97 | const urls = prepareUrls(protocol, HOST, port);
|
98 |
|
99 | const compiler = createCompiler(webpack, config, appName, urls, useYarn);
|
100 |
|
101 | const proxySetting = require(paths.appPackageJson).proxy;
|
102 | const proxyConfig = prepareProxy(proxySetting, paths.appPublic);
|
103 |
|
104 | const serverConfig = createDevServerConfig(
|
105 | proxyConfig,
|
106 | urls.lanUrlForConfig
|
107 | );
|
108 | const devServer = new WebpackDevServer(compiler, serverConfig);
|
109 | const publicPath = _.get(config, ['output', 'publicPath']);
|
110 |
|
111 | devServer.listen(port, HOST, err => {
|
112 | if (err) {
|
113 | return console.log(err);
|
114 | }
|
115 | if (isInteractive) {
|
116 |
|
117 | }
|
118 | console.log(chalk.cyan('Starting the development server...\n'));
|
119 |
|
120 | openBrowser(
|
121 | urls.localUrlForBrowser +
|
122 | publicPath.substring(1) +
|
123 | `${paths.allPages[0]}.html`
|
124 | )
|
125 | });
|
126 |
|
127 | ['SIGINT', 'SIGTERM'].forEach(function(sig) {
|
128 | process.on(sig, function() {
|
129 | devServer.close();
|
130 | process.exit();
|
131 | });
|
132 | });
|
133 | })
|
134 | .catch(err => {
|
135 | if (err && err.message) {
|
136 | console.log(err.message);
|
137 | }
|
138 | process.exit(1);
|
139 | });
|