forked from seajs/seajs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sea-debug.js
1083 lines (843 loc) · 23.3 KB
/
sea-debug.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @preserve SeaJS v2.0.0-beta | seajs.org/LICENSE.md
*/
(function(global, undefined) {
"use strict"
// Avoid conflicting when `sea.js` is loaded multiple times
if (global.seajs) {
return
}
var seajs = global.seajs = {
// The current version of SeaJS being used
version: "2.0.0-beta"
}
/**
* util-lang.js - The minimal language enhancement
*/
var hasOwnProperty = {}.hasOwnProperty
function hasOwn(obj, prop) {
return hasOwnProperty.call(obj, prop)
}
function isFunction(obj) {
return typeof obj === "function"
}
var isArray = Array.isArray || function(obj) {
return obj instanceof Array
}
function unique(arr) {
var obj = {}
var ret = []
for (var i = 0, len = arr.length; i < len; i++) {
var item = arr[i]
if (obj[item] !== 1) {
obj[item] = 1
ret.push(item)
}
}
return ret
}
/**
* util-log.js - The tiny log function
*/
// The safe wrapper for `console.xxx` functions
// log("message") ==> console.log("message")
// log("message", "warn") ==> console.warn("message")
var log = seajs.log = function(msg, type) {
var console = global.console
if (console) {
// Do NOT print `log(msg)` in non-debug mode
if (type || configData.debug) {
(console[type] || console["log"]).call(console, msg)
}
}
}
/**
* util-events.js - The minimal events support
*/
var eventsCache = {}
// Bind event
seajs.on = function(event, callback) {
if (!callback) return seajs
var list = eventsCache[event] || (eventsCache[event] = [])
list.push(callback)
return seajs
}
// Remove event. If `callback` is undefined, remove all callbacks for the
// event. If `event` and `callback` are both undefined, remove all callbacks
// for all events
seajs.off = function(event, callback) {
// Remove *all* events
if (!(event || callback)) {
eventsCache = {}
return seajs
}
var list = eventsCache[event]
if (list) {
if (callback) {
for (var i = list.length - 1; i >= 0; i--) {
if (list[i] === callback) {
list.splice(i, 1)
}
}
}
else {
delete eventsCache[event]
}
}
return seajs
}
// Emit event, firing all bound callbacks. Callbacks are passed the same
// arguments as `emit` is, apart from the event name
var emit = seajs.emit = function(event, data) {
var list = eventsCache[event], fn
if (list) {
// Copy callback lists to prevent modification
list = list.slice()
// Execute event callbacks
while ((fn = list.shift())) {
fn(data)
}
}
return seajs
}
// Emit event and return the specified property of the data
function emitData(event, data, prop) {
emit(event, data)
return data[prop]
}
/**
* util-path.js - The utilities for operating path such as id, uri
*/
var DIRNAME_RE = /[^?]*(?=\/.*$)/
var MULTIPLE_SLASH_RE = /([^:\/])\/\/+/g
var URI_END_RE = /\?|\.(?:css|js)$|\/$/
var ROOT_RE = /^(.*?:\/\/.*?)(?:\/|$)/
var VARS_RE = /{([^{}]+)}/g
// Extract the directory portion of a path
// dirname("a/b/c.js") ==> "a/b/"
// dirname("d.js") ==> "./"
// ref: http://jsperf.com/regex-vs-split/2
function dirname(path) {
var s = path.match(DIRNAME_RE)
return (s ? s[0] : ".") + "/"
}
// Canonicalize a path
// realpath("./a//b/../c") ==> "a/c"
function realpath(path) {
// "file:///a//b/c" ==> "file:///a/b/c"
// "http://a//b/c" ==> "http://a/b/c"
// "https://a//b/c" ==> "https://a/b/c"
if (path.lastIndexOf("//") > 7) {
path = path.replace(MULTIPLE_SLASH_RE, "$1\/")
}
// If "a/b/c", just return
if (path.indexOf(".") === -1) {
return path
}
var original = path.split("/")
var ret = [], part
for (var i = 0, len = original.length; i < len; i++) {
part = original[i]
if (part === "..") {
if (ret.length === 0) {
throw new Error("Invalid path: " + path)
}
ret.pop()
}
else if (part !== ".") {
ret.push(part)
}
}
return ret.join("/")
}
// Normalize an uri
// normalize("path/to/a") ==> "path/to/a.js"
function normalize(uri) {
// Call realpath() before adding extension, so that most of uris will
// contains no `.` and will just return in realpath() call
uri = realpath(uri)
// Add the default `.js` extension except that the uri ends with `#`
var lastChar = uri.charAt(uri.length - 1)
if (lastChar === "#") {
uri = uri.slice(0, -1)
}
else if (!URI_END_RE.test(uri)) {
uri += ".js"
}
// issue #256: fix `:80` bug in IE
uri = uri.replace(":80/", "/")
return uri
}
function parseAlias(id) {
var alias = configData.alias
// Only parse top-level id
if (alias && hasOwn(alias, id) && isTopLevel(id)) {
id = alias[id]
}
return id
}
function parseVars(id) {
var vars = configData.vars
if (vars && id.indexOf("{") > -1) {
id = id.replace(VARS_RE, function(m, key) {
return hasOwn(vars, key) ? vars[key] : "{" + key + "}"
})
}
return id
}
function addBase(id, refUri) {
var ret
// absolute id
if (isAbsolute(id)) {
ret = id
}
// relative id
else if (isRelative(id)) {
// Convert "./a" to "a", to avoid unnecessary loop in realpath() call
if (id.indexOf("./") === 0) {
id = id.substring(2)
}
ret = dirname(refUri) + id
}
// root id
else if (isRoot(id)) {
ret = refUri.match(ROOT_RE)[1] + id
}
// top-level id
else {
ret = configData.base + id
}
return ret
}
function parseMap(uri) {
var map = configData.map || []
var ret = uri
var len = map.length
if (len) {
for (var i = 0; i < len; i++) {
var rule = map[i]
ret = isFunction(rule) ?
(rule(uri) || uri) :
uri.replace(rule[0], rule[1])
// Only apply the first matched rule
if (ret !== uri) break
}
}
return ret
}
function id2Uri(id, refUri) {
if (!id) return ""
id = parseAlias(id)
id = parseVars(id)
id = addBase(id, refUri || pageUri)
id = normalize(id)
id = parseMap(id)
return id
}
function isAbsolute(id) {
return id.indexOf("://") > 0 || id.indexOf("//") === 0
}
function isRelative(id) {
return id.indexOf("./") === 0 || id.indexOf("../") === 0
}
function isRoot(id) {
return id.charAt(0) === "/" && id.charAt(1) !== "/"
}
function isTopLevel(id) {
var c = id.charAt(0)
return id.indexOf("://") === -1 && c !== "." && c !== "/"
}
var doc = document
var loc = global.location
var pageUri = (function() {
var pathname = loc.pathname
// Normalize pathname to start with "/"
// ref: https://groups.google.com/forum/#!topic/seajs/9R29Inqk1UU
if (pathname.charAt(0) !== "/") {
pathname = "/" + pathname
}
var pageUri = loc.protocol + "//" + loc.host + pathname
// local file in IE: C:\path\to\xx.js
if (pageUri.indexOf("\\") > -1) {
pageUri = pageUri.replace(/\\/g, "/")
}
return pageUri
})()
// Recommend to add `seajs-node` id for the `sea.js` script element
var loaderScript = doc.getElementById("seajs-node") || (function() {
var scripts = doc.getElementsByTagName("script")
return scripts[scripts.length - 1] ||
// Maybe undefined in some environment such as PhantomJS
doc.createElement("script")
})()
// When `sea.js` is inline, set loaderDir according to pageUri
var loaderDir = dirname(getScriptAbsoluteSrc(loaderScript) || pageUri)
function getScriptAbsoluteSrc(node) {
return node.hasAttribute ? // non-IE6/7
node.src :
// see http://msdn.microsoft.com/en-us/library/ms536429(VS.85).aspx
node.getAttribute("src", 4)
}
/**
* util-request.js - The utilities for requesting script and style files
* ref: tests/research/load-js-css/test.html
*/
var head = doc.getElementsByTagName("head")[0] || doc.documentElement
var baseElement = head.getElementsByTagName("base")[0]
var IS_CSS_RE = /\.css(?:\?|$)/i
var READY_STATE_RE = /^(?:loaded|complete|undefined)$/
var currentlyAddingScript
var interactiveScript
function request(url, callback, charset) {
var isCSS = IS_CSS_RE.test(url)
var node = doc.createElement(isCSS ? "link" : "script")
if (charset) {
var cs = isFunction(charset) ? charset(url) : charset
if (cs) {
node.charset = cs
}
}
addOnload(node, callback, isCSS)
if (isCSS) {
node.rel = "stylesheet"
node.href = url
}
else {
node.async = true
node.src = url
}
// For some cache cases in IE 6-8, the script executes IMMEDIATELY after
// the end of the insert execution, so use `currentlyAddingScript` to
// hold current node, for deriving url in `define` call
currentlyAddingScript = node
// ref: #185 & http://dev.jquery.com/ticket/2709
baseElement ?
head.insertBefore(node, baseElement) :
head.appendChild(node)
currentlyAddingScript = null
}
function addOnload(node, callback, isCSS) {
var missingOnload = isCSS && (isOldWebKit || !("onload" in node))
// for Old WebKit and Old Firefox
if (missingOnload) {
setTimeout(function() {
pollCss(node, callback)
}, 1) // Begin after node insertion
return
}
node.onload = node.onerror = node.onreadystatechange = function() {
if (READY_STATE_RE.test(node.readyState)) {
// Ensure only run once and handle memory leak in IE
node.onload = node.onerror = node.onreadystatechange = null
// Remove the script to reduce memory leak
if (!isCSS && !configData.debug) {
head.removeChild(node)
}
// Dereference the node
node = undefined
callback()
}
}
}
function pollCss(node, callback) {
var sheet = node.sheet
var isLoaded
// for WebKit < 536
if (isOldWebKit) {
if (sheet) {
isLoaded = true
}
}
// for Firefox < 9.0
else if (sheet) {
try {
if (sheet.cssRules) {
isLoaded = true
}
} catch (ex) {
// The value of `ex.name` is changed from "NS_ERROR_DOM_SECURITY_ERR"
// to "SecurityError" since Firefox 13.0. But Firefox is less than 9.0
// in here, So it is ok to just rely on "NS_ERROR_DOM_SECURITY_ERR"
if (ex.name === "NS_ERROR_DOM_SECURITY_ERR") {
isLoaded = true
}
}
}
setTimeout(function() {
if (isLoaded) {
// Place callback here to give time for style rendering
callback()
}
else {
pollCss(node, callback)
}
}, 20)
}
function getCurrentScript() {
if (currentlyAddingScript) {
return currentlyAddingScript
}
// For IE6-9 browsers, the script onload event may not fire right
// after the the script is evaluated. Kris Zyp found that it
// could query the script nodes and the one that is in "interactive"
// mode indicates the current script
// ref: http://goo.gl/JHfFW
if (interactiveScript && interactiveScript.readyState === "interactive") {
return interactiveScript
}
var scripts = head.getElementsByTagName("script")
for (var i = scripts.length - 1; i >= 0; i--) {
var script = scripts[i]
if (script.readyState === "interactive") {
interactiveScript = script
return interactiveScript
}
}
}
// `onload` event is supported in WebKit < 535.23 and Firefox < 9.0
// ref:
// - https://bugs.webkit.org/show_activity.cgi?id=38995
// - https://bugzilla.mozilla.org/show_bug.cgi?id=185236
// - https://developer.mozilla.org/en/HTML/Element/link#Stylesheet_load_events
var isOldWebKit = Number(navigator.userAgent
.replace(/.*AppleWebKit\/(\d+)\..*/, "$1")) < 536
/**
* util-deps.js - The parser for dependencies
* ref: tests/research/parse-dependencies/test.html
*/
var REQUIRE_RE = /"(?:\\"|[^"])*"|'(?:\\'|[^'])*'|\/\*[\S\s]*?\*\/|\/(?:\\\/|[^/\r\n])+\/(?=[^\/])|\/\/.*|\.\s*require|(?:^|[^$])\brequire\s*\(\s*(["'])(.+?)\1\s*\)/g
var SLASH_RE = /\\\\/g
function parseDependencies(code) {
var ret = [], m
REQUIRE_RE.lastIndex = 0
code = code.replace(SLASH_RE, "")
while ((m = REQUIRE_RE.exec(code))) {
if (m[2]) ret.push(m[2])
}
return unique(ret)
}
/**
* module.js - The core of module loader
*/
var cachedModules = seajs.cache = {}
var STATUS = Module.STATUS = {
"INITIALIZED": 1, // The module is initialized
"FETCHING": 2, // The module file is being fetched now
"SAVED": 3, // The module data has been saved to cachedModules
"LOADED": 4, // The module and all its dependencies are ready to compile
"COMPILING": 5, // The module is being compiled
"COMPILED": 6 // The module is compiled and `module.exports` is available
}
function Module(uri, status) {
this.uri = uri
this.status = status || STATUS.INITIALIZED
this.dependencies = []
this.waitings = []
}
Module.prototype.load = function(ids, callback) {
var uris = resolve(isArray(ids) ? ids : [ids], this.uri)
load(uris, function() {
var exports = []
for (var i = 0, len = uris.length; i < len; i++) {
exports[i] = compile(cachedModules[uris[i]])
}
if (callback) {
callback.apply(global, exports)
}
})
return this
}
function resolve(ids, refUri) {
if (isArray(ids)) {
// Use `for` loop instead of `forEach` or `map` function for performance
var ret = []
for (var i = 0, len = ids.length; i < len; i++) {
ret[i] = resolve(ids[i], refUri)
}
return ret
}
var data = { id: ids, refUri: refUri, id2Uri: id2Uri }
var id = emitData("resolve", data, "id")
return data.uri || id2Uri(id, refUri)
}
function load(uris, callback, options) {
options = options || {}
var unloadedUris = options.filtered ? uris : getUnloadedUris(uris)
if (unloadedUris.length === 0) {
callback()
return
}
// Emit load event for plugins such as combo plugin
emit("load", unloadedUris)
var len = unloadedUris.length
var remain = len
for (var i = 0; i < len; i++) {
(function(uri) {
var mod = cachedModules[uri]
mod.status < STATUS.SAVED ? fetch(uri, onFetched) : onFetched()
function onFetched() {
// Maybe failed to fetch successfully, such as 404 error
// In these cases, just call `cb` function directly
if (mod.status < STATUS.SAVED) {
done()
return
}
// Break circular waiting callbacks
if (isCircularWaiting(mod)) {
printCircularLog(circularStack)
circularStack.length = 0
done()
return
}
// Load all unloaded dependencies
var waitings = mod.waitings = getUnloadedUris(mod.dependencies)
if (waitings.length === 0) {
done(mod)
return
}
// Copy waitings to prevent modification
load(waitings.slice(), function() {
done(mod)
}, { filtered: true })
}
})(unloadedUris[i])
}
function done(mod) {
if (mod && mod.status < STATUS.LOADED) {
mod.status = STATUS.LOADED
}
if (--remain === 0) {
callback()
}
}
}
var fetchingList = {}
var fetchedList = {}
var callbackList = {}
var anonymousModuleMeta = null
function fetch(uri, callback) {
cachedModules[uri].status = STATUS.FETCHING
// Emit `fetch` event. Plugins could use this event to
// modify uri or do other magic things
var requestUri = emitData("fetch",
{ uri: uri, fetchedList: fetchedList },
"requestUri") || uri
if (fetchedList[requestUri]) {
callback()
return
}
if (fetchingList[requestUri]) {
callbackList[requestUri].push(callback)
return
}
fetchingList[requestUri] = true
callbackList[requestUri] = [callback]
// Emit `request` event and send request
var charset = configData.charset
var data = {
uri: uri,
requestUri: requestUri,
callback: onRequested,
charset: charset
}
var requested = emitData("request", data, "requested")
if (!requested) {
request(data.requestUri, onRequested, charset)
}
function onRequested() {
delete fetchingList[requestUri]
fetchedList[requestUri] = true
// Save meta data of anonymous module
if (anonymousModuleMeta) {
save(uri, anonymousModuleMeta)
anonymousModuleMeta = null
}
// Call callbacks
var fn, fns = callbackList[requestUri]
delete callbackList[requestUri]
while ((fn = fns.shift())) fn()
}
}
function define(id, deps, factory) {
var argsLen = arguments.length
// HANDLE: define(factory)
if (argsLen === 1) {
factory = id
id = undefined
}
// HANDLE: define(id || deps, factory)
else if (argsLen === 2) {
factory = deps
deps = undefined
// HANDLE: define(deps, factory)
if (isArray(id)) {
deps = id
id = undefined
}
}
// Parse dependencies according to the module factory code
if (!isArray(deps) && isFunction(factory)) {
deps = parseDependencies(factory.toString())
}
var meta = { id: id, dependencies: deps, factory: factory }
var derivedUri
// Try to derive uri in IE6-9 for anonymous modules
if (!id && doc.attachEvent) {
var script = getCurrentScript()
if (script) {
derivedUri = script.src
}
else {
log("Failed to derive: " + factory)
// NOTE: If the id-deriving methods above is failed, then falls back
// to use onload event to get the uri
}
}
var resolvedUri = id ? resolve(id) : derivedUri
if (resolvedUri) {
save(resolvedUri, meta)
}
else {
// Save information for "memoizing" work in the script onload event
anonymousModuleMeta = meta
}
}
function save(uri, meta) {
meta.uri = uri
uri = emitData("save", meta, "uri")
var mod = getModule(uri)
// Do NOT override already saved modules
if (mod.status < STATUS.SAVED) {
// Let anonymous module id equal to its uri
mod.id = meta.id || uri
// Remove duplicated dependencies
mod.dependencies = resolve(meta.dependencies || [], uri)
mod.factory = meta.factory
mod.status = STATUS.SAVED
}
}
function compile(mod) {
// Return null when mod is invalid
if (!mod) {
return null
}
// When module is compiled, DO NOT compile it again. When module
// is being compiled, just return `module.exports` too, for avoiding
// circularly calling
if (mod.status >= STATUS.COMPILING) {
return mod.exports
}
emit("compile", mod)
// Just return `null` when:
// 1. the module file is 404
// 2. the module file is not written with valid module format
// 3. other error cases
if (mod.status < STATUS.LOADED && mod.exports === undefined) {
return null
}
mod.status = STATUS.COMPILING
function require(id) {
var child = cachedModules[require.resolve(id)]
// Return `null` when `uri` is invalid
if (child === undefined) {
return null
}
child.parent = mod
return compile(child)
}
require.async = function(ids, callback) {
mod.load(ids, callback)
return require
}
require.resolve = function(id) {
return resolve(id, mod.uri)
}
var factory = mod.factory
var exports = factory === undefined ? mod.exports : factory
if (isFunction(factory)) {
exports = factory(require, mod.exports = {}, mod)
}
mod.exports = exports === undefined ? mod.exports : exports
mod.status = STATUS.COMPILED
emit("compiled", mod)
return mod.exports
}
// Helpers
function getModule(uri, status) {
return cachedModules[uri] ||
(cachedModules[uri] = new Module(uri, status))
}
function getUnloadedUris(uris) {
var ret = []
for (var i = 0, len = uris.length; i < len; i++) {
var uri = uris[i]
if (uri && getModule(uri).status < STATUS.LOADED) {
ret.push(uri)
}
}
return ret
}
var circularStack = []
function isCircularWaiting(mod) {
var waitings = mod.waitings
if (waitings.length === 0) {
return false
}
circularStack.push(mod.uri)
if (isOverlap(waitings, circularStack)) {
cutWaitings(waitings)
return true
}
for (var i = 0; i < waitings.length; i++) {
if (isCircularWaiting(cachedModules[waitings[i]])) {
return true
}
}
circularStack.pop()
return false
}
function isOverlap(arrA, arrB) {
var arrC = arrA.concat(arrB)
return unique(arrC).length < arrC.length
}
function cutWaitings(waitings) {
var uri = circularStack[0]
for (var i = waitings.length - 1; i >= 0; i--) {
if (waitings[i] === uri) {
waitings.splice(i, 1)
break
}
}
}
function printCircularLog(stack) {
stack.push(stack[0])
log("Circular dependencies: " + stack.join(" --> "))
}
function preload(callback) {
var preloadMods = configData.preload
var len = preloadMods.length
if (len) {
// Use splice method to copy array and empty configData.preload
globalModule.load(preloadMods.splice(0, len), function() {
// Allow preload modules to add new preload modules
preload(callback)
})
}
else {
callback()
}
}
// Public API
var globalModule = new Module(pageUri, STATUS.COMPILED)
seajs.use = function(ids, callback) {
// Load preload modules before all other modules
preload(function() {
globalModule.load(ids, callback)
})
return seajs
}
global.define = define
/**
* config.js - The configuration for the loader
*/
var configData = config.data = {
// The root path to use for id2uri parsing
base: (function() {
var ret = loaderDir
// If loaderUri is `http://test.com/libs/seajs/seajs/1.0.0/sea.js`, the
// baseUri should be `http://test.com/libs/`
var m = ret.match(/^(.+?\/)(?:seajs\/)+\d[^/]+\/$/)
if (m) {
ret = m[1]
}
return ret
})(),
// The charset for requesting files
charset: "utf-8",
// Modules that are needed to load before all other modules
preload: []
// debug: false - Debug mode
// alias - The shorthand alias for module id
// vars - The {xxx} variables in module id
// map - An array containing rules to map module uri
// plugins - An array containing needed plugins
}
function config(data) {
for (var key in data) {
var curr = data[key]
if (hasOwn(data, key) && curr !== undefined) {
// Convert plugins to preload config
if (key === "plugins") {
key = "preload"
curr = plugin2preload(curr)
}
var prev = configData[key]
// For alias, vars
if (prev && /^(?:alias|vars)$/.test(key)) {
for (var k in curr) {
if (hasOwn(curr, k)) {
prev[k] = curr[k]
}
}