import { render , createElement , Fragment , FunctionComponent } from 'rax' ; import { isWeb , isWeex , isKraken } from 'universal-env' ; import UniversalDriver from 'driver-universal' ; import { IContext , RenderOptions } from './types' ; import { setInitialData } from './initialData' ; import parseSearch from './parseSearch' ; import type { RuntimeModule } from 'create-app-shared' ; let driver = UniversalDriver ; async function raxAppRenderer ( options , renderOptions ) { if ( ! options . appConfig ) { options . appConfig = { } ; } const { appConfig , buildConfig , appLifecycle , staticConfig } = options ; const { createBaseApp , emitLifeCycles , initAppLifeCycles } = appLifecycle ; const context = { } ; if ( ( window as any ) ?. __INITIAL_DATA__ ) { context . initialData = ( window as any ) . __INITIAL_DATA__ . initialData ; context . pageInitialProps = ( window as any ) . __INITIAL_DATA__ . pageInitialProps ; } else if ( isWeb && appConfig ?. app ?. getInitialData ) { const { pathname , search } = window . location ; const query = parseSearch ( search ) ; const initialContext = { pathname , query , } ; context . initialData = await appConfig . app . getInitialData ( initialContext ) ; } setInitialData ( context . initialData ) ; const { runtime , appConfig : modifiedAppConfig } = createBaseApp ( appConfig , buildConfig , context , staticConfig ) ; initAppLifeCycles ( ) ; setInitialData ( context . initialData ) ; emitLifeCycles ( ) ; return _render ( runtime , context , { ... options , appConfig : modifiedAppConfig , } ) ; } function _render ( runtime , context , options ) { const { appConfig = { } , buildConfig } = options ; const { rootId , mountNode } = appConfig . app ; const webConfig = buildConfig . web || { } ; const App = getRenderApp ( runtime , options ) ; const appMountNode = _getAppMountNode ( mountNode , rootId ) ; if ( runtime ?. modifyDOMRender ) { return runtime ?. modifyDOMRender ?. ( { App , appMountNode } ) ; } render ( < App />, appMountNode, { driver , hydrate : webConfig . hydrate || webConfig . snapshot || webConfig . ssr || webConfig . staticExport , } ) ; } function _getAppMountNode ( mountNode , rootId = 'root' ) { if ( isWeex || isKraken ) return null ; return mountNode || document . getElementById ( rootId ) ; } export function getRenderApp ( runtime , options ) { const { ErrorBoundary , appConfig = { app : { } } , TabBar } = options ; const { ErrorBoundaryFallback , onErrorBoundaryHandler , errorBoundary } = appConfig . app ; const AppProvider = runtime ?. composeAppProvider ?. ( ) ; const AppComponent = runtime ?. getAppComponent ?. ( ) ; function App ( ) { const appComponent = < AppComponent />; let rootApp = AppProvider ? < AppProvider > { appComponent } < /AppProvider> : appComponent; if ( TabBar ) { rootApp = ( < Fragment > { rootApp } < TabBar / > < /Fragment>); } if ( errorBoundary ) { return ( < ErrorBoundary Fallback = { ErrorBoundaryFallback } onError = { onErrorBoundaryHandler } > { rootApp } < /ErrorBoundary>); } return rootApp ; } return App ; } export default raxAppRenderer ;