I think we can write some function like this
const fetchJsFromCDN = (src, externals = []) => { new Promise((resolve, reject) => { const script = document.createElement('script') script.setAttribute('src', src) script.addEventListener('load', () => { resolve(externals.map(key => { const ext = window[key] typeof ext === 'undefined' && console.warn(`No external named '${key}' in window`) return ext })) }) script.addEventListener('error', reject) document.body.appendChild(script) }) } fetchJsFromCDN('//cdn.jsdelivr.net/npm/eruda', ['eruda']).then(([eruda]) => eruda.init())
without a function like require , the CDN source can insert an object into the window so that we can get it using the name
CSS files may be easier to import this way
CJY0208
source share