-
Notifications
You must be signed in to change notification settings - Fork 1
/
amd.js
296 lines (247 loc) · 7.16 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
* AMD lite implementation
* https://github.com/zandroid/amd-lite
*
* Copyright (c) 2014 Andrey Zaytsev
* MIT License
*
*/
;( function( glob, undefined ) {
"use strict";
function noop() {}
function logModuleIsBuilt( name ) {
console.log( 'Module "' + name + ' is built' );
}
function handlePromise( promise, descriptor ) {
var currentSession = _session;
( promise.done || promise.then ).call( promise, function( result ) {
if ( currentSession === _session ) {
descriptor.exports = result;
if ( define.verbose ) {
logModuleIsBuilt( descriptor.name );
}
_check( descriptor.name );
}
} );
}
var _modules = {},
_expected = {},
_session = Date.now();
function define( name, dependencies, module ) {
var asis, m, thenable;
if ( name === true ) {
// define( asis === true, name, module );
name = dependencies;
dependencies = [];
asis = true;
}
else if ( arguments.length < 3 ) {
// define( name, module );
module = dependencies;
dependencies = undefined;
asis = !module || typeof module !== 'function' && !module.then;
}
thenable = !asis && !!module.then;
if ( typeof name !== 'string' ) {
throw new Error( 'Module name is not a string' );
}
if ( name in _modules ) {
throw new Error( 'Redefinition of module "' + name + '"' );
}
if ( asis || thenable ) {
dependencies = undefined;
}
else if ( !Array.isArray( dependencies ) ) {
if ( typeof dependencies === 'string' ) {
dependencies = dependencies.split( ' ' );
}
else if ( dependencies === undefined ) {
dependencies = asis ? [] : [ 'require', 'exports', 'module' ];
}
else {
throw new Error( 'Dependencies argument is not an array' );
}
}
m = _modules[ name ] = {
name: name,
dependencies: dependencies,
thenable: thenable
};
if ( asis ) {
m.exports = module;
}
else if ( thenable ) {
handlePromise( module, m );
}
else {
m.module = module;
}
if ( define.verbose ) {
console.log( 'Module "' + name + '" is defined' );
if ( asis ) {
logModuleIsBuilt( name );
}
}
if ( name in _expected || define.lazy === false ) {
_build( [], m );
}
}
function require( dependencies, callback ) {
var m;
if ( arguments.length === 1 && typeof dependencies === 'string' ) {
// require( moduleName ) => module.exports or throw Error
if ( define.verbose ) {
console.log( 'Module "' + dependencies + '" is required' );
}
m = _modules[ dependencies ];
if ( !m ) {
throw new Error( 'Module "' + dependencies + '" is not defined' );
}
if ( !_build( [], m ) ) {
throw new Error( 'Module "' + dependencies + '" cannot be built' );
}
return m.exports;
}
if ( typeof dependencies === 'string' ) {
dependencies = Array.prototype.slice.call( arguments );
callback = typeof dependencies[ dependencies.length - 1 ] !== 'string' ? dependencies.pop() : noop;
}
else if ( !Array.isArray( dependencies ) ) {
throw new Error( 'Dependencies argument is not an array' );
}
if ( define.verbose ) {
console.log( 'Modules collection [ "' + dependencies.join( '", "' ) + '" ] is required' );
}
if ( callback === undefined ) {
callback = noop;
}
else if ( typeof callback !== 'function' ) {
throw new Error( 'Callback argument is not a function' );
}
_build( [], {
name: 'require',
dependencies: dependencies,
module: callback
} );
return undefined;
}
function _build( stack, m ) {
var name = m.name,
isModule = name !== 'require',
module, deps,
e = false;
if ( 'exports' in m ) {
_check( name );
return m;
}
if ( m.thenable ) {
return undefined;
}
if ( isModule ) {
e = stack.indexOf( name ) > -1;
stack = stack.concat( [ name ] );
}
if ( e ) {
throw new Error( 'Cyclic dependency: ' + stack.join( ' > ' ) );
}
if ( define.verbose && isModule ) {
console.log( 'Trying to build module "' + name + '", stack: ' + stack.join( ' > ' ) );
}
deps = [];
module = { id: name, exports: {} };
e = m.dependencies.every( function( depName, dep ) {
// var dep;
switch ( depName ) {
case 'require':
dep = require;
break;
case 'module':
dep = module;
break;
case 'exports':
dep = module.exports;
break;
default:
dep = _modules[ depName ];
if ( dep ) {
dep = _build( stack, dep );
dep = dep && dep.exports;
}
if ( dep === undefined ) {
if ( define.verbose ) {
console.log( isModule ? 'Module "' + name + '" awaits for "' + depName + '"' : 'Module "' + depName + '" is awaited' );
}
dep = _expected[ depName ];
if ( !dep ) {
_expected[ depName ] = {
targets: [ m ]
};
}
else if ( dep.targets.indexOf( m ) === -1 ) {
dep.targets.push( m );
}
return false;
}
break;
}
deps.push( dep );
return true;
} );
if ( e ) {
e = m.module.apply( module, deps );
if ( e === undefined ) {
e = module.exports;
}
if ( e && e.then ) {
m.thenable = true;
handlePromise( e, m );
}
else {
m.exports = e;
if ( define.verbose && isModule ) {
logModuleIsBuilt( name );
}
_check( name );
return m;
}
}
return undefined;
}
function _check( name ) {
var t;
if ( name in _expected ) {
t = _expected[ name ].targets;
delete _expected[ name ];
t.forEach( _build.bind( null, [] ) );
}
}
define.amd = {};
define._modules = _modules;
define._expected = _expected;
define.verbose = false;
define.asis = function( name, module ) {
define( true, name, module );
};
define.required = function( name, dependencies, module ) {
require( [ name ] );
define.apply( this, arguments );
};
define.clean = function() {
define._modules = _modules = {};
define._expected = _expected = {};
define._session = _session = Date.now();
if ( define.verbose ) {
console.log( 'AMD list is clean' );
}
};
define.require = require;
glob.AMD = {
define: define,
require: require,
namespace: function( obj ) {
obj = obj || {};
obj.define = define;
obj.require = require;
}
};
} )( this );