UNPKG

5.68 kBJavaScriptView Raw
1// @remove-on-eject-begin
2/**
3 * Copyright (c) 2015-present, Facebook, Inc.
4 *
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8// @remove-on-eject-end
9'use strict';
10
11// Do this as the first thing so that any code reading it knows the right env.
12process.env.BABEL_ENV = 'development';
13process.env.NODE_ENV = 'development';
14
15// Makes the script crash on unhandled rejections instead of silently
16// ignoring them. In the future, promise rejections that are not handled will
17// terminate the Node.js process with a non-zero exit code.
18process.on('unhandledRejection', err => {
19 throw err;
20});
21
22// Ensure environment variables are read.
23require('../config/env');
24// @remove-on-eject-begin
25// Do the preflight check (only happens before eject).
26const verifyPackageTree = require('./utils/verifyPackageTree');
27if (process.env.SKIP_PREFLIGHT_CHECK !== 'true') {
28 verifyPackageTree();
29}
30const verifyTypeScriptSetup = require('./utils/verifyTypeScriptSetup');
31verifyTypeScriptSetup();
32// @remove-on-eject-end
33
34const fs = require('fs');
35const chalk = require('react-dev-utils/chalk');
36const webpack = require('webpack');
37const WebpackDevServer = require('webpack-dev-server');
38const clearConsole = require('react-dev-utils/clearConsole');
39const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
40const {
41 choosePort,
42 createCompiler,
43 prepareProxy,
44 prepareUrls,
45} = require('react-dev-utils/WebpackDevServerUtils');
46const openBrowser = require('react-dev-utils/openBrowser');
47const semver = require('semver');
48const paths = require('../config/paths');
49const configFactory = require('../config/webpack.config');
50const createDevServerConfig = require('../config/webpackDevServer.config');
51const getClientEnvironment = require('../config/env');
52const react = require(require.resolve('react', { paths: [paths.appPath] }));
53
54const env = getClientEnvironment(paths.publicUrlOrPath.slice(0, -1));
55const useYarn = fs.existsSync(paths.yarnLockFile);
56const isInteractive = process.stdout.isTTY;
57
58// Warn and crash if required files are missing
59if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
60 process.exit(1);
61}
62
63// Tools like Cloud9 rely on this.
64const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
65const HOST = process.env.HOST || '0.0.0.0';
66
67if (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// We require that you explicitly set browsers and do not fall back to
85// browserslist defaults.
86const { checkBrowsers } = require('react-dev-utils/browsersHelper');
87checkBrowsers(paths.appPath, isInteractive)
88 .then(() => {
89 // We attempt to use the default port but if it is busy, we offer the user to
90 // run on a different port. `choosePort()` Promise resolves to the next free port.
91 return choosePort(HOST, DEFAULT_PORT);
92 })
93 .then(port => {
94 if (port == null) {
95 // We have not found a port.
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 // Create a webpack compiler that is configured with custom messages.
118 const compiler = createCompiler({
119 appName,
120 config,
121 devSocket,
122 urls,
123 useYarn,
124 useTypeScript,
125 tscCompileOnError,
126 webpack,
127 });
128 // Load proxy config
129 const proxySetting = require(paths.appPackageJson).proxy;
130 const proxyConfig = prepareProxy(
131 proxySetting,
132 paths.appPublic,
133 paths.publicUrlOrPath
134 );
135 // Serve webpack assets generated by the compiler over a web server.
136 const serverConfig = createDevServerConfig(
137 proxyConfig,
138 urls.lanUrlForConfig
139 );
140 const devServer = new WebpackDevServer(compiler, serverConfig);
141 // Launch WebpackDevServer.
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 // Gracefully exit when stdin ends
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 });