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 |
|
26 | const verifyPackageTree = require('./utils/verifyPackageTree');
|
27 | if (process.env.SKIP_PREFLIGHT_CHECK !== 'true') {
|
28 | verifyPackageTree();
|
29 | }
|
30 | const verifyTypeScriptSetup = require('./utils/verifyTypeScriptSetup');
|
31 | verifyTypeScriptSetup();
|
32 |
|
33 |
|
34 | const fs = require('fs');
|
35 | const chalk = require('react-dev-utils/chalk');
|
36 | const webpack = require('webpack');
|
37 | const WebpackDevServer = require('webpack-dev-server');
|
38 | const clearConsole = require('react-dev-utils/clearConsole');
|
39 | const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
|
40 | const {
|
41 | choosePort,
|
42 | createCompiler,
|
43 | prepareProxy,
|
44 | prepareUrls,
|
45 | } = require('react-dev-utils/WebpackDevServerUtils');
|
46 | const openBrowser = require('react-dev-utils/openBrowser');
|
47 | const semver = require('semver');
|
48 | const paths = require('../config/paths');
|
49 | const configFactory = require('../config/webpack.config');
|
50 | const createDevServerConfig = require('../config/webpackDevServer.config');
|
51 | const getClientEnvironment = require('../config/env');
|
52 | const react = require(require.resolve('react', { paths: [paths.appPath] }));
|
53 |
|
54 | const env = getClientEnvironment(paths.publicUrlOrPath.slice(0, -1));
|
55 | const useYarn = fs.existsSync(paths.yarnLockFile);
|
56 | const isInteractive = process.stdout.isTTY;
|
57 |
|
58 |
|
59 | if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
|
60 | process.exit(1);
|
61 | }
|
62 |
|
63 |
|
64 | const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
|
65 | const HOST = process.env.HOST || '0.0.0.0';
|
66 |
|
67 | if (process.env.HOST) {
|
68 | console.log(
|
69 | chalk.cyan(
|
70 | `Attempting to bind to HOST environment variable: ${chalk.yellow(
|
71 | chalk.bold(process.env.HOST)
|
72 | )}`
|
73 | )
|
74 | );
|
75 | console.log(
|
76 | `If this was unintentional, check that you haven't mistakenly set it in your shell.`
|
77 | );
|
78 | console.log(
|
79 | `Learn more here: ${chalk.yellow('https://cra.link/advanced-config')}`
|
80 | );
|
81 | console.log();
|
82 | }
|
83 |
|
84 |
|
85 |
|
86 | const { checkBrowsers } = require('react-dev-utils/browsersHelper');
|
87 | checkBrowsers(paths.appPath, isInteractive)
|
88 | .then(() => {
|
89 |
|
90 |
|
91 | return choosePort(HOST, DEFAULT_PORT);
|
92 | })
|
93 | .then(port => {
|
94 | if (port == null) {
|
95 |
|
96 | return;
|
97 | }
|
98 |
|
99 | const config = configFactory('development');
|
100 | const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
|
101 | const appName = require(paths.appPackageJson).name;
|
102 |
|
103 | const useTypeScript = fs.existsSync(paths.appTsConfig);
|
104 | const tscCompileOnError = process.env.TSC_COMPILE_ON_ERROR === 'true';
|
105 | const urls = prepareUrls(
|
106 | protocol,
|
107 | HOST,
|
108 | port,
|
109 | paths.publicUrlOrPath.slice(0, -1)
|
110 | );
|
111 | const devSocket = {
|
112 | warnings: warnings =>
|
113 | devServer.sockWrite(devServer.sockets, 'warnings', warnings),
|
114 | errors: errors =>
|
115 | devServer.sockWrite(devServer.sockets, 'errors', errors),
|
116 | };
|
117 |
|
118 | const compiler = createCompiler({
|
119 | appName,
|
120 | config,
|
121 | devSocket,
|
122 | urls,
|
123 | useYarn,
|
124 | useTypeScript,
|
125 | tscCompileOnError,
|
126 | webpack,
|
127 | });
|
128 |
|
129 | const proxySetting = require(paths.appPackageJson).proxy;
|
130 | const proxyConfig = prepareProxy(
|
131 | proxySetting,
|
132 | paths.appPublic,
|
133 | paths.publicUrlOrPath
|
134 | );
|
135 |
|
136 | const serverConfig = createDevServerConfig(
|
137 | proxyConfig,
|
138 | urls.lanUrlForConfig
|
139 | );
|
140 | const devServer = new WebpackDevServer(compiler, serverConfig);
|
141 |
|
142 | devServer.listen(port, HOST, err => {
|
143 | if (err) {
|
144 | return console.log(err);
|
145 | }
|
146 | if (isInteractive) {
|
147 | clearConsole();
|
148 | }
|
149 |
|
150 | if (env.raw.FAST_REFRESH && semver.lt(react.version, '16.10.0')) {
|
151 | console.log(
|
152 | chalk.yellow(
|
153 | `Fast Refresh requires React 16.10 or higher. You are using React ${react.version}.`
|
154 | )
|
155 | );
|
156 | }
|
157 |
|
158 | console.log(chalk.cyan('Starting the development server...\n'));
|
159 | openBrowser(urls.localUrlForBrowser);
|
160 | });
|
161 |
|
162 | ['SIGINT', 'SIGTERM'].forEach(function (sig) {
|
163 | process.on(sig, function () {
|
164 | devServer.close();
|
165 | process.exit();
|
166 | });
|
167 | });
|
168 |
|
169 | if (process.env.CI !== 'true') {
|
170 |
|
171 | process.stdin.on('end', function () {
|
172 | devServer.close();
|
173 | process.exit();
|
174 | });
|
175 | }
|
176 | })
|
177 | .catch(err => {
|
178 | if (err && err.message) {
|
179 | console.log(err.message);
|
180 | }
|
181 | process.exit(1);
|
182 | });
|