/**
 * This has been adapted from `create-react-app`, authored by Facebook, Inc.
 * see: https://github.com/facebookincubator/create-react-app/tree/master/packages/react-dev-utils
 */ import { LAUNCH_EDITOR_ENDPOINT } from '@modern-js/utils/constants' ; import { formatWebpackMessages } from '@modern-js/utils/formatWebpackMessages' ; import stripAnsi from 'strip-ansi' ; import webpack from 'webpack' ; import { createSocketUrl } from './createSocketUrl' ; const ErrorOverlay = require ( 'react-error-overlay' ) ; ErrorOverlay . setEditorHandler ( function editorHandler ( errorLocation ) { fetch ( `${ LAUNCH_EDITOR_ENDPOINT }?filename=${ window . encodeURIComponent ( errorLocation . fileName ) }&line=${ window . encodeURIComponent ( errorLocation . lineNumber || 1 ) }&column=${ window . encodeURIComponent ( errorLocation . colNumber || 1 ) }` ) ; } ) ; let hadRuntimeError = false ; ErrorOverlay . startReportingRuntimeErrors ( { onError ( ) { hadRuntimeError = true ; } , filename : '/static/js/bundle.js' , } ) ; if ( module . hot && typeof module . hot . dispose === 'function' ) { module . hot . dispose ( ( ) => { ErrorOverlay . stopReportingRuntimeErrors ( ) ; } ) ; } const socketUrl = createSocketUrl ( __resourceQuery ) ; const connection = new WebSocket ( socketUrl ) ; connection . onclose = function ( ) { if ( typeof console !== 'undefined' && typeof console . info === 'function' ) { console . info ( 'The development server has disconnected.\nRefresh the page if necessary.' ) ; } } ; let isFirstCompilation = true ; let mostRecentCompilationHash = null ; let hasCompileErrors = false ; function clearOutdatedErrors ( ) { if ( typeof console !== 'undefined' && typeof console . clear === 'function' ) { if ( hasCompileErrors ) { console . clear ( ) ; } } } function handleSuccess ( ) { clearOutdatedErrors ( ) ; const isHotUpdate = ! isFirstCompilation ; isFirstCompilation = false ; hasCompileErrors = false ; if ( isHotUpdate ) { tryApplyUpdates ( function onHotUpdateSuccess ( ) { tryDismissErrorOverlay ( ) ; } ) ; } } function handleWarnings ( warnings ) { clearOutdatedErrors ( ) ; const isHotUpdate = ! isFirstCompilation ; isFirstCompilation = false ; hasCompileErrors = false ; function printWarnings ( ) { const formatted = formatWebpackMessages ( { warnings , errors : [ ] , } ) ; if ( typeof console !== 'undefined' && typeof console . warn === 'function' ) { for ( let i = 0 ; i < formatted . warnings . length ; i ++ ) { if ( i === 5 ) { console . warn ( 'There were more warnings in other files.\n' + 'You can find a complete log in the terminal.' ) ; break ; } console . warn ( stripAnsi ( formatted . warnings [ i ] ) ) ; } } } printWarnings ( ) ; if ( isHotUpdate ) { tryApplyUpdates ( function onSuccessfulHotUpdate ( ) { tryDismissErrorOverlay ( ) ; } ) ; } } function handleErrors ( errors ) { clearOutdatedErrors ( ) ; isFirstCompilation = false ; hasCompileErrors = true ; const formatted = formatWebpackMessages ( { errors , warnings : [ ] , } ) ; ErrorOverlay . reportBuildError ( formatted . errors [ 0 ] ) ; if ( typeof console !== 'undefined' && typeof console . error === 'function' ) { for ( const error of formatted . errors ) { console . error ( stripAnsi ( error ) ) ; } } } function tryDismissErrorOverlay ( ) { if ( ! hasCompileErrors ) { ErrorOverlay . dismissBuildError ( ) ; } } function handleAvailableHash ( hash ) { mostRecentCompilationHash = hash ; } connection . onmessage = function ( e ) { const message = JSON . parse ( e . data ) ; switch ( message . type ) { case 'hash' : handleAvailableHash ( message . data ) ; break ; case 'still-ok' : case 'ok' : handleSuccess ( ) ; break ; case 'content-changed' : window . location . reload ( ) ; break ; case 'warnings' : handleWarnings ( message . data ) ; break ; case 'errors' : handleErrors ( message . data ) ; break ; default : } } ; function isUpdateAvailable ( ) { return mostRecentCompilationHash !== __webpack_hash__ ; } function canApplyUpdates ( ) { return module . hot . status ( ) === 'idle' ; } function tryApplyUpdates ( onHotUpdateSuccess ? ) { if ( ! module . hot ) { window . location . reload ( ) ; return ; } if ( ! isUpdateAvailable ( ) || ! canApplyUpdates ( ) ) { return ; } function handleApplyUpdates ( err , updatedModules ) { const hasReactRefresh = process . env . FAST_REFRESH ; const wantsForcedReload = err || ! updatedModules || hadRuntimeError ; if ( ! hasReactRefresh && wantsForcedReload ) { window . location . reload ( ) ; return ; } if ( typeof onHotUpdateSuccess === 'function' ) { onHotUpdateSuccess ( ) ; } if ( isUpdateAvailable ( ) ) { tryApplyUpdates ( ) ; } } const result = module . hot . check ( true , handleApplyUpdates ) ; if ( result ?. then ) { result . then ( ( updatedModules ) => { handleApplyUpdates ( null , updatedModules ) ; } , ( err ) => { handleApplyUpdates ( err , null ) ; } ) ; } }