Created
September 8, 2017 12:38
-
-
Save maple3142/7795959116fe0585c13a0aafb9553a5b to your computer and use it in GitHub Desktop.
require from unpkg in browser (need jquery)
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
//require single module | |
require('vue').then(function(Vue){ | |
//do something | |
}) | |
//require multiple module | |
require(['jquery','lodash','lowdb']).then(function([$,_,low]){ | |
//do something | |
}) |
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
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