import { mapLoadable } from './bundler' ; import { RequireLoader , ImportLoader } from './interface' ; const cache = { } as any ; export const isCached = ( componentName ) => ! ! cache [ componentName ] ; const DEPRECATED_API_MESSAGE = "You are using a deprecated API that will be removed in a future releases. Please consider using `loader` instead of `require`" ; const ERROR_WHILE_LOADING = "An error occurred while lazy loading a component. Perhaps the path where you are trying to load the component does not exist? Stacktrace: " ; const nonBlockingLoader = ( loader ) => new Promise ( ( resolve ) => { setTimeout ( async ( ) => { try { const file = await loader ( ) ; resolve ( file ) ; } catch ( e ) { console . error ( ERROR_WHILE_LOADING + e ) ; resolve ( null ) ; } } , 0 ) ; } ) ; export const getComponent = async ( name ) => { if ( ! isCached ( name ) ) { const { require : load , loader , ... rest } = mapLoadable [ name ] ; let component = null ; if ( loader ) { component = await nonBlockingLoader ( loader ) ; } else if ( load ) { console . warn ( DEPRECATED_API_MESSAGE ) ; component = await nonBlockingLoader ( load ) ; } cache [ name ] = { ... rest , component : component [ rest . extract ] , } ; } return cache [ name ] ; } ; export const getComponentFromCache = ( name ) => cache [ name ] ;