/
sea-debug.js
1429 lines (1098 loc) · 30.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 - A Module Loader for the Web
* v1.2.0 | seajs.org | MIT Licensed
*/
/**
* Base namespace for the framework.
*/
this.seajs = { _seajs: this.seajs }
/**
* The version of the framework. It will be replaced with "major.minor.patch"
* when building.
*/
seajs.version = '1.2.0'
/**
* The private utilities. Internal use only.
*/
seajs._util = {}
/**
* The private configuration data. Internal use only.
*/
seajs._config = {
/**
* Debug mode. It will be turned off automatically when compressing.
*/
debug: '%DEBUG%',
/**
* Modules that are needed to load before all other modules.
*/
preload: []
}
/**
* The minimal language enhancement
*/
;(function(util) {
var toString = Object.prototype.toString
var AP = Array.prototype
util.isString = function(val) {
return toString.call(val) === '[object String]'
}
util.isFunction = function(val) {
return toString.call(val) === '[object Function]'
}
util.isRegExp = function(val) {
return toString.call(val) === '[object RegExp]'
}
util.isObject = function(val) {
return val === Object(val)
}
util.isArray = Array.isArray || function(val) {
return toString.call(val) === '[object Array]'
}
util.indexOf = AP.indexOf ?
function(arr, item) {
return arr.indexOf(item)
} :
function(arr, item) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] === item) {
return i
}
}
return -1
}
var forEach = util.forEach = AP.forEach ?
function(arr, fn) {
arr.forEach(fn)
} :
function(arr, fn) {
for (var i = 0; i < arr.length; i++) {
fn(arr[i], i, arr)
}
}
util.map = AP.map ?
function(arr, fn) {
return arr.map(fn)
} :
function(arr, fn) {
var ret = []
forEach(arr, function(item, i, arr) {
ret.push(fn(item, i, arr))
})
return ret
}
util.filter = AP.filter ?
function(arr, fn) {
return arr.filter(fn)
} :
function(arr, fn) {
var ret = []
forEach(arr, function(item, i, arr) {
if (fn(item, i, arr)) {
ret.push(item)
}
})
return ret
}
util.unique = function(arr) {
var ret = []
var o = {}
forEach(arr, function(item) {
o[item] = 1
})
if (Object.keys) {
ret = Object.keys(o)
}
else {
for (var p in o) {
if (o.hasOwnProperty(p)) {
ret.push(p)
}
}
}
return ret
}
util.keys = Object.keys
if (!util.keys) {
util.keys = function(o) {
var ret = []
for (var p in o) {
if (o.hasOwnProperty(p)) {
ret.push(p)
}
}
return ret
}
}
util.now = Date.now || function() {
return new Date().getTime()
}
})(seajs._util)
/**
* The tiny console support
*/
;(function(util, config) {
var AP = Array.prototype
/**
* The safe wrapper of console.log/error/...
*/
util.log = function() {
if (typeof console !== 'undefined') {
var args = AP.slice.call(arguments)
var type = 'log'
var last = args[args.length - 1]
console[last] && (type = args.pop())
// Only show log info in debug mode
if (type === 'log' && !config.debug) return
var out = type === 'dir' ? args[0] : AP.join.call(args, ' ')
console[type](out)
}
}
})(seajs._util, seajs._config)
/**
* Path utilities for the framework
*/
;(function(util, config, global) {
var DIRNAME_RE = /.*(?=\/.*$)/
var MULTIPLE_SLASH_RE = /([^:\/])\/\/+/g
var FILE_EXT_RE = /\.(?:css|js)$/
var ROOT_RE = /^(.*?\w)(?:\/|$)/
/**
* Extracts the directory portion of a path.
* dirname('a/b/c.js') ==> 'a/b/'
* dirname('d.js') ==> './'
* @see http://jsperf.com/regex-vs-split/2
*/
function dirname(path) {
var s = path.match(DIRNAME_RE)
return (s ? s[0] : '.') + '/'
}
/**
* Canonicalizes a path.
* realpath('./a//b/../c') ==> 'a/c'
*/
function realpath(path) {
MULTIPLE_SLASH_RE.lastIndex = 0
// 'file:///a//b/c' ==> 'file:///a/b/c'
// 'http://a//b/c' ==> 'http://a/b/c'
if (MULTIPLE_SLASH_RE.test(path)) {
path = path.replace(MULTIPLE_SLASH_RE, '$1\/')
}
// 'a/b/c', just return.
if (path.indexOf('.') === -1) {
return path
}
var original = path.split('/')
var ret = [], part
for (var i = 0; i < original.length; i++) {
part = original[i]
if (part === '..') {
if (ret.length === 0) {
throw new Error('The path is invalid: ' + path)
}
ret.pop()
}
else if (part !== '.') {
ret.push(part)
}
}
return ret.join('/')
}
/**
* Normalizes an uri.
*/
function normalize(uri) {
uri = realpath(uri)
var lastChar = uri.charAt(uri.length - 1)
if (lastChar === '/') {
return uri
}
// Adds the default '.js' extension except that the uri ends with #.
// ref: http://jsperf.com/get-the-last-character
if (lastChar === '#') {
uri = uri.slice(0, -1)
}
else if (uri.indexOf('?') === -1 && !FILE_EXT_RE.test(uri)) {
uri += '.js'
}
return uri
}
/**
* Parses alias in the module id. Only parse the first part.
*/
function parseAlias(id) {
// #xxx means xxx is already alias-parsed.
if (id.charAt(0) === '#') {
return id.substring(1)
}
var alias = config.alias
// Only top-level id needs to parse alias.
if (alias && isTopLevel(id)) {
var parts = id.split('/')
var first = parts[0]
if (alias.hasOwnProperty(first)) {
parts[0] = alias[first]
id = parts.join('/')
}
}
return id
}
var mapCache = {}
/**
* Converts the ur according to the map rules.
*/
function parseMap(uri, map) {
// map: [[match, replace], ...]
map || (map = config.map || [])
if (!map.length) return uri
var ret = uri
// Apply all matched rules in sequence.
for (var i = 0; i < map.length; i++) {
var rule = map[i]
if (util.isArray(rule) && rule.length === 2) {
var m = rule[0]
if (util.isString(m) && ret.indexOf(m) > -1 ||
util.isRegExp(m) && m.test(ret)) {
ret = ret.replace(m, rule[1])
}
}
else if (util.isFunction(rule)) {
ret = rule(ret)
}
}
if (ret !== uri) {
mapCache[ret] = uri
}
return ret
}
/**
* Gets the original uri.
*/
function unParseMap(uri) {
return mapCache[uri] || uri
}
/**
* Converts id to uri.
*/
function id2Uri(id, refUri) {
id = parseAlias(id)
refUri || (refUri = pageUri)
var ret
// absolute id
if (isAbsolute(id)) {
ret = id
}
// relative id
else if (isRelative(id)) {
// Converts './a' to 'a', to avoid unnecessary loop in realpath.
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 = config.base + '/' + id
}
return normalize(ret)
}
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 !== '/'
}
/**
* Normalizes pathname to start with '/'
* Ref: https://groups.google.com/forum/#!topic/seajs/9R29Inqk1UU
*/
function normalizePathname(pathname) {
if (pathname.charAt(0) !== '/') {
pathname = '/' + pathname
}
return pathname
}
var loc = global['location']
var pageUri = loc.protocol + '//' + loc.host +
normalizePathname(loc.pathname)
// local file in IE: C:\path\to\xx.js
if (pageUri.indexOf('\\') > 0) {
pageUri = pageUri.replace(/\\/g, '/')
}
util.dirname = dirname
util.realpath = realpath
util.normalize = normalize
util.parseAlias = parseAlias
util.parseMap = parseMap
util.unParseMap = unParseMap
util.id2Uri = id2Uri
util.isAbsolute = isAbsolute
util.isTopLevel = isTopLevel
util.pageUri = pageUri
})(seajs._util, seajs._config, this)
/**
* Utilities for fetching js and css files.
*/
;(function(util, config) {
var doc = document
var head = doc.head ||
doc.getElementsByTagName('head')[0] ||
doc.documentElement
var baseElement = head.getElementsByTagName('base')[0]
var UA = navigator.userAgent
var isSafari = UA.indexOf('Safari') > 0 && UA.indexOf('Chrome') === -1
var isFirefox = UA.indexOf('Firefox') > 0
var IS_CSS_RE = /\.css(?:\?|$)/i
var READY_STATE_RE = /loaded|complete|undefined/
var currentlyAddingScript
var interactiveScript
util.fetch = function(url, callback, charset) {
var isCSS = IS_CSS_RE.test(url)
var node = document.createElement(isCSS ? 'link' : 'script')
if (charset) {
var cs = util.isFunction(charset) ? charset(url) : charset
cs && (node.charset = cs)
}
assetOnload(node, callback || noop)
if (isCSS) {
node.rel = 'stylesheet'
node.href = url
}
else {
node.async = 'async'
node.src = url
}
// For some cache cases in IE 6-9, the script executes IMMEDIATELY after
// the end of the insertBefore execution, so use `currentlyAddingScript`
// to hold current node, for deriving url in `define`.
currentlyAddingScript = node
// ref: #185 & http://dev.jquery.com/ticket/2709
baseElement ?
head.insertBefore(node, baseElement) :
head.appendChild(node)
currentlyAddingScript = null
}
function assetOnload(node, callback) {
if (node.nodeName === 'SCRIPT') {
scriptOnload(node, callback)
} else {
styleOnload(node, callback)
}
}
function scriptOnload(node, callback) {
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 (node.parentNode && !config.debug) {
head.removeChild(node)
}
// Dereference the node
node = undefined
callback()
}
}
}
function styleOnload(node, callback) {
// for Safari and Old Firefox
if (isSafari || (isFirefox && !('onload' in node))) {
util.log('Start poll to fetch css')
setTimeout(function() {
poll(node, callback)
}, 1) // Begin after node insertion
}
else {
node.onload = node.onerror = function() {
node.onload = node.onerror = null
node = undefined
callback()
}
}
}
function poll(node, callback) {
var isLoaded
if (isSafari) {
if (node['sheet']) {
isLoaded = true
}
}
// for Firefox
else if (node['sheet']) {
try {
if (node['sheet'].cssRules) {
isLoaded = true
}
} catch (ex) {
if (ex.name === 'SecurityError' || // firefox >= 13.0
ex.name === 'NS_ERROR_DOM_SECURITY_ERR') { // old firefox
isLoaded = true
}
}
}
setTimeout(function() {
if (isLoaded) {
// Place callback in here due to giving time for style rendering.
callback()
} else {
poll(node, callback)
}
}, 1)
}
function noop() {
}
util.getCurrentScript = function() {
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 = 0; i < scripts.length; i++) {
var script = scripts[i]
if (script.readyState === 'interactive') {
interactiveScript = script
return script
}
}
}
util.getScriptAbsoluteSrc = function(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.importStyle = function(cssText, id) {
// Don't add multi times
if (id && doc.getElementById(id)) return
var element = doc.createElement('style')
id && (element.id = id)
// Adds to DOM first to avoid the css hack invalid
head.appendChild(element)
// IE
if (element.styleSheet) {
element.styleSheet.cssText = cssText
}
// W3C
else {
element.appendChild(doc.createTextNode(cssText))
}
}
/**
* References:
* - http://unixpapa.com/js/dyna.html
* - ../test/research/load-js-css/test.html
* - ../test/issues/load-css/test.html
* - http://www.blaze.io/technical/ies-premature-execution-problem/
*/
})(seajs._util, seajs._config, this)
/**
* The parser for dependencies
*/
;(function(util) {
var REQUIRE_RE = /(?:^|[^.$])\brequire\s*\(\s*(["'])([^"'\s\)]+)\1\s*\)/g
util.parseDependencies = function(code) {
// Parse these `requires`:
// var a = require('a');
// someMethod(require('b'));
// require('c');
// ...
// Doesn't parse:
// someInstance.require(...);
var ret = [], match
code = removeComments(code)
REQUIRE_RE.lastIndex = 0
while ((match = REQUIRE_RE.exec(code))) {
if (match[2]) {
ret.push(match[2])
}
}
return util.unique(ret)
}
// See: research/remove-comments-safely
function removeComments(code) {
return code
.replace(/^\s*\/\*[\s\S]*?\*\/\s*$/mg, '') // block comments
.replace(/^\s*\/\/.*$/mg, '') // line comments
}
})(seajs._util)
/**
* The Module constructor and its methods
*/
;(function(seajs, util, config) {
var cachedModules = {}
var cachedModifiers = {}
var compileStack = []
var STATUS = {
'FETCHING': 1, // The module file is fetching now.
'FETCHED': 2, // The module file has been fetched.
'SAVED': 3, // The module info has been saved.
'READY': 4, // All dependencies and self are ready to compile.
'COMPILED': 5 // The module.exports is available.
}
function Module(uri, status) {
this.uri = uri
this.status = status || 0
// this.id is set when saving
// this.factory is set when saving
// this.uri is set when saving
// this.exports is set when compiling
// this.parent is set when compiling
}
Module.prototype._use = function(ids, callback) {
util.isString(ids) && (ids = [ids])
var uris = resolve(ids, this.uri)
this._load(uris, function() {
var args = util.map(uris, function(uri) {
return cachedModules[uri]._compile()
})
if (callback) {
callback.apply(null, args)
}
})
}
Module.prototype._load = function(uris, callback) {
var unLoadedUris = util.filter(uris, function(uri) {
return uri && (!cachedModules[uri] ||
cachedModules[uri].status < STATUS.READY)
})
var length = unLoadedUris.length
if (length === 0) {
callback()
return
}
var remain = length
for (var i = 0; i < length; i++) {
(function(uri) {
var module = cachedModules[uri] ||
(cachedModules[uri] = new Module(uri, STATUS.FETCHING))
module.status >= STATUS.FETCHED ? onFetched() : fetch(uri, onFetched)
function onFetched() {
// cachedModules[uri] is changed in un-correspondence case
module = cachedModules[uri]
if (module.status >= STATUS.SAVED) {
var deps = getPureDependencies(module)
if (deps.length) {
Module.prototype._load(deps, function() {
cb(module)
})
}
else {
cb(module)
}
}
// Maybe failed to fetch successfully, such as 404 or non-module.
// In these cases, module.status stay at FETCHING or FETCHED.
else {
cb()
}
}
})(unLoadedUris[i])
}
function cb(module) {
module && (module.status = STATUS.READY)
--remain === 0 && callback()
}
}
Module.prototype._compile = function() {
var module = this
if (module.status === STATUS.COMPILED) {
return module.exports
}
// 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 (module.status < STATUS.READY) {
return null
}
function require(id) {
var uri = resolve(id, module.uri)
var child = cachedModules[uri]
// Just return null when uri is invalid.
if (!child) {
return null
}
if (isCircular(child)) {
return child.exports
}
child.parent = module
return child._compile()
}
require.async = function(ids, callback) {
module._use(ids, callback)
}
require.resolve = function(id) {
return resolve(id, module.uri)
}
require.cache = cachedModules
module.require = require
module.exports = {}
var factory = module.factory
if (util.isFunction(factory)) {
compileStack.push(module)
runInModuleContext(factory, module)
compileStack.pop()
}
else if (factory !== undefined) {
module.exports = factory
}
module.status = STATUS.COMPILED
execModifiers(module)
return module.exports
}
Module._define = function(id, deps, factory) {
var argsLength = arguments.length
// define(factory)
if (argsLength === 1) {
factory = id
id = undefined
}
// define(id || deps, factory)
else if (argsLength === 2) {
factory = deps
deps = undefined
// define(deps, factory)
if (util.isArray(id)) {
deps = id
id = undefined
}
}
// Parses dependencies.
if (!util.isArray(deps) && util.isFunction(factory)) {
deps = util.parseDependencies(factory.toString())
}
// Gets uri directly for specific modules.
if (id) {
var uri = resolve(id)
}
// Try to derive uri in IE6-9 for anonymous modules.
else if (document.attachEvent) {
// Try to get the current script.
var script = util.getCurrentScript()
if (script) {
uri = util.unParseMap(util.getScriptAbsoluteSrc(script))
}
if (!uri) {
util.log('Failed to derive URI from interactive script for:',
factory.toString(), 'warn')
// NOTE: If the id-deriving methods above is failed, then falls back
// to use onload event to get the uri.
}
}
var meta = { id: id, dependencies: deps, factory: factory }
if (uri) {
currentPackageModules.push(save(uri, meta))
}
else {
// Saves information for "memoizing" work in the onload event.
anonymousModuleMeta = meta
}
}
Module._getCompilingModule = function() {
return compileStack[compileStack.length - 1]
}
Module._find = function(selector) {
var matches = []
util.forEach(util.keys(cachedModules), function(uri) {
if (util.isString(selector) && uri.indexOf(selector) > -1 ||
util.isRegExp(selector) && selector.test(uri)) {
var module = cachedModules[uri]
module.exports && matches.push(module.exports)
}
})
var length = matches.length
if (length === 1) {
matches = matches[0]
}
else if (length === 0) {
matches = null
}
return matches
}
Module._modify = function(id, modifier) {
var uri = resolve(id)
var module = cachedModules[uri]
if (module && module.status === STATUS.COMPILED) {
runInModuleContext(modifier, module)
}
else {
cachedModifiers[uri] || (cachedModifiers[uri] = [])
cachedModifiers[uri].push(modifier)
}
return seajs
}
// For plugin developers
Module.STATUS = STATUS
Module._resolve = util.id2Uri
Module._fetch = util.fetch
Module.cache = cachedModules
// Helpers
// -------
var fetchingList = {}
var fetchedList = {}
var callbackList = {}
var anonymousModuleMeta = null
var currentPackageModules = []
var circularCheckStack = []
function resolve(ids, refUri) {
if (util.isString(ids)) {
return Module._resolve(ids, refUri)
}
return util.map(ids, function(id) {
return resolve(id, refUri)
})
}
function fetch(uri, callback) {
var requestUri = util.parseMap(uri)
if (fetchedList[requestUri]) {
callback()
return
}