on bd2a4fd help sign in
bridge/scripts/start.js
145 lines 4.8 kB history raw
1'use strict';
3// Do this as the first thing so that any code reading it knows the right env.
4process.env.BABEL_ENV = 'development';
5process.env.NODE_ENV = 'development';
7// Makes the script crash on unhandled rejections instead of silently
8// ignoring them. In the future, promise rejections that are not handled will
9// terminate the Node.js process with a non-zero exit code.
10process.on('unhandledRejection', err => {
11 throw err;
12});
14// Ensure environment variables are read.
15require('../config/env');
18const fs = require('fs');
19const chalk = require('react-dev-utils/chalk');
20const webpack = require('webpack');
21const WebpackDevServer = require('webpack-dev-server');
22const clearConsole = require('react-dev-utils/clearConsole');
23const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
24const {
25 choosePort,
26 createCompiler,
27 prepareProxy,
28 prepareUrls,
29} = require('react-dev-utils/WebpackDevServerUtils');
30const openBrowser = require('react-dev-utils/openBrowser');
31const paths = require('../config/paths');
32const configFactory = require('../config/webpack.config');
33const createDevServerConfig = require('../config/webpackDevServer.config');
35const useYarn = fs.existsSync(paths.yarnLockFile);
36const isInteractive = process.stdout.isTTY;
38// Warn and crash if required files are missing
39if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
40 process.exit(1);
43// Tools like Cloud9 rely on this.
44const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
45const HOST = process.env.HOST || '0.0.0.0';
47if (process.env.HOST) {
48 console.log(
49 chalk.cyan(
50 `Attempting to bind to HOST environment variable: ${chalk.yellow(
51 chalk.bold(process.env.HOST)
52 )}`
53 )
54 );
55 console.log(
56 `If this was unintentional, check that you haven't mistakenly set it in your shell.`
57 );
58 console.log(
59 `Learn more here: ${chalk.yellow('https://bit.ly/CRA-advanced-config')}`
60 );
61 console.log();
64// We require that you explicitly set browsers and do not fall back to
65// browserslist defaults.
66const { checkBrowsers } = require('react-dev-utils/browsersHelper');
67checkBrowsers(paths.appPath, isInteractive)
68 .then(() => {
69 // We attempt to use the default port but if it is busy, we offer the user to
70 // run on a different port. `choosePort()` Promise resolves to the next free port.
71 return choosePort(HOST, DEFAULT_PORT);
72 })
73 .then(port => {
74 if (port == null) {
75 // We have not found a port.
76 return;
77 }
78 const config = configFactory('development');
79 const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
80 const appName = require(paths.appPackageJson).name;
81 const useTypeScript = fs.existsSync(paths.appTsConfig);
82 const urls = prepareUrls(protocol, HOST, port);
83 const devSocket = {
84 warnings: warnings =>
85 devServer.sockWrite(devServer.sockets, 'warnings', warnings),
86 errors: errors =>
87 devServer.sockWrite(devServer.sockets, 'errors', errors),
88 };
89 // Create a webpack compiler that is configured with custom messages.
90 const compiler = createCompiler({
91 appName,
92 config,
93 devSocket,
94 urls,
95 useYarn,
96 useTypeScript,
97 webpack,
98 });
99 // Load proxy config
100 const proxySetting = require(paths.appPackageJson).proxy;
101 const proxyConfig = prepareProxy(proxySetting, paths.appPublic);
102 // Serve webpack assets generated by the compiler over a web server.
103 const serverConfig = createDevServerConfig(
104 proxyConfig,
105 urls.lanUrlForConfig
106 );
107 const devServer = new WebpackDevServer(compiler, serverConfig);
108 // Launch WebpackDevServer.
109 devServer.listen(port, HOST, err => {
110 if (err) {
111 return console.log(err);
112 }
113 if (isInteractive) {
114 clearConsole();
115 }
117 // We used to support resolving modules according to `NODE_PATH`.
118 // This now has been deprecated in favor of jsconfig/tsconfig.json
119 // This lets you use absolute paths in imports inside large monorepos:
120 if (process.env.NODE_PATH) {
121 console.log(
122 chalk.yellow(
123 '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.'
124 )
125 );
126 console.log();
127 }
129 console.log(chalk.cyan('Starting the development server...\n'));
130 openBrowser(urls.localUrlForBrowser);
131 });
133 ['SIGINT', 'SIGTERM'].forEach(function(sig) {
134 process.on(sig, function() {
135 devServer.close();
136 process.exit();
137 });
138 });
139 })
140 .catch(err => {
141 if (err && err.message) {
142 console.log(err.message);
143 }
144 process.exit(1);
145 });