forked from systemjs/systemjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amd.js
112 lines (101 loc) · 3.38 KB
/
amd.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
(function () {
function errMsg(errCode, msg) {
return (msg || "") + " (SystemJS Error#" + errCode + " " + "https://github.com/systemjs/systemjs/blob/main/docs/errors.md#" + errCode + ")";
}
/*
* Support for AMD loading
*/
(function (global) {
function unsupportedRequire () {
throw Error(errMsg(5, 'AMD require not supported.'));
}
var requireExportsModule = ['require', 'exports', 'module'];
function createAMDRegister (amdDefineDeps, amdDefineExec) {
var exports = {};
var module = { exports: exports };
var depModules = [];
var setters = [];
var splice = 0;
for (var i = 0; i < amdDefineDeps.length; i++) {
var id = amdDefineDeps[i];
var index = setters.length;
if (id === 'require') {
depModules[i] = unsupportedRequire;
splice++;
}
else if (id === 'module') {
depModules[i] = module;
splice++;
}
else if (id === 'exports') {
depModules[i] = exports;
splice++;
}
else {
createSetter(i);
}
if (splice)
amdDefineDeps[index] = id;
}
if (splice)
amdDefineDeps.length -= splice;
var amdExec = amdDefineExec;
return [amdDefineDeps, function (_export) {
_export({ default: exports, __useDefault: true });
return {
setters: setters,
execute: function () {
var amdResult = amdExec.apply(exports, depModules);
if (amdResult !== undefined)
module.exports = amdResult;
_export(module.exports);
_export('default', module.exports);
}
};
}];
// needed to avoid iteration scope issues
function createSetter(idx) {
setters.push(function (ns) {
depModules[idx] = ns.__useDefault ? ns.default : ns;
});
}
}
global.define = function (arg1, arg2, arg3) {
var isNamedRegister = typeof arg1 === 'string';
var name = isNamedRegister ? arg1 : null;
var depArg = isNamedRegister ? arg2 : arg1;
var execArg = isNamedRegister ? arg3 : arg2;
// The System.register(deps, exec) arguments
var deps, exec;
// define([], function () {})
if (Array.isArray(depArg)) {
deps = depArg;
exec = execArg;
}
// define({})
else if (typeof depArg === 'object') {
deps = [];
exec = function () { return depArg };
}
// define(function () {})
else if (typeof depArg === 'function') {
deps = requireExportsModule;
exec = depArg;
} else {
throw Error(errMsg(9, 'Invalid call to AMD define()'));
}
var amdRegister = createAMDRegister(deps, exec);
if (isNamedRegister) {
if (System.registerRegistry) {
System.registerRegistry[name] = amdRegister;
System.register(name, amdRegister[0], amdRegister[1]);
} else
console.warn(errMsg('W6', 'Include named-register.js for full named define support'));
// TODO: create new warning number and documentation for using named define without named-register extra
System.register(amdRegister[0], amdRegister[1]);
} else
System.register(amdRegister[0], amdRegister[1]);
};
global.define.amd = {};
})(typeof self !== 'undefined' ? self : global);
})();