-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd.js
70 lines (67 loc) · 1.71 KB
/
md.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
(function (global) {
//factories cache
var factories = {};
//modules cache
var modules = {};
//defined cache
var defined = {};
var PREFIX = 'components/';
var PREFIX_LENGHT = PREFIX.length;
/**
* define(id, factory), define(factory)
*
* @param {String|Function} id
* @param {Function|undefined} factory
*/
global.define = function (id, factory) {
switch (typeof id) {
case 'string':
if (defined.hasOwnProperty(id)) {
throw new Error('cannot redeclare module [' + id + ']');
} else {
if (typeof factory === 'function') {
factories[id] = factory;
} else {
modules[id] = factory;
}
}
defined[id] = true;
break;
case 'function':
id(require);
break;
}
};
/**
* require(id)
*
* @param {String} id
* @returns {*}
*/
var require = global.require = function (id) {
if(id.indexOf('.') === -1){
var last = id.split('/').pop();
id = id + '/' + last + '.js';
}
if(id.substring(0, PREFIX_LENGHT) !== PREFIX){
id = 'components/' + id;
}
if (modules.hasOwnProperty(id)) { //if cached
return modules[id];
} else if (factories.hasOwnProperty(id)) { //has factory
var module = {exports: {}},
factory = factories[id],
exports = modules[id]
= module.exports;
exports = factory(require, exports, module);
if (typeof exports === 'undefined') {
modules[id] = exports = module.exports;
} else {
modules[id] = exports;
}
return exports;
} else { //undefined module
throw new Error('undefined module [' + id + ']');
}
};
})(window);