Skip to content

Instantly share code, notes, and snippets.

@janwirth
Last active December 18, 2017 17:08
Show Gist options
  • Save janwirth/2bc3b33cea77e49d9429454efa70024e to your computer and use it in GitHub Desktop.
Save janwirth/2bc3b33cea77e49d9429454efa70024e to your computer and use it in GitHub Desktop.
Contain global JS libraries
// Load the given libraries without polluting the global namespace.
const libDefs = [
['https://cdn.jsdelivr.net/npm/[email protected]/dist/handlebars.min.js', 'Handlebars'],
['https://cdn.jsdelivr.net/npm/[email protected]', 'L'],
['https://cdn.jsdelivr.net/npm/[email protected]/dist/lodash.js', '_'],
['https://cdn.jsdelivr.net/npm/[email protected]', 'jQuery'],
['https://rawgit.com/joaquingatica/713768/raw/126357dd46a0e6aef4a8441a77d1e8e3ae81c0ba/typeface.js', '_typeface_js']
]
const i = document.createElement('iframe')
document.children[0].append(i)
i.contentWindow.eval(`
window.load = ([url, varName]) => fetch(url).then(response => response.text()).then( script =>{
eval(script)
window[varName] = eval(varName)
return eval(varName)
})
`)
const load = i.contentWindow.load
Promise.all(libDefs.map( load )).then( libs => {
console.log(libs)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment