Last active
December 18, 2017 17:08
-
-
Save janwirth/2bc3b33cea77e49d9429454efa70024e to your computer and use it in GitHub Desktop.
Contain global JS libraries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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