import type * as d from '../declarations' ; import { BUILD , NAMESPACE } from '@app-data' ; import { consoleDevInfo , H , doc , plt , promiseResolve , win } from '@platform' ; import { getDynamicImportFunction } from '@utils' ; export const patchBrowser = ( ) : Promise < d . CustomElementsDefineOptions > => { if ( BUILD . isDev && ! BUILD . isTesting ) { consoleDevInfo ( 'Running in development mode.' ) ; } if ( BUILD . cssVarShim ) { plt . $cssShim$ = ( win as any ) . __cssshim ; } if ( BUILD . cloneNodeFix ) { patchCloneNodeFix ( ( H as any ) . prototype ) ; } if ( BUILD . profile && ! performance . mark ) { performance . mark = performance . measure = ( ) => { } ; performance . getEntriesByName = ( ) => [ ] ; } const scriptElm = BUILD . scriptDataOpts || BUILD . safari10 || BUILD . dynamicImportShim ? Array . from ( doc . querySelectorAll ( 'script' ) ) . find ( ( s ) => new RegExp ( `\/${ NAMESPACE }(\\.esm)?\\.js($|\\?|#)` ) . test ( s . src ) || s . getAttribute ( 'data-stencil-namespace' ) === NAMESPACE ) : null ; const importMeta = import . meta . url ; const opts = BUILD . scriptDataOpts ? ( scriptElm as any ) [ 'data-opts' ] || { } : { } ; if ( BUILD . safari10 && 'onbeforeload' in scriptElm && ! history . scrollRestoration ) { return { then ( ) { } , } as any ; } if ( ! BUILD . safari10 && importMeta !== '' ) { opts . resourcesUrl = new URL ( '.' , importMeta ) . href ; } else if ( BUILD . dynamicImportShim || BUILD . safari10 ) { opts . resourcesUrl = new URL ( '.' , new URL ( scriptElm . getAttribute ( 'data-resources-url' ) || scriptElm . src , win . location . href ) ) . href ; if ( BUILD . dynamicImportShim ) { patchDynamicImport ( opts . resourcesUrl , scriptElm ) ; } if ( BUILD . dynamicImportShim && ! win . customElements ) { return import ( './polyfills/dom.js' ) . then ( ( ) => opts ) ; } } return promiseResolve ( opts ) ; } ; const patchDynamicImport = ( base , orgScriptElm ) => { const importFunctionName = getDynamicImportFunction ( NAMESPACE ) ; try { ( win as any ) [ importFunctionName ] = new Function ( 'w' , `return import(w);//${ Math . random ( ) }` ) ; } catch ( e ) { const moduleMap = new Map < string , any > ( ) ; ( win as any ) [ importFunctionName ] = ( src ) => { const url = new URL ( src , base ) . href ; let mod = moduleMap . get ( url ) ; if ( ! mod ) { const script = doc . createElement ( 'script' ) ; script . type = 'module' ; script . crossOrigin = orgScriptElm . crossOrigin ; script . src = URL . createObjectURL ( new Blob ( [ `import * as m from '${ url }'; window.${ importFunctionName }.m = m;` ] , { type : 'application/javascript' , } ) ) ; mod = new Promise ( ( resolve ) => { script . onload = ( ) => { resolve ( ( win as any ) [ importFunctionName ] . m ) ; script . remove ( ) ; } ; } ) ; moduleMap . set ( url , mod ) ; doc . head . appendChild ( script ) ; } return mod ; } ; } } ; const patchCloneNodeFix = ( HTMLElementPrototype ) => { const nativeCloneNodeFn = HTMLElementPrototype . cloneNode ; HTMLElementPrototype . cloneNode = function ( this , deep ) { if ( this . nodeName === 'TEMPLATE' ) { return nativeCloneNodeFn . call ( this , deep ) ; } const clonedNode = nativeCloneNodeFn . call ( this , false ) ; const srcChildNodes = this . childNodes ; if ( deep ) { for ( let i = 0 ; i < srcChildNodes . length ; i ++ ) { if ( srcChildNodes [ i ] . nodeType !== 2 ) { clonedNode . appendChild ( srcChildNodes [ i ] . cloneNode ( true ) ) ; } } } return clonedNode ; } ; } ;