import { createElement , ComponentType , Component , RaxElement } from 'rax' ; interface IRoute { source : string ; component : ( ) => ComponentType & { __pageConfig : IRoute ; } ; pageSource : string ; } function miniappRenderer ( { appConfig = { } as any , buildConfig , staticConfig , ErrorBoundary , appLifecycle : { createBaseApp , emitLifeCycles , initAppLifeCycles } } , { mount , unmount } ) { const { runtime } = createBaseApp ( appConfig , buildConfig , { } , staticConfig ) ; initAppLifeCycles ( ) ; const { app = { } } = appConfig ; const { ErrorBoundaryFallback , onErrorBoundaryHander , onErrorBoundaryHandler , errorBoundary , rootId = 'root' } = app ; ErrorBoundary = errorBoundary ? ErrorBoundary : null ; const onError = onErrorBoundaryHander || onErrorBoundaryHandler ; if ( onErrorBoundaryHander ) { console . error ( 'Please use onErrorBoundaryHandler instead of onErrorBoundaryHander' ) ; } emitLifeCycles ( ) ; const pagesRenderInfo = staticConfig . routes . map ( ( route ) => { const { source , component , pageSource } = route ; return { path : pageSource || source , render ( ) { const Page = component ( ) ; Page . __pageConfig = route ; const rootEl = document . createElement ( 'div' ) ; rootEl . setAttribute ( 'id' , rootId ) ; const appInstance = mount ( getRenderApp ( Page , runtime , { ErrorBoundary , ErrorBoundaryFallback , onError , } ) , rootEl ) ; document . body . appendChild ( rootEl ) ; ( document as any ) . __unmount = unmount ( appInstance , rootEl ) ; } , setDocument ( value ) { document = value ; if ( typeof getApp === 'function' ) { const MiniAppGlobalInstance = getApp ( ) ; const dispatchDocumentModify = MiniAppGlobalInstance . _dispatchDocumentModify ; if ( typeof dispatchDocumentModify === 'function' ) { dispatchDocumentModify . call ( MiniAppGlobalInstance , value ) ; } } } } ; } ) ; ( window as any ) . __pagesRenderInfo = ( ( window as any ) . __pagesRenderInfo || [ ] ) . concat ( pagesRenderInfo ) ; } function wrapperPage ( component , pageWrappers ) { const Wrapper = ( pageWrappers || [ ] ) . reduce ( ( acc , curr ) => { return curr ( acc ) ; } , component ) ; return < Wrapper />; } function getRenderApp ( Page , runtime , { onError , ErrorBoundary , ErrorBoundaryFallback } ) { const wrapperPageRegistration = runtime ?. getWrapperPageRegistration ?. ( ) ; let rootApp = wrapperPage ( Page , wrapperPageRegistration ) ; const AppProvider = runtime ?. composeAppProvider ?. ( ) ; if ( AppProvider ) { rootApp = < AppProvider > { rootApp } < /AppProvider>; } if ( ErrorBoundary ) { rootApp = < ErrorBoundary Fallback = { ErrorBoundaryFallback } onError = { onError } > { rootApp } < /ErrorBoundary>; } class App extends Component { public render ( ) { return rootApp ; } } return < App />; } export default miniappRenderer ;