-
Notifications
You must be signed in to change notification settings - Fork 522
/
main.js
4931 lines (4197 loc) · 139 KB
/
main.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
(function(global){
// helpers
var camelize = function(str){
return str.replace(/-+(.)?/g, function(match, chr){
return chr ? chr.toUpperCase() : ''
});
},
each = function( o, cb){
var i, len;
// weak array detection, but we only use this internally so don't
// pass it weird stuff
if ( typeof o.length == 'number' && (o.length - 1) in o) {
for ( i = 0, len = o.length; i < len; i++ ) {
cb.call(o[i], o[i], i, o);
}
} else {
for ( i in o ) {
if(o.hasOwnProperty(i)){
cb.call(o[i], o[i], i, o);
}
}
}
return o;
},
map = function(o, cb) {
var arr = [];
each(o, function(item, i){
arr[i] = cb(item, i);
});
return arr;
},
isString = function(o) {
return typeof o == "string";
},
extend = function(d,s){
each(s, function(v, p){
d[p] = v;
});
return d;
},
dir = function(uri){
var lastSlash = uri.lastIndexOf("/");
//if no / slashes, check for \ slashes since it might be a windows path
if(lastSlash === -1)
lastSlash = uri.lastIndexOf("\\");
if(lastSlash !== -1) {
return uri.substr(0, lastSlash);
} else {
return uri;
}
},
last = function(arr){
return arr[arr.length - 1];
},
parseURI = function(url) {
var m = String(url).replace(/^\s+|\s+$/g, '').match(/^([^:\/?#]+:)?(\/\/(?:[^:@\/]*(?::[^:@\/]*)?@)?(([^:\/?#]*)(?::(\d*))?))?([^?#]*)(\?[^#]*)?(#[\s\S]*)?/);
// authority = '//' + user + ':' + pass '@' + hostname + ':' port
return (m ? {
href : m[0] || '',
protocol : m[1] || '',
authority: m[2] || '',
host : m[3] || '',
hostname : m[4] || '',
port : m[5] || '',
pathname : m[6] || '',
search : m[7] || '',
hash : m[8] || ''
} : null);
},
joinURIs = function(base, href) {
function removeDotSegments(input) {
var output = [];
input.replace(/^(\.\.?(\/|$))+/, '')
.replace(/\/(\.(\/|$))+/g, '/')
.replace(/\/\.\.$/, '/../')
.replace(/\/?[^\/]*/g, function (p) {
if (p === '/..') {
output.pop();
} else {
output.push(p);
}
});
return output.join('').replace(/^\//, input.charAt(0) === '/' ? '/' : '');
}
href = parseURI(href || '');
base = parseURI(base || '');
return !href || !base ? null : (href.protocol || base.protocol) +
(href.protocol || href.authority ? href.authority : base.authority) +
removeDotSegments(href.protocol || href.authority || href.pathname.charAt(0) === '/' ? href.pathname : (href.pathname ? ((base.authority && !base.pathname ? '/' : '') + base.pathname.slice(0, base.pathname.lastIndexOf('/') + 1) + href.pathname) : base.pathname)) +
(href.protocol || href.authority || href.pathname ? href.search : (href.search || base.search)) +
href.hash;
},
relativeURI = function(base, path) {
var uriParts = path.split("/"),
baseParts = base.split("/"),
result = [];
while ( uriParts.length && baseParts.length && uriParts[0] == baseParts[0] ) {
uriParts.shift();
baseParts.shift();
}
for(var i = 0 ; i< baseParts.length-1; i++) {
result.push("../");
}
return "./" + result.join("") + uriParts.join("/");
},
fBind = Function.prototype.bind,
isFunction = function(obj) {
return !!(obj && obj.constructor && obj.call && obj.apply);
},
isWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope,
isNode = typeof process === "object" && {}.toString.call(process) === "[object process]",
isBrowserWithWindow = !isNode && typeof window !== "undefined",
isNW = isNode && (function(){
try {
return require("nw.gui") !== "undefined";
} catch(e) {
return false;
}
})(),
isElectron = isNode && !!process.versions["electron"],
isNode = isNode && !isNW && !isElectron,
hasAWindow = isBrowserWithWindow || isNW || isElectron,
getStealScript = function(){
if(isBrowserWithWindow || isNW || isElectron) {
if(document.currentScript) {
return document.currentScript;
}
var scripts = document.scripts;
if (scripts.length) {
var currentScript = scripts[scripts.length - 1];
return currentScript;
}
}
},
stealScript = getStealScript(),
warn = typeof console === "object" ?
fBind.call(console.warn, console) : function(){};
var filename = function(uri){
var lastSlash = uri.lastIndexOf("/");
//if no / slashes, check for \ slashes since it might be a windows path
if(lastSlash === -1)
lastSlash = uri.lastIndexOf("\\");
var matches = ( lastSlash == -1 ? uri : uri.substr(lastSlash+1) ).match(/^[\w-\s\.!]+/);
return matches ? matches[0] : "";
};
var ext = function(uri){
var fn = filename(uri);
var dot = fn.lastIndexOf(".");
if(dot !== -1) {
return fn.substr(dot+1);
} else {
return "";
}
};
var pluginCache = {};
var normalize = function(unnormalizedName, loader){
var name = unnormalizedName;
// Detech if this name contains a plugin part like: app.less!steal/less
// and catch the plugin name so that when it is normalized we do not perform
// Steal's normalization against it.
var pluginIndex = name.lastIndexOf('!');
var pluginPart = "";
if (pluginIndex != -1) {
// argumentName is the part before the !
var argumentName = name.substr(0, pluginIndex);
var pluginName = name.substr(pluginIndex + 1);
pluginPart = "!" + pluginName;
// Set the name to the argument name so that we can normalize it alone.
name = argumentName;
}
var last = filename(name),
extension = ext(name);
// if the name ends with /
if( name[name.length -1] === "/" ) {
return name+filename( name.substr(0, name.length-1) ) + pluginPart;
} else if( !/^(\w+(?:s)?:\/\/|\.|file|\/)/.test(name) &&
// and doesn't end with a dot
last.indexOf(".") === -1
) {
return name+"/"+last + pluginPart;
} else {
if(extension === "js") {
return name.substr(0, name.lastIndexOf(".")) + pluginPart;
} else {
return name + pluginPart;
}
}
};
var cloneSteal = function(System){
var loader = System || this.System;
var steal = makeSteal(loader.clone());
steal.loader.set("@steal", steal.loader.newModule({
"default": steal,
__useDefault: true
}));
steal.clone = cloneSteal;
return steal;
};
var ArraySet;
if(typeof Set === "function") {
ArraySet = Set;
} else {
ArraySet = function(){ this._items = []; };
ArraySet.prototype.has = function(item) {
return this._items.indexOf(item) !== -1;
};
ArraySet.prototype.add = function(item) {
if(!this.has(item)) {
this._items.push(item);
}
};
}
var makeSteal = function(System){
var addStealExtension = function (extensionFn) {
if (typeof System !== "undefined" && isFunction(extensionFn)) {
if (System._extensions) {
System._extensions.push(extensionFn);
}
extensionFn(System, steal);
}
};
System.set('@loader', System.newModule({
'default': System,
__useDefault: true
}));
System.set("less", System.newModule({
__useDefault: true,
default: {
fetch: function() {
throw new Error(
[
"steal-less plugin must be installed and configured properly",
"See https://stealjs.com/docs/steal-less.html"
].join("\n")
);
}
}
}));
System.config({
map: {
"@loader/@loader": "@loader",
"@steal/@steal": "@steal"
}
});
var configPromise,
devPromise,
appPromise;
var steal = function(){
var args = arguments;
var afterConfig = function(){
var imports = [];
var factory;
each(args, function(arg){
if(isString(arg)) {
imports.push( steal.System['import']( normalize(arg) ) );
} else if(typeof arg === "function") {
factory = arg;
}
});
var modules = Promise.all(imports);
if(factory) {
return modules.then(function(modules) {
return factory && factory.apply(null, modules);
});
} else {
return modules;
}
};
if(System.isEnv("production")) {
return afterConfig();
} else {
// wait until the config has loaded
return configPromise.then(afterConfig,afterConfig);
}
};
System.set("@steal", System.newModule({
"default": steal,
__useDefault:true
}));
System.Set = ArraySet;
var loaderClone = System.clone;
System.clone = function(){
var loader = loaderClone.apply(this, arguments);
loader.set("@loader", loader.newModule({
"default": loader,
__useDefault: true
}));
loader.set("@steal", loader.newModule({
"default": steal,
__useDefault: true
}));
loader.Set = ArraySet;
return loader;
};
// steal.System remains for backwards compat only
steal.System = steal.loader = System;
steal.parseURI = parseURI;
steal.joinURIs = joinURIs;
steal.normalize = normalize;
steal.relativeURI = relativeURI;
steal.addExtension = addStealExtension;
// System-Ext
// This normalize-hook does 2 things.
// 1. with specify a extension in your config
// you can use the "!" (bang) operator to load
// that file with the extension
// System.ext = {bar: "path/to/bar"}
// foo.bar! -> foo.bar!path/to/bar
// 2. if you load a javascript file e.g. require("./foo.js")
// normalize will remove the ".js" to load the module
addStealExtension(function addExt(loader) {
loader.ext = {};
var normalize = loader.normalize,
endingExtension = /\.(\w+)!?$/;
loader.normalize = function (name, parentName, parentAddress, pluginNormalize) {
if (pluginNormalize) {
return normalize.apply(this, arguments);
}
var matches = name.match(endingExtension);
var outName = name;
if (matches) {
var hasBang = name[name.length - 1] === "!",
ext = matches[1];
// load js-files nodd-like
if (parentName && loader.configMain !== name && matches[0] === '.js') {
outName = name.substr(0, name.lastIndexOf("."));
// matches ext mapping
} else if (loader.ext[ext]) {
outName = name + (hasBang ? "" : "!") + loader.ext[ext];
}
}
return normalize.call(this, outName, parentName, parentAddress);
};
});
// Steal Locate Extension
// normalize a given path e.g.
// "path/to/folder/" -> "path/to/folder/folder"
addStealExtension(function addForwardSlash(loader) {
var normalize = loader.normalize;
var npmLike = /@.+#.+/;
loader.normalize = function (name, parentName, parentAddress, pluginNormalize) {
var lastPos = name.length - 1,
secondToLast,
folderName,
newName = name;
if (name[lastPos] === "/") {
secondToLast = name.substring(0, lastPos).lastIndexOf("/");
folderName = name.substring(secondToLast + 1, lastPos);
if (npmLike.test(folderName)) {
folderName = folderName.substr(folderName.lastIndexOf("#") + 1);
}
newName += folderName;
}
return normalize.call(this, newName, parentName, parentAddress, pluginNormalize);
};
});
// override loader.translate to rewrite 'locate://' & 'pkg://' path schemes found
// in resources loaded by supporting plugins
addStealExtension(function addLocateProtocol(loader) {
/**
* @hide
* @function normalizeAndLocate
* @description Run a module identifier through Normalize and Locate hooks.
* @param {String} moduleName The module to run through normalize and locate.
* @return {Promise} A promise to resolve when the address is found.
*/
var normalizeAndLocate = function(moduleName, parentName){
var loader = this;
return Promise.resolve(loader.normalize(moduleName, parentName))
.then(function(name){
return loader.locate({name: name, metadata: {}});
}).then(function(address){
var outAddress = address;
if(address.substr(address.length - 3) === ".js") {
outAddress = address.substr(0, address.length - 3);
}
return outAddress;
});
};
var relative = function(base, path){
var uriParts = path.split("/"),
baseParts = base.split("/"),
result = [];
while ( uriParts.length && baseParts.length && uriParts[0] == baseParts[0] ) {
uriParts.shift();
baseParts.shift();
}
for(var i = 0 ; i< baseParts.length-1; i++) {
result.push("../");
}
return result.join("") + uriParts.join("/");
};
var schemePattern = /(locate):\/\/([a-z0-9/._@-]*)/ig,
parsePathSchemes = function(source, parent) {
var locations = [];
source.replace(schemePattern, function(whole, scheme, path, index){
locations.push({
start: index,
end: index+whole.length,
name: path,
postLocate: function(address){
return relative(parent, address);
}
});
});
return locations;
};
var _translate = loader.translate;
loader.translate = function(load){
var loader = this;
// This only applies to plugin resources.
if(!load.metadata.plugin) {
return _translate.call(this, load);
}
// Use the translator if this file path scheme is supported by the plugin
var locateSupport = load.metadata.plugin.locateScheme;
if(!locateSupport) {
return _translate.call(this, load);
}
// Parse array of module names
var locations = parsePathSchemes(load.source, load.address);
// no locations found
if(!locations.length) {
return _translate.call(this, load);
}
// normalize and locate all of the modules found and then replace those instances in the source.
var promises = [];
for(var i = 0, len = locations.length; i < len; i++) {
promises.push(
normalizeAndLocate.call(this, locations[i].name, load.name)
);
}
return Promise.all(promises).then(function(addresses){
for(var i = locations.length - 1; i >= 0; i--) {
load.source = load.source.substr(0, locations[i].start)
+ locations[i].postLocate(addresses[i])
+ load.source.substr(locations[i].end, load.source.length);
}
return _translate.call(loader, load);
});
};
});
addStealExtension(function addContextual(loader) {
loader._contextualModules = {};
loader.setContextual = function(moduleName, definer){
this._contextualModules[moduleName] = definer;
};
var normalize = loader.normalize;
loader.normalize = function(name, parentName){
var loader = this;
var pluginLoader = loader.pluginLoader || loader;
if (parentName) {
var definer = this._contextualModules[name];
// See if `name` is a contextual module
if (definer) {
var localName = name + '/' + parentName;
if(!loader.has(localName)) {
// `definer` could be a function or could be a moduleName
if (typeof definer === 'string') {
definer = pluginLoader['import'](definer);
}
return Promise.resolve(definer)
.then(function(modDefiner) {
var definer = modDefiner;
if (definer['default']) {
definer = definer['default'];
}
var definePromise = Promise.resolve(
definer.call(loader, parentName)
);
return definePromise;
})
.then(function(moduleDef){
loader.set(localName, loader.newModule(moduleDef));
return localName;
});
}
return Promise.resolve(localName);
}
}
return normalize.apply(this, arguments);
};
});
/**
* Steal Script-Module Extension
*
* Add a steal-module script to the page and it will run after Steal has been
* configured, e.g:
*
* <script type="text/steal-module">...</script>
* <script type="steal-module">...</script>
*/
addStealExtension(function addStealModule(loader) {
// taken from https://github.com/ModuleLoader/es6-module-loader/blob/master/src/module-tag.js
function completed() {
document.removeEventListener("DOMContentLoaded", completed, false);
window.removeEventListener("load", completed, false);
ready();
}
function ready() {
var scripts = document.getElementsByTagName("script");
for (var i = 0; i < scripts.length; i++) {
var script = scripts[i];
if (script.type == "steal-module" || script.type == "text/steal-module") {
var source = script.innerHTML;
if (/\S/.test(source)) {
loader.module(source)["catch"](function(err) {
setTimeout(function() {
throw err;
});
});
}
}
}
}
loader.loadScriptModules = function() {
if (isBrowserWithWindow) {
if (document.readyState === "complete") {
setTimeout(ready);
} else if (document.addEventListener) {
document.addEventListener("DOMContentLoaded", completed, false);
window.addEventListener("load", completed, false);
}
}
};
});
// SystemJS Steal Format
// Provides the Steal module format definition.
addStealExtension(function addStealFormat(loader) {
// Steal Module Format Detection RegEx
// steal(module, ...)
var stealRegEx = /(?:^\s*|[}{\(\);,\n\?\&]\s*)steal\s*\(\s*((?:"[^"]+"\s*,|'[^']+'\s*,\s*)*)/;
// What we stole.
var stealInstantiateResult;
function createSteal(loader) {
stealInstantiateResult = null;
// ensure no NodeJS environment detection
loader.global.module = undefined;
loader.global.exports = undefined;
function steal() {
var deps = [];
var factory;
for( var i = 0; i < arguments.length; i++ ) {
if (typeof arguments[i] === 'string') {
deps.push( normalize(arguments[i]) );
} else {
factory = arguments[i];
}
}
if (typeof factory !== 'function') {
factory = (function(factory) {
return function() { return factory; };
})(factory);
}
stealInstantiateResult = {
deps: deps,
execute: function(require, exports, moduleName) {
var depValues = [];
for (var i = 0; i < deps.length; i++) {
depValues.push(require(deps[i]));
}
var output = factory.apply(loader.global, depValues);
if (typeof output !== 'undefined') {
return output;
}
}
};
}
loader.global.steal = steal;
}
var loaderInstantiate = loader.instantiate;
loader.instantiate = function(load) {
var loader = this;
if (load.metadata.format === 'steal' || !load.metadata.format && load.source.match(stealRegEx)) {
load.metadata.format = 'steal';
var oldSteal = loader.global.steal;
createSteal(loader);
loader.__exec(load);
loader.global.steal = oldSteal;
if (!stealInstantiateResult) {
throw "Steal module " + load.name + " did not call steal";
}
if (stealInstantiateResult) {
load.metadata.deps = load.metadata.deps ? load.metadata.deps.concat(stealInstantiateResult.deps) : stealInstantiateResult.deps;
load.metadata.execute = stealInstantiateResult.execute;
}
}
return loaderInstantiate.call(loader, load);
};
});
addStealExtension(function addMetaDeps(loader) {
var superTranspile = loader.transpile;
var superDetermineFormat = loader._determineFormat;
function prependDeps (loader, load, callback) {
var meta = loader.meta[load.name];
if (meta && meta.deps && meta.deps.length) {
var imports = meta.deps.map(callback).join('\n');
load.source = imports + "\n" + load.source;
}
}
function createImport(dep) {
return "import \"" + dep + "\";";
}
function createRequire(dep) {
return "require(\"" + dep + "\");";
}
loader.transpile = function (load) {
prependDeps(this, load, createImport);
var result = superTranspile.apply(this, arguments);
return result;
}
loader._determineFormat = function (load) {
if(load.metadata.format === 'cjs') {
prependDeps(this, load, createRequire);
}
var result = superDetermineFormat.apply(this, arguments);
return result;
};
});
addStealExtension(function addStackTrace(loader) {
function StackTrace(message, items) {
this.message = message;
this.items = items;
}
StackTrace.prototype.toString = function(){
var arr = ["Error: " + this.message];
var t, desc;
for(var i = 0, len = this.items.length; i < len; i++) {
t = this.items[i];
desc = " at ";
if(t.fnName) {
desc += (t.fnName + " ");
}
desc += StackTrace.positionLink(t);
arr.push(desc);
}
return arr.join("\n");
};
StackTrace.positionLink = function(t){
var line = t.line || 0;
var col = t.column || 0;
return "(" + t.url + ":" + line + ":" + col + ")";
};
StackTrace.item = function(fnName, url, line, column) {
return {
method: fnName,
fnName: fnName,
url: url,
line: line,
column: column
}
};
function parse(stack) {
var rawLines = stack.split('\n');
var v8Lines = compact(rawLines.map(parseV8Line));
if (v8Lines.length > 0) return v8Lines;
var geckoLines = compact(rawLines.map(parseGeckoLine));
if (geckoLines.length > 0) return geckoLines;
throw new Error('Unknown stack format: ' + stack);
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Stack
var GECKO_LINE = /^(?:([^@]*)@)?(.*?):(\d+)(?::(\d+))?$/;
function parseGeckoLine(line) {
var match = line.match(GECKO_LINE);
if (!match) return null;
var meth = match[1] || ''
return {
method: meth,
fnName: meth,
url: match[2] || '',
line: parseInt(match[3]) || 0,
column: parseInt(match[4]) || 0,
};
}
// https://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
var V8_OUTER1 = /^\s*(eval )?at (.*) \((.*)\)$/;
var V8_OUTER2 = /^\s*at()() (\S+)$/;
var V8_INNER = /^\(?([^\(]+):(\d+):(\d+)\)?$/;
function parseV8Line(line) {
var outer = line.match(V8_OUTER1) || line.match(V8_OUTER2);
if (!outer) return null;
var inner = outer[3].match(V8_INNER);
if (!inner) return null;
var method = outer[2] || '';
if (outer[1]) method = 'eval at ' + method;
return {
method: method,
fnName: method,
url: inner[1] || '',
line: parseInt(inner[2]) || 0,
column: parseInt(inner[3]) || 0,
};
}
// Helpers
function compact(array) {
var result = [];
array.forEach(function(value) {
if (value) {
result.push(value);
}
});
return result;
}
StackTrace.parse = function(error) {
try {
var lines = parse(error.stack || error);
if(lines.length) {
return new StackTrace(error.message, lines);
}
} catch(e) {
return undefined;
}
};
loader.StackTrace = StackTrace;
function getPositionOfError(txt) {
var res = /at position ([0-9]+)/.exec(txt);
if(res && res.length > 1) {
return Number(res[1]);
}
}
loader.loadCodeFrame = function(){
if(!this.global.process) {
this.global.process = { argv: '', env: {} };
}
var loader = this.pluginLoader || this;
var isProd = loader.isEnv("production");
var p = isProd ? Promise.resolve() : loader["import"]("@@babel-code-frame");
return p;
};
loader._parseJSONError = function(err, source){
var pos = getPositionOfError(err.message);
if(pos) {
return this._getLineAndColumnFromPosition(source, pos);
} else {
return {line: 0, column: 0};
}
};
var errPos = /at position( |:)([0-9]+)/;
var errLine = /at line ([0-9]+) column ([0-9]+)/;
loader._parseSyntaxErrorLocation = function(error, load){
// V8 and Edge
var res = errPos.exec(error.message);
if(res && res.length === 3) {
var pos = Number(res[2]);
return this._getLineAndColumnFromPosition(load.source, pos);
}
// Firefox
res = errLine.exec(error.message);
if(res && res.length === 3) {
return {
line: Number(res[1]),
column: Number(res[2])
};
}
}
loader._addSourceInfoToError = function(err, pos, load, fnName){
return this.loadCodeFrame()
.then(function(codeFrame){
if(codeFrame) {
var src = load.metadata.originalSource || load.source;
var codeSample = codeFrame(src, pos.line, pos.column);
err.message += "\n\n" + codeSample + "\n";
}
var stackTrace = new StackTrace(err.message, [
StackTrace.item(fnName, load.address, pos.line, pos.column)
]);
err.stack = stackTrace.toString();
return Promise.reject(err);
});
};
function findStackFromAddress(st, address) {
for(var i = 0; i < st.items.length; i++) {
if(st.items[i].url === address) {
return st.items[i];
}
}
}
loader.rejectWithCodeFrame = function(error, load) {
var st = StackTrace.parse(error);
var item;
if(error.onlyIncludeCodeFrameIfRootModule) {
item = st && st.items[0] && st.items[0].url === load.address && st.items[0];
} else {
item = findStackFromAddress(st, load.address);
}
if(item) {
return this.loadCodeFrame()
.then(function(codeFrame){
if(codeFrame) {
var newError = new Error(error.message);
var line = item.line;
var column = item.column;
// CommonJS adds 3 function wrappers
if(load.metadata.format === "cjs") {
line = line - 3;
}
var src = load.metadata.originalSource || load.source;
var codeSample = codeFrame(src, line, column);
if(!codeSample) return Promise.reject(error);
newError.message += "\n\n" + codeSample + "\n";
st.message = newError.message;
newError.stack = st.toString();
return Promise.reject(newError);
} else {
return Promise.reject(error);
}
});
}
return Promise.reject(error);
};
});
addStealExtension(function addPrettyName(loader){
loader.prettyName = function(load){
var pnm = load.metadata.parsedModuleName;
if(pnm) {
return pnm.packageName + "/" + pnm.modulePath;
}
return load.name;
};
});
addStealExtension(function addTreeShaking(loader) {
function treeShakingEnabled(loader, load) {
return !loader.noTreeShaking && loader.treeShaking !== false;
}
function determineUsedExports(load) {
var loader = this;
// 1. Get any new dependencies that haven't been accounted for.
var newDeps = newDependants.call(this, load);
var usedExports = new loader.Set();
var allUsed = false;
newDeps.forEach(function(depName) {
var depLoad = loader.getModuleLoad(depName);
var specifier = loader.moduleSpecifierFromName(depLoad, load.name);
if (depLoad.metadata.format !== "es6") {
allUsed = true;
return;
}
});
// Only walk the export tree if all are not being used.
// This saves not needing to do the traversal.
if(!allUsed) {
allUsed = walkExports.call(loader, load, function(exps){
exps.forEach(function(name){
usedExports.add(name);
});
});
}
// Copy over existing exports
if(load.metadata.usedExports) {
load.metadata.usedExports.forEach(function(name){
usedExports.add(name);
});
}
if(!loader.treeShakeConfig[load.name]) {
loader.treeShakeConfig[load.name] = Object.create(null);
}
load.metadata.usedExports = loader.treeShakeConfig[load.name].usedExports = usedExports;
load.metadata.allExportsUsed = loader.treeShakeConfig[load.name].allExportsUsed = allUsed;
return {
all: allUsed,
used: usedExports
};
}
// Determine if this load's dependants have changed,
function newDependants(load) {
var out = [];
var deps = this.getDependants(load.name);
var shakenParents = load.metadata.shakenParents;