import ora from 'ora' ; import merge from 'deepmerge' ; import { Compiler , Stats , MultiCompiler } from 'webpack' ; import chalk from 'chalk' ; import clearConsole from 'react-dev-utils/clearConsole' ; import openBrowser from 'react-dev-utils/openBrowser' ; import formatWebpackMessages from 'react-dev-utils/formatWebpackMessages' ; import { run } from '../compiler' ; import { Args } from '../options' ; import Server from '../server' ; import { exitProcess , getServerOptions } from './utilities' ; const isInteractive = process . stdout . isTTY ; export default async ( args ) => { const serverOptions = await getServerOptions ( args ) ; const protocol = serverOptions . https ? 'https' : 'http' ; const url = `${ protocol }://${ serverOptions . host }:${ serverOptions . port }` ; args = merge < Args > ( { options : { env : { NODE_ENV : process . env . NODE_ENV || 'development' , } , } , middleware : [ ] , } , args ) ; if ( isInteractive ) { clearConsole ( ) ; } const spinner = ora ( 'Compiling project' ) . start ( ) ; let multiCompiler ; try { multiCompiler = await run ( 'start' , args ) ; } catch ( err ) { spinner . fail ( 'Failed to compile' ) ; console . log ( ) ; console . log ( err . stackTrace || err ) ; console . log ( ) ; throw exitProcess ( 1 ) ; } const server = new Server ( { multiCompiler , ... serverOptions , } ) ; await server . listen ( ) ; spinner . succeed ( `Development server running on: ${ url }` ) ; openBrowser ( url ) ; const building = ora ( 'Waiting for initial compilation to finish' ) . start ( ) ; multiCompiler . plugin ( 'done' , ( stats ) => { if ( isInteractive ) { clearConsole ( ) ; } const messages = formatWebpackMessages ( stats . toJson ( ) ) ; const isSuccessful = ! messages . errors . length && ! messages . warnings . length ; if ( isSuccessful ) { building . succeed ( `Compiled successfully!` ) ; } if ( messages . errors . length ) { if ( messages . errors . length > 1 ) { messages . errors . length = 1 ; } building . fail ( `Failed to compile` ) ; console . log ( messages . errors . join ( '\n\n' ) ) ; return ; } if ( messages . warnings . length ) { building . warn ( 'Compiled with warnings' ) ; console . log ( messages . warnings . join ( '\n\n' ) ) ; console . log ( '\nSearch for the ' + chalk . underline ( chalk . yellow ( 'keywords' ) ) + ' to learn more about each warning.' ) ; console . log ( 'To ignore, add ' + chalk . cyan ( '// eslint-disable-next-line' ) + ' to the line before.\n' ) ; } } ) ; multiCompiler . plugin ( 'invalid' , ( ) => { if ( isInteractive ) { clearConsole ( ) ; } building . text = 'Compiling...' ; building . start ( ) ; } ) ; } ;