UNPKG

accelerator-core

Version:

[![Build Status](https://travis-ci.org/furkleindustries/accelerator-core.svg?branch=master)](https://travis-ci.org/furkleindustries/accelerator-core)

126 lines (109 loc) 3.2 kB
import './functions/setUnhandledRejectionEvent'; import '../config/setDevelopmentEnv'; import { checkBrowsers, } from 'react-dev-utils/browsersHelper'; import chalk from 'chalk'; import checkRequiredFiles from 'react-dev-utils/checkRequiredFiles'; import { error, log, } from 'colorful-logging'; import config from '../config/webpack/webpack.config'; import createDevServerConfig from '../config/webpack/webpackDevServer.config'; import { choosePort, createCompiler, prepareProxy, prepareUrls, } from '../lib/react-dev-utils/FixedWebpackDevServerUtils'; import openBrowser from 'react-dev-utils/openBrowser'; import { paths, } from '../config/paths'; import webpack from 'webpack'; import WebpackDevServer from 'webpack-dev-server'; const packageJson = require(paths.appPackageJson); const { env: { HOST, HTTPS, PORT, }, exit, stdout: { isTty: isInteractive }, } = process; const filesMissing = !checkRequiredFiles([ paths.htmlTemplate, paths.appIndex, paths.appPackageJson, ]); /* Warn and crash if required files are missing. */ if (filesMissing) { exit(1); } // Tools like Cloud9 rely on this. const defaultPort = parseInt(PORT, 10) || 3000; const host = HOST || '0.0.0.0'; if (HOST) { log( `Attempting to bind to HOST environment variable: ` + `${chalk.yellow(chalk.bold(host))}\nIf this was unintentional, check ` + `that you haven't mistakenly set it in your shell.\nLearn more here: ` + `${chalk.bold('http://bit.ly/CRA-advanced-config')}.\n` ); } // We require that you explictly set browsers and do not fall back to // browserslist defaults. checkBrowsers(paths.appPath, isInteractive) // We attempt to use the default port but if it is busy, we offer the user to // run on a different port. `choosePort()` Promise resolves to the next free port. .then(() => choosePort(host, defaultPort)) .then((port) => { if (port === null) { // We have not found a port. return; } const protocol = HTTPS === 'true' ? 'https' : 'http'; const appName = packageJson.name; const urls = prepareUrls(protocol, host, port); const { lanUrlForConfig, localUrlForBrowser, } = urls; // Create a webpack compiler that is configured with custom messages. const compiler = createCompiler({ appName, config, urls, webpack, }); // Load proxy config const proxySetting = packageJson.proxy; const proxyConfig = prepareProxy(proxySetting, paths.appPublic); // Serve webpack assets generated by the compiler over a web server. const serverConfig = createDevServerConfig( proxyConfig, lanUrlForConfig, ); const devServer = new WebpackDevServer(compiler, serverConfig); /* Launch WebpackDevServer. */ devServer.listen(port, host, (err) => { if (err) { error(err); return; } log(chalk.cyan('Starting the development server...\n')); openBrowser(localUrlForBrowser); }); [ 'SIGINT', 'SIGTERM', ].forEach((sig) => process.on(sig, () => { devServer.close(); exit(0); })); }).catch((err) => { error(err); exit(1); });