Skip to content

Instantly share code, notes, and snippets.

@maple3142
Created September 8, 2017 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maple3142/7795959116fe0585c13a0aafb9553a5b to your computer and use it in GitHub Desktop.
Save maple3142/7795959116fe0585c13a0aafb9553a5b to your computer and use it in GitHub Desktop.
require from unpkg in browser (need jquery)
//require single module
require('vue').then(function(Vue){
//do something
})
//require multiple module
require(['jquery','lodash','lowdb']).then(function([$,_,low]){
//do something
})
function _require(pkg) {
return new Promise(function(res,rej){
var exports = {}
var module = {
_exports: exports,
get exports() {
return this._exports
},
set exports(v) {
this._exports = v
}
}
var env = { exports, module }
$.get('https://unpkg.com/'+pkg).then(function(d){
eval(d)
res(module.exports)
})
})
}
function require(ar) {
return new Promise(function(res,rej){
var lst = ar
if (!Array.isArray(ar))
lst = [ar]
Promise.all(lst.map(function (m) {
return _require(m)
})).then(function (r) {
Array.isArray(ar) ? res(r) : res(r[0])
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment