-
Notifications
You must be signed in to change notification settings - Fork 2
/
cjs.js
38 lines (32 loc) · 1.08 KB
/
cjs.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
define(function () {
var commentRegExp = /(\/\*([\s\S]*?)\*\/|\/\/(.*)$)/mg,
cjsRequireRegExp = /require\(["']([^'"\s]+)["']\)/g;
return {
load: function (target, require, load) {
// fetch the raw text
require(['text!' + target + '.js'], function (source) {
// start with the "standard" dependencies
var deps = ['require', 'exports', 'module'],
wrapped;
// scan the source for other dependencies
source.replace(commentRegExp, '').replace(cjsRequireRegExp, function (match, dep) {
deps.push('cjs!' + dep);
});
wrapped = "define(['" + deps.join("','") + "'],function (req, exports, module) {" +
"function require(id) {" +
"return req('cjs!'+ id);" +
"}" +
source +
'});';
// workaround IE conditional comments
/*@if (@_jscript) @else @*/
wrapped += '\n//@ sourceURL=' + require.toUrl(target);
/*@end@*/
// simulate injecting the wrapped source
load.fromText('cjs!' + target, wrapped);
// get the module we just defined and return it via load
require(['cjs!' + target], load);
});
}
};
});