import { globalModule } from './module' ; import { Module } from './module/Module' ; import { Record } from './module/Record' ; import { IBundleOption } from './type' ; param id param script param timeout function onScriptComplete ( id , script , timeout ) { script . onerror = script . onload = null ; clearTimeout ( timeout ) ; const record = Module . record . get ( id ) ; if ( id && id . endsWith && ! id . endsWith ( '_scripts_' ) && record && ! record . loaded ) { Module . record . delete ( id ) ; } } param id param script param timeout function onScriptError ( id , script , timeout ) { const record = Module . record . get ( id ) ; onScriptComplete ( id , script , timeout ) ; if ( record ) { record . reject ( new Error ( 'script load fail' ) ) ; } } param { IBundleOption } bundle function jsonpRequire ( id , url ) { const script = document . createElement ( 'script' ) ; script . charset = 'utf-8' ; script . src = url ; script . setAttribute ( 'nonce' , '' ) ; const timeout = window . setTimeout ( ( ) => { onScriptError ( id , script , timeout ) ; } , 120000 ) ; script . onerror = ( ) => { onScriptError ( id , script , timeout ) ; } ; script . onload = ( ) => { onScriptComplete ( id , script , timeout ) ; } ; document . head . appendChild ( script ) ; } export async function xmlRequire ( id , url , transform ) { const resp = await fetch ( url ) ; const code = await resp . text ( ) ; window . eval ( `__CONSOLE_OS_GLOBAL_HOOK__('${ id . replace ( '_scripts_' , '' ) }', function(require, module, exports, {window, location, history, document}){
    with(window.__CONSOLE_OS_GLOBAL_VARS_){
      ${ transform ( code ) }
    }
  })` ) ; } param bundle { IBundleOption } export async function requireEnsure < T > ( bundle ) { if ( ! bundle . transform ) { bundle . transform = ( source ) => source ; } if ( ! bundle . noCache && globalModule . resolved ( bundle . id ) ) { if ( bundle . context ) { return globalModule . requireIsolateWithContext ( bundle . id , bundle . context ) ; } return globalModule . require ( bundle . id ) ; } const promises = [ ] ; let chunkRecord = Module . record . get ( bundle . id ) ; if ( ! chunkRecord || ! chunkRecord . loaded ) { if ( chunkRecord ) { promises . push ( chunkRecord . promise ) ; } else { const promise = new Promise < T > ( function ( resolve , reject ) { chunkRecord = new Record ( ) ; chunkRecord . resolve = resolve ; chunkRecord . reject = reject ; chunkRecord . context = bundle . context ; chunkRecord . deps = bundle . deps ; Module . record . set ( bundle . id , chunkRecord ) ; } ) ; chunkRecord . promise = promise ; promises . push ( promise ) ; if ( bundle . xmlrequest ) { xmlRequire ( bundle . id , bundle . url , bundle . transform ) ; } else { jsonpRequire ( bundle . id , bundle . url ) ; } } } await Promise . all ( promises ) ; return globalModule . require ( bundle . id ) ; }