import type * as d from '../declarations' ; import { appDidLoad } from './update-component' ; import { BUILD } from '@app-data' ; import { CMP_FLAGS } from '@utils' ; import { connectedCallback } from './connected-callback' ; import { convertScopedToShadow , registerStyle } from './styles' ; import { createTime , installDevTools } from './profile' ; import { disconnectedCallback } from './disconnected-callback' ; import { doc , getHostRef , plt , registerHost , win , supportsShadow } from '@platform' ; import { hmrStart } from './hmr-component' ; import { HYDRATED_CSS , HYDRATED_STYLE_ID , PLATFORM_FLAGS , PROXY_FLAGS } from './runtime-constants' ; import { patchCloneNode , patchSlotAppendChild , patchChildSlotNodes , patchTextContent } from './dom-extras' ; import { proxyComponent } from './proxy-component' ; export const bootstrapLazy = ( lazyBundles , options = { } ) => { if ( BUILD . profile && performance . mark ) { performance . mark ( 'st:app:start' ) ; } installDevTools ( ) ; const endBootstrap = createTime ( 'bootstrapLazy' ) ; const cmpTags = [ ] ; const exclude = options . exclude || [ ] ; const customElements = win . customElements ; const head = doc . head ; const metaCharset = head . querySelector ( 'meta[charset]' ) ; const visibilityStyle = doc . createElement ( 'style' ) ; const deferredConnectedCallbacks = [ ] ; const styles = doc . querySelectorAll ( `[${ HYDRATED_STYLE_ID }]` ) ; let appLoadFallback ; let isBootstrapping = true ; let i = 0 ; Object . assign ( plt , options ) ; plt . $resourcesUrl$ = new URL ( options . resourcesUrl || './' , doc . baseURI ) . href ; if ( BUILD . asyncQueue ) { if ( options . syncQueue ) { plt . $flags$ |= PLATFORM_FLAGS . queueSync ; } } if ( BUILD . hydrateClientSide ) { plt . $flags$ |= PLATFORM_FLAGS . appLoaded ; } if ( BUILD . hydrateClientSide && BUILD . shadowDom ) { for ( ; i < styles . length ; i ++ ) { registerStyle ( styles [ i ] . getAttribute ( HYDRATED_STYLE_ID ) , convertScopedToShadow ( styles [ i ] . innerHTML ) , true ) ; } } lazyBundles . map ( ( lazyBundle ) => { lazyBundle [ 1 ] . map ( ( compactMeta ) => { const cmpMeta = { $flags$ : compactMeta [ 0 ] , $tagName$ : compactMeta [ 1 ] , $members$ : compactMeta [ 2 ] , $listeners$ : compactMeta [ 3 ] , } ; if ( BUILD . member ) { cmpMeta . $members$ = compactMeta [ 2 ] ; } if ( BUILD . hostListener ) { cmpMeta . $listeners$ = compactMeta [ 3 ] ; } if ( BUILD . reflect ) { cmpMeta . $attrsToReflect$ = [ ] ; } if ( BUILD . watchCallback ) { cmpMeta . $watchers$ = { } ; } if ( BUILD . shadowDom && ! supportsShadow && cmpMeta . $flags$ & CMP_FLAGS . shadowDomEncapsulation ) { cmpMeta . $flags$ |= CMP_FLAGS . needsShadowDomShim ; } const tagName = BUILD . transformTagName && options . transformTagName ? options . transformTagName ( cmpMeta . $tagName$ ) : cmpMeta . $tagName$ ; const HostElement = class extends HTMLElement { [ 's-p' ] : Promise < void > [ ] ; [ 's-rc' ] : ( ( ) => void ) [ ] ; constructor ( self ) { super ( self ) ; self = this ; registerHost ( self , cmpMeta ) ; if ( BUILD . shadowDom && cmpMeta . $flags$ & CMP_FLAGS . shadowDomEncapsulation ) { if ( supportsShadow ) { if ( BUILD . shadowDelegatesFocus ) { self . attachShadow ( { mode : 'open' , delegatesFocus : ! ! ( cmpMeta . $flags$ & CMP_FLAGS . shadowDelegatesFocus ) , } ) ; } else { self . attachShadow ( { mode : 'open' } ) ; } } else if ( ! BUILD . hydrateServerSide && ! ( 'shadowRoot' in self ) ) { ( self as any ) . shadowRoot = self ; } } if ( BUILD . slotChildNodesFix ) { patchChildSlotNodes ( self , cmpMeta ) ; } } connectedCallback ( ) { if ( appLoadFallback ) { clearTimeout ( appLoadFallback ) ; appLoadFallback = null ; } if ( isBootstrapping ) { deferredConnectedCallbacks . push ( this ) ; } else { plt . jmp ( ( ) => connectedCallback ( this ) ) ; } } disconnectedCallback ( ) { plt . jmp ( ( ) => disconnectedCallback ( this ) ) ; } componentOnReady ( ) { return getHostRef ( this ) . $onReadyPromise$ ; } } ; if ( BUILD . cloneNodeFix ) { patchCloneNode ( HostElement . prototype ) ; } if ( BUILD . appendChildSlotFix ) { patchSlotAppendChild ( HostElement . prototype ) ; } if ( BUILD . hotModuleReplacement ) { ( HostElement as any ) . prototype [ 's-hmr' ] = function ( hmrVersionId ) { hmrStart ( this , cmpMeta , hmrVersionId ) ; } ; } if ( BUILD . scopedSlotTextContentFix ) { patchTextContent ( HostElement . prototype , cmpMeta ) ; } cmpMeta . $lazyBundleId$ = lazyBundle [ 0 ] ; if ( ! exclude . includes ( tagName ) && ! customElements . get ( tagName ) ) { cmpTags . push ( tagName ) ; customElements . define ( tagName , proxyComponent ( HostElement as any , cmpMeta , PROXY_FLAGS . isElementConstructor ) as any ) ; } } ) ; } ) ; if ( BUILD . invisiblePrehydration && ( BUILD . hydratedClass || BUILD . hydratedAttribute ) ) { visibilityStyle . innerHTML = cmpTags + HYDRATED_CSS ; visibilityStyle . setAttribute ( 'data-styles' , '' ) ; head . insertBefore ( visibilityStyle , metaCharset ? metaCharset . nextSibling : head . firstChild ) ; } isBootstrapping = false ; if ( deferredConnectedCallbacks . length ) { deferredConnectedCallbacks . map ( ( host ) => host . connectedCallback ( ) ) ; } else { if ( BUILD . profile ) { plt . jmp ( ( ) => ( appLoadFallback = setTimeout ( appDidLoad , 30 , 'timeout' ) ) ) ; } else { plt . jmp ( ( ) => ( appLoadFallback = setTimeout ( appDidLoad , 30 ) ) ) ; } } endBootstrap ( ) ; } ;