import React from 'react' ; import { importScript } from './importScript' ; export type LazyComponentResult = React . LazyExoticComponent < React . ComponentType < any > > & { WrappedComponent ? : React . ComponentType < any > ; } ; export function lazyComponent ( url , name , success ? , fail ? ) { const ret = React . lazy ( ( ) => importScript ( url , name ) . then ( ( res ) => { if ( ! res . default ) { throw new Error ( `Failed to import ${ name } component: no default export` ) ; } ret . WrappedComponent = res . default || res ; if ( success ) { success ( ) ; } return res ; } ) . catch ( ( err ) => { if ( fail ) { fail ( err ) ; } return { default : ( ) => < > < /> }; } ) ) ; return ret ; }