import { getScript , ILoadScriptOptions , isLoaded , loadScript } from './script' ; import utils from './utils/index' ; function requireModules < T extends any [ ] = any [ ] > ( modules ) { return new utils . Promise ( ( resolve , reject ) => { const errorHandler = window [ 'require' ] . on ( 'error' , reject ) ; window [ 'require' ] ( modules , ( ... args ) => { errorHandler . remove ( ) ; resolve ( args as T ) ; } ) ; } ) ; } export function loadModules < T extends any [ ] = any [ ] > ( modules , loadScriptOptions = { } ) { if ( ! isLoaded ( ) ) { const script = getScript ( ) ; const src = script && script . getAttribute ( 'src' ) ; if ( ! loadScriptOptions . url && src ) { loadScriptOptions . url = src ; } return loadScript ( loadScriptOptions ) . then ( ( ) => requireModules < T > ( modules ) ) ; } else { return requireModules ( modules ) ; } }