forked from dmail-fork/systemjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamd.js
166 lines (148 loc) · 4.97 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Support for AMD loading
*/
(function (global) {
const systemPrototype = global.System.constructor.prototype;
const emptyInstantiation = [[], function () { return {} }];
function unsupportedRequire () {
throw Error('AMD require not supported.');
}
let tmpRegister, firstNamedDefine;
function emptyFn () {}
const requireExportsModule = ['require', 'exports', 'module'];
function createAMDRegister (amdDefineDeps, amdDefineExec) {
const exports = {};
const module = { exports: exports };
const depModules = [];
const setters = [];
let splice = 0;
for (let i = 0; i < amdDefineDeps.length; i++) {
const id = amdDefineDeps[i];
const 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 {
// needed for ie11 lack of iteration scope
const idx = i;
setters.push(function (ns) {
depModules[idx] = ns.__useDefault ? ns.default : ns;
});
}
if (splice)
amdDefineDeps[index] = id;
}
if (splice)
amdDefineDeps.length -= splice;
const amdExec = amdDefineExec;
return [amdDefineDeps, function (_export) {
_export({ default: exports, __useDefault: true });
return {
setters: setters,
execute: function () {
const amdResult = amdExec.apply(exports, depModules);
if (amdResult !== undefined)
module.exports = amdResult;
if (exports !== module.exports)
_export('default', module.exports);
}
};
}];
}
// hook System.register to know the last declaration binding
let lastRegisterDeclare;
const systemRegister = systemPrototype.register;
systemPrototype.register = function (name, deps, declare) {
lastRegisterDeclare = typeof name === 'string' ? declare : deps;
systemRegister.apply(this, arguments);
};
const instantiate = systemPrototype.instantiate;
systemPrototype.instantiate = function() {
// Reset "currently executing script"
amdDefineDeps = null;
return instantiate.apply(this, arguments);
};
const getRegister = systemPrototype.getRegister;
systemPrototype.getRegister = function () {
if (tmpRegister)
return tmpRegister;
const _firstNamedDefine = firstNamedDefine;
firstNamedDefine = null;
const register = getRegister.call(this);
// if its an actual System.register leave it
if (register && register[1] === lastRegisterDeclare)
return register;
const _amdDefineDeps = amdDefineDeps;
amdDefineDeps = null;
// If the script registered a named module, return that module instead of re-instantiating it.
if (_firstNamedDefine)
return _firstNamedDefine;
// otherwise AMD takes priority
// no registration -> attempt AMD detection
if (!_amdDefineDeps)
return register || emptyInstantiation;
return createAMDRegister(_amdDefineDeps, amdDefineExec);
};
let amdDefineDeps, amdDefineExec;
global.define = function (name, deps, execute) {
// define('', [], function () {})
if (typeof name === 'string') {
const depsAndExec = getDepsAndExec(deps, execute);
if (amdDefineDeps) {
if (!System.registerRegistry)
throw Error('Include the named register extension for SystemJS named AMD support.');
addToRegisterRegistry(name, createAMDRegister(depsAndExec[0], depsAndExec[1]));
amdDefineDeps = [];
amdDefineExec = emptyFn;
return;
}
else {
if (System.registerRegistry)
addToRegisterRegistry(name, createAMDRegister([].concat(depsAndExec[0]), depsAndExec[1]));
name = deps;
deps = execute;
}
}
const depsAndExec = getDepsAndExec(name, deps);
amdDefineDeps = depsAndExec[0];
amdDefineExec = depsAndExec[1];
};
global.define.amd = {};
function getDepsAndExec(arg1, arg2) {
// define([], function () {})
if (arg1 instanceof Array) {
return [arg1, arg2];
}
// define({})
else if (typeof arg1 === 'object') {
return [[], function () { return arg1 }];
}
// define(function () {})
else if (typeof arg1 === 'function') {
return [requireExportsModule, arg1];
}
}
function addToRegisterRegistry(name, define) {
if (!firstNamedDefine) {
firstNamedDefine = define;
Promise.resolve().then(function () {
firstNamedDefine = null;
});
}
// We must call System.getRegister() here to give other extras, such as the named-exports extra,
// a chance to modify the define before it's put into the registerRegistry.
// See https://github.com/systemjs/systemjs/issues/2073
tmpRegister = define;
System.registerRegistry[name] = System.getRegister();
tmpRegister = null;
}
})(typeof self !== 'undefined' ? self : global);