-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
80 lines (71 loc) · 2.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
var promise = require('sync-p')
var all = require('sync-p/all')
var fetchJs = require('fetch-js')
var path = require('./lib/path')
var map = require('./lib/map')
module.exports = function (options) {
options = options || {}
var modules = {}
var waiting = {}
var anons = []
function req (deps, cb, fail) {
if (typeof deps === 'string') {
if (deps in modules) return modules[deps]
throw new Error('Module not loaded: ' + deps)
}
return all(map(deps || [], fetch)).then(evaluate)['catch'](fail)
function evaluate (deps) {
if (typeof cb !== 'function') return cb
var m = { exports: {} }
var ret = cb.apply(null, map(deps, function (dep) {
if (dep === 'exports') return m.exports
if (dep === 'module') return m
return dep
}))
return typeof ret === 'undefined' ? m.exports : ret
}
}
function def (name, deps, cb, fail) {
if (typeof name !== 'string') return anons.push(arguments)
if (!cb) {
cb = deps
deps = false
}
if (!deps) deps = (cb && cb.length > 1) ? ['require', 'exports', 'module'] : []
waiting[name] = reqLocal(deps, cb).then(register)
return waiting[name]['catch'](fail)
function reqLocal (deps, cb) {
return typeof deps === 'string'
? req(path(name, deps, true))
: req(map(deps, localize), cb)
}
function register (m) {
modules[name] = m
delete waiting[name]
return m
}
function localize (dep) {
return dep === 'require' ? reqLocal : path(name, dep, true)
}
}
function fetch (name) {
if (typeof name !== 'string') return name
if (name === 'exports' || name === 'module') return name
if (waiting[name] || name in modules) return waiting[name] || modules[name]
return promise(function (resolve, reject) {
setTimeout(function lookup () {
if (waiting[name] || name in modules) return resolve(waiting[name] || modules[name])
fetchJs(path(options.base, name), function (err) {
if (err) return reject(err)
if (waiting[name] || name in modules) return resolve(waiting[name] || modules[name])
if (anons.length) {
var args = anons.pop()
return resolve(def(name, args[0], args[1]))
}
return resolve(def(name))
})
}, 0)
})
}
return { require: req, define: def }
}