This repository has been archived by the owner on Jun 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commonjs.js
122 lines (103 loc) · 2.73 KB
/
commonjs.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*!
* Ender: open module JavaScript framework (module-lib)
* http://enderjs.com
* License MIT
*/
var global = this
/**
* @param {string} id module id to load
* @return {object}
*/
function require(id) {
if ('$' + id in require._cache)
return require._cache['$' + id]
if ('$' + id in require._modules)
return (require._cache['$' + id] = require._modules['$' + id]._load())
if (id in window)
return window[id]
throw new Error('Requested module "' + id + '" has not been defined.')
}
/**
* @param {string} id module id to provide to require calls
* @param {object} exports the exports object to be returned
*/
function provide(id, exports) {
return (require._cache['$' + id] = exports)
}
/**
* @expose
* @dict
*/
require._cache = {}
/**
* @expose
* @dict
*/
require._modules = {}
/**
* @constructor
* @param {string} id module id for this module
* @param {function(Module, object, function(id), object)} fn module definition
*/
function Module(id, fn) {
this.id = id
this.fn = fn
require._modules['$' + id] = this
}
/**
* @expose
* @param {string} id module id to load from the local module context
* @return {object}
*/
Module.prototype.require = function (id) {
var parts, i
if (id.charAt(0) == '.') {
parts = (this.id.replace(/\/.*?$/, '/') + id.replace(/\.js$/, '')).split('/')
while (~(i = parts.indexOf('.')))
parts.splice(i, 1)
while ((i = parts.lastIndexOf('..')) > 0)
parts.splice(i - 1, 2)
id = parts.join('/')
}
return require(id)
}
/**
* @expose
* @return {object}
*/
Module.prototype._load = function () {
var m = this
var dotdotslash = /^\.\.\//g
var dotslash = /^\.\/[^\/]+$/g
if (!m._loaded) {
m._loaded = true
/**
* @expose
*/
m.exports = {}
m.fn.call(global, m, m.exports, function (id) {
if (id.match(dotdotslash)) {
id = m.id.replace(/[^\/]+\/[^\/]+$/, '') + id.replace(dotdotslash, '')
}
else if (id.match(dotslash)) {
id = m.id.replace(/\/[^\/]+$/, '') + id.replace('.', '')
}
return m.require(id)
}, global)
}
return m.exports
}
/**
* @expose
* @param {string} id main module id
* @param {Object.<string, function>} modules mapping of module ids to definitions
* @param {string} main the id of the main module
*/
Module.createPackage = function (id, modules, main) {
var path, m
for (path in modules) {
new Module(id + '/' + path, modules[path])
if (m = path.match(/^(.+)\/index$/)) new Module(id + '/' + m[1], modules[path])
}
if (main) require._modules['$' + id] = require._modules['$' + id + '/' + main]
}