-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (122 loc) · 3.85 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
self.uceRequire = (function (exports) {
'use strict';
var Promise$1 = typeof Promise === 'function' ? Promise : function (fn) {
var queue = [],
resolved = 0,
value;
fn(function ($) {
value = $;
resolved = 1;
queue.splice(0).forEach(then);
});
return {
then: then
};
function then(fn) {
return resolved ? setTimeout(fn, 0, value) : queue.push(fn), this;
}
};
if (!Promise$1.all) Promise$1.all = function (list) {
return new Promise$1(function ($) {
var length = list.length;
if (!length) $();
var i = 0;
while (i < length) {
list[i++].then(update);
}
i = 0;
function update() {
if (++i === length) $();
}
});
};
var create = Object.create,
defineProperty = Object.defineProperty,
keys = Object.keys;
var lazyModules = [];
var strict = '"use strict;"\n';
var $require = function $require(module) {
return cache[module];
};
var cache = create(null);
var waiting = {};
var asCJS = function asCJS(esm, require) {
var exports = [];
var imports = [];
var cjs = esm.replace(/(^|[\r\n])\s*import\s*((['|"])[^\3]+?\3)/g, function (_, $1, $2) {
return $1 + 'require(' + $2 + ')';
}).replace(/(^|[\r\n])\s*import\s*([^\3]+?)(\s*from\s*)((['|"])[^\5]+?\5)/g, function (_, $1, $2, $, $3) {
return $1 + 'const ' + $2.replace(/\s+as\s+/g, ':') + ' = require(' + $3 + ')';
}).replace(/^\s*export\s+default(\s*)/mg, 'exports.default =$1').replace(/(^|[\r\n])\s*export\s*\{([^}]+?)\}[^\n]*/g, function (_, $, $1) {
$1.trim().split(/\s*,\s*/).forEach(function (name) {
exports.push("exports.".concat(name, " = ").concat(name, ";"));
});
return $;
}).replace(/(^|[\r\n])\s*export\s+(const|let|var|function)(\s+)(\w+)/g, function (_, $, $1, $2, $3) {
exports.push("exports.".concat($3, " = ").concat($3, ";"));
return $ + $1 + $2 + $3;
}).concat('\n', exports.join('\n')).replace(/require\s*\(\s*(['"])([^\1]+?)\1\s*\)/g, function ($, _, module) {
imports.push(module);
return $;
});
if (require) {
imports.forEach(function (key) {
if (!(key in cache)) {
lazyModules.push(new Promise$1(function ($) {
var module = waiting;
if (/^(?:[./]|https?:)/.test(key)) {
cache[key] = module;
var xhr = new XMLHttpRequest();
xhr.open('get', key, true);
xhr.send(null);
xhr.onload = function () {
$(cache[key] = loader(xhr.responseText));
};
} else {
defineProperty(cache, key, {
get: function get() {
return module;
},
set: function set(value) {
$(module = value);
}
});
}
}));
}
});
return new Promise$1(function ($) {
Promise$1.all(lazyModules).then(function () {
return $(cjs);
});
});
}
return cjs;
};
var cjs = function cjs(extras) {
var args = keys(extras || {});
var values = args.map(function (k) {
return extras[k];
}).concat($require);
args.push('require');
return function (code) {
var exports = {};
var module = {
exports: exports
};
var params = args.concat('module', 'exports', strict + asCJS(code));
var fn = Function.apply(null, params);
fn.apply(null, values.concat(module, exports));
var result = module.exports;
var k = keys(result);
return k.length === 1 && k[0] === 'default' ? result["default"] : result;
};
};
var loader = cjs();
exports.asCJS = asCJS;
exports.cache = cache;
exports.cjs = cjs;
exports.loader = loader;
exports.waiting = waiting;
return exports;
}({}));