UNPKG

5.49 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 paths = require('../config/paths');
48const configFactory = require('../config/webpack.config');
49const createDevServerConfig = require('../config/webpackDevServer.config');
50
51const useYarn = fs.existsSync(paths.yarnLockFile);
52const isInteractive = process.stdout.isTTY;
53
54// Warn and crash if required files are missing
55if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
56 process.exit(1);
57}
58
59// Tools like Cloud9 rely on this.
60const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
61const HOST = process.env.HOST || '0.0.0.0';
62
63if (process.env.HOST) {
64 console.log(
65 chalk.cyan(
66 `Attempting to bind to HOST environment variable: ${chalk.yellow(
67 chalk.bold(process.env.HOST)
68 )}`
69 )
70 );
71 console.log(
72 `If this was unintentional, check that you haven't mistakenly set it in your shell.`
73 );
74 console.log(
75 `Learn more here: ${chalk.yellow('https://bit.ly/CRA-advanced-config')}`
76 );
77 console.log();
78}
79
80// We require that you explicitly set browsers and do not fall back to
81// browserslist defaults.
82const { checkBrowsers } = require('react-dev-utils/browsersHelper');
83checkBrowsers(paths.appPath, isInteractive)
84 .then(() => {
85 // We attempt to use the default port but if it is busy, we offer the user to
86 // run on a different port. `choosePort()` Promise resolves to the next free port.
87 return choosePort(HOST, DEFAULT_PORT);
88 })
89 .then(port => {
90 if (port == null) {
91 // We have not found a port.
92 return;
93 }
94 const config = configFactory('development');
95 const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
96 const appName = require(paths.appPackageJson).name;
97 const useTypeScript = fs.existsSync(paths.appTsConfig);
98 const tscCompileOnError = process.env.TSC_COMPILE_ON_ERROR === 'true';
99 const urls = prepareUrls(protocol, HOST, port);
100 const devSocket = {
101 warnings: warnings =>
102 devServer.sockWrite(devServer.sockets, 'warnings', warnings),
103 errors: errors =>
104 devServer.sockWrite(devServer.sockets, 'errors', errors),
105 };
106 // Create a webpack compiler that is configured with custom messages.
107 const compiler = createCompiler({
108 appName,
109 config,
110 devSocket,
111 urls,
112 useYarn,
113 useTypeScript,
114 tscCompileOnError,
115 webpack,
116 });
117 // Load proxy config
118 const proxySetting = require(paths.appPackageJson).proxy;
119 const proxyConfig = prepareProxy(proxySetting, paths.appPublic);
120 // Serve webpack assets generated by the compiler over a web server.
121 const serverConfig = createDevServerConfig(
122 proxyConfig,
123 urls.lanUrlForConfig
124 );
125 const devServer = new WebpackDevServer(compiler, serverConfig);
126 // Launch WebpackDevServer.
127 devServer.listen(port, HOST, err => {
128 if (err) {
129 return console.log(err);
130 }
131 if (isInteractive) {
132 clearConsole();
133 }
134
135 // We used to support resolving modules according to `NODE_PATH`.
136 // This now has been deprecated in favor of jsconfig/tsconfig.json
137 // This lets you use absolute paths in imports inside large monorepos:
138 if (process.env.NODE_PATH) {
139 console.log(
140 chalk.yellow(
141 'Setting NODE_PATH to resolve modules absolutely has been deprecated in favor of setting baseUrl in jsconfig.json (or tsconfig.json if you are using TypeScript) and will be removed in a future major release of create-react-app.'
142 )
143 );
144 console.log();
145 }
146
147 console.log(chalk.cyan('Starting the development server...\n'));
148 openBrowser(urls.localUrlForBrowser);
149 });
150
151 ['SIGINT', 'SIGTERM'].forEach(function(sig) {
152 process.on(sig, function() {
153 devServer.close();
154 process.exit();
155 });
156 });
157 })
158 .catch(err => {
159 if (err && err.message) {
160 console.log(err.message);
161 }
162 process.exit(1);
163 });