-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathboot.js
1181 lines (1023 loc) · 36.3 KB
/
boot.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
// @generated
/*eslint semi:[0], no-native-reassign:[0]*/
global = this;
(function (modules) {
// Bundle allows the run-time to extract already-loaded modules from the
// boot bundle.
var bundle = {};
var main;
// Unpack module tuples into module objects.
for (var i = 0; i < modules.length; i++) {
var module = modules[i];
module = modules[i] = new Module(
module[0],
module[1],
module[2],
module[3],
module[4]
);
bundle[module.filename] = module;
}
function Module(id, dirname, basename, dependencies, factory) {
this.id = id;
this.dirname = dirname;
this.filename = dirname + "/" + basename;
// Dependency map and factory are used to instantiate bundled modules.
this.dependencies = dependencies;
this.factory = factory;
}
Module.prototype._require = function () {
var module = this;
if (module.exports === void 0) {
module.exports = {};
var require = function (id) {
var index = module.dependencies[id];
var dependency = modules[index];
if (!dependency)
throw new Error("Bundle is missing a dependency: " + id);
return dependency._require();
};
require.main = main;
module.exports = module.factory(
require,
module.exports,
module,
module.filename,
module.dirname
) || module.exports;
}
return module.exports;
};
// Communicate the bundle to all bundled modules
Module.prototype.modules = bundle;
return function require(filename) {
main = bundle[filename];
main._require();
}
})([["boot-entry.js","system","boot-entry.js",{"./system":1,"./url":2,"./script-params":9},function (require, exports, module, __filename, __dirname){
// system/boot-entry.js
// --------------------
/*eslint-env browser*/
"use strict";
var System = require("./system");
var URL = require("./url");
var getParams = require("./script-params");
module.exports = boot;
function boot(params) {
params = params || getParams("boot.js");
var moduleLocation = URL.resolve(window.location, ".");
var systemLocation = URL.resolve(window.location, params.package || ".");
var abs = "";
if (moduleLocation.lastIndexOf(systemLocation, 0) === 0) {
abs = moduleLocation.slice(systemLocation.length);
}
var rel = params.import || "";
return System.load(systemLocation, {
browser: true
}).then(function onSystemLoaded(system) {
window.system = system;
return system.import(rel, abs);
});
}
if (require.main === module) {
boot();
}
}],["browser-system.js","system","browser-system.js",{"./common-system":3},function (require, exports, module, __filename, __dirname){
// system/browser-system.js
// ------------------------
/*eslint-env browser*/
"use strict";
var CommonSystem = require("./common-system");
module.exports = BrowserSystem;
function BrowserSystem(location, description, options) {
var self = this;
CommonSystem.call(self, location, description, options);
}
BrowserSystem.prototype = Object.create(CommonSystem.prototype);
BrowserSystem.prototype.constructor = BrowserSystem;
BrowserSystem.load = CommonSystem.load;
BrowserSystem.prototype.read = function read(location, charset, contentType) {
return new Promise(function (resolve, reject) {
var request = new XMLHttpRequest();
function onload() {
if (xhrSuccess(request)) {
resolve(request.responseText);
} else {
onerror();
}
}
function onerror() {
var error = new Error("Can't XHR " + JSON.stringify(location));
if (request.status === 404 || request.status === 0) {
error.code = "ENOENT";
error.notFound = true;
}
reject(error);
}
try {
request.open("GET", location, true);
if (contentType && request.overrideMimeType) {
request.overrideMimeType(contentType);
}
request.onreadystatechange = function () {
if (request.readyState === 4) {
onload();
}
};
request.onload = request.load = onload;
request.onerror = request.error = onerror;
request.send();
} catch (exception) {
reject(exception);
}
});
};
// Determine if an XMLHttpRequest was successful
// Some versions of WebKit return 0 for successful file:// URLs
function xhrSuccess(req) {
return (req.status === 200 || (req.status === 0 && req.responseText));
}
}],["browser-url.js","system","browser-url.js",{},function (require, exports, module, __filename, __dirname){
// system/browser-url.js
// ---------------------
/*eslint-env browser*/
"use strict";
exports.resolve = function resolve(base, relative) {
return new URL(relative, base).toString();
};
}],["common-system.js","system","common-system.js",{"./url":2,"./identifier":5,"./module":6,"./resource":8,"./parse-dependencies":7,"./compile":4},function (require, exports, module, __filename, __dirname){
// system/common-system.js
// -----------------------
/*eslint no-console:[0]*/
/*global console*/
"use strict";
var URL = require("./url");
var Identifier = require("./identifier");
var Module = require("./module");
var Resource = require("./resource");
var parseDependencies = require("./parse-dependencies");
var compile = require("./compile");
var has = Object.prototype.hasOwnProperty;
module.exports = System;
function System(location, description, options) {
var self = this;
options = options || {};
description = description || {};
self.name = options.name || description.name || "";
self.location = location;
self.description = description;
self.dependencies = {};
self.main = null;
self.resources = options.resources || {}; // by system.name / module.id
self.modules = options.modules || {}; // by system.name/module.id
self.systemLocations = options.systemLocations || {}; // by system.name;
self.systems = options.systems || {}; // by system.name
self.systemLoadedPromises = options.systemLoadedPromises || {}; // by system.name
self.buildSystem = options.buildSystem; // or self if undefined
self.strategy = options.strategy || "nested";
self.analyzers = {js: self.analyzeJavaScript};
self.translators = {json: self.translateJson};
self.internalRedirects = {};
self.externalRedirects = {};
self.node = !!options.node;
self.browser = !!options.browser;
self.parent = options.parent;
self.root = options.root || self;
// TODO options.optimize
// TODO options.instrument
self.systems[self.name] = self;
self.systemLocations[self.name] = self.location;
self.systemLoadedPromises[self.name] = Promise.resolve(self);
if (options.name != null && description.name == null) {
console.warn(
"Package loaded by name " + JSON.stringify(options.name) +
" has no name"
);
} else if (options.name != null && options.name !== description.name) {
console.warn(
"Package loaded by name " + JSON.stringify(options.name) +
" has mismatched name " + JSON.stringify(description.name)
);
}
// The main property of the description can only create an internal
// redirect, as such it normalizes absolute identifiers to relative.
// All other redirects, whether from internal or external identifiers, can
// redirect to either internal or external identifiers.
self.main = description.main || "index.js";
self.internalRedirects[".js"] = "./" + Identifier.resolve(self.main, "");
// Overlays:
if (options.browser) { self.overlayBrowser(description); }
if (options.node) { self.overlayNode(description); }
// Dependencies:
if (description.dependencies) {
self.addDependencies(description.dependencies);
}
if (self.root === self && description.devDependencies) {
self.addDependencies(description.devDependencies);
}
// Local per-extension overrides:
if (description.extensions) { self.addExtensions(description.extensions); }
if (description.redirects) { self.addRedirects(description.redirects); }
}
System.load = function loadSystem(location, options) {
var self = this;
return self.prototype.loadSystemDescription(location, "<anonymous>")
.then(function (description) {
return new self(location, description, options);
});
};
System.prototype.import = function importModule(rel, abs) {
var self = this;
return self.load(rel, abs)
.then(function onModuleLoaded() {
self.root.main = self.lookup(rel, abs);
return self.require(rel, abs);
});
};
// system.require(rel, abs) must be called only after the module and its
// transitive dependencies have been loaded, as guaranteed by system.load(rel,
// abs)
System.prototype.require = function require(rel, abs) {
var self = this;
// Apart from resolving relative identifiers, this also normalizes absolute
// identifiers.
var res = Identifier.resolve(rel, abs);
if (Identifier.isAbsolute(rel)) {
if (self.externalRedirects[res] === false) {
return {};
}
if (self.externalRedirects[res]) {
return self.require(self.externalRedirects[res], res);
}
var head = Identifier.head(rel);
var tail = Identifier.tail(rel);
if (self.dependencies[head]) {
return self.getSystem(head, abs).requireInternalModule(tail, abs);
} else if (self.modules[head]) {
return self.requireInternalModule(rel, abs, self.modules[rel]);
} else {
var via = abs ? " via " + JSON.stringify(abs) : "";
throw new Error("Can't require " + JSON.stringify(rel) + via + " in " + JSON.stringify(self.name));
}
} else {
return self.requireInternalModule(rel, abs);
}
};
System.prototype.requireInternalModule = function requireInternalModule(rel, abs, module) {
var self = this;
var res = Identifier.resolve(rel, abs);
var id = self.normalizeIdentifier(res);
if (self.internalRedirects[id]) {
return self.require(self.internalRedirects[id], id);
}
module = module || self.lookupInternalModule(id);
// check for load error
if (module.error) {
var error = module.error;
var via = abs ? " via " + JSON.stringify(abs) : "";
error.message = (
"Can't require module " + JSON.stringify(module.id) +
via +
" in " + JSON.stringify(self.name || self.location) +
" because " + error.message
);
throw error;
}
// do not reinitialize modules
if (module.exports != null) {
return module.exports;
}
// do not initialize modules that do not define a factory function
if (typeof module.factory !== "function") {
throw new Error(
"Can't require module " + JSON.stringify(module.filename) +
". No exports. No exports factory."
);
}
module.require = self.makeRequire(module.id, self.root.main);
module.exports = {};
// Execute the factory function:
module.factory.call(
// in the context of the module:
null, // this (defaults to global, except in strict mode)
module.require,
module.exports,
module,
module.filename,
module.dirname
);
return module.exports;
};
System.prototype.makeRequire = function makeRequire(abs, main) {
var self = this;
function require(rel) {
return self.require(rel, abs);
}
require.main = main;
return require;
};
// System:
// Should only be called if the system is known to have already been loaded by
// system.loadSystem.
System.prototype.getSystem = function getSystem(rel, abs) {
var via;
var hasDependency = this.dependencies[rel];
if (!hasDependency) {
via = abs ? " via " + JSON.stringify(abs) : "";
throw new Error(
"Can't get dependency " + JSON.stringify(rel) +
" in package named " + JSON.stringify(this.name) + via
);
}
var dependency = this.systems[rel];
if (!dependency) {
via = abs ? " via " + JSON.stringify(abs) : "";
throw new Error(
"Can't get dependency " + JSON.stringify(rel) +
" in package named " + JSON.stringify(this.name) + via
);
}
return dependency;
};
System.prototype.loadSystem = function (name, abs) {
var self = this;
//var hasDependency = self.dependencies[name];
//if (!hasDependency) {
// var error = new Error("Can't load module " + JSON.stringify(name));
// error.module = true;
// throw error;
//}
var loadingSystem = self.systemLoadedPromises[name];
if (!loadingSystem) {
loadingSystem = self.actuallyLoadSystem(name, abs);
self.systemLoadedPromises[name] = loadingSystem;
}
return loadingSystem;
};
System.prototype.loadSystemDescription = function loadSystemDescription(location, name) {
var self = this;
var descriptionLocation = URL.resolve(location, "package.json");
return self.read(descriptionLocation, "utf-8", "application/json")
.then(function (json) {
try {
return JSON.parse(json);
} catch (error) {
error.message = error.message + " in " +
JSON.stringify(descriptionLocation);
throw error;
}
}, function (error) {
error.message = "Can't load package " + JSON.stringify(name) + " at " +
JSON.stringify(location) + " because " + error.message;
throw error;
});
};
System.prototype.actuallyLoadSystem = function (name, abs) {
var self = this;
var System = self.constructor;
var location = self.systemLocations[name];
if (!location) {
var via = abs ? " via " + JSON.stringify(abs) : "";
throw new Error(
"Can't load package " + JSON.stringify(name) + via +
" because it is not a declared dependency"
);
}
var buildSystem;
if (self.buildSystem) {
buildSystem = self.buildSystem.actuallyLoadSystem(name, abs);
}
return Promise.all([
self.loadSystemDescription(location, name),
buildSystem
]).then(function onDescriptionAndBuildSystem(args) {
var description = args[0];
var buildSystem = args[1];
var system = new System(location, description, {
parent: self,
root: self.root,
name: name,
resources: self.resources,
modules: self.modules,
systems: self.systems,
systemLocations: self.systemLocations,
systemLoadedPromises: self.systemLoadedPromises,
buildSystem: buildSystem,
browser: self.browser,
node: self.node,
strategy: inferStrategy(description)
});
self.systems[system.name] = system;
return system;
});
};
System.prototype.getBuildSystem = function getBuildSystem() {
var self = this;
return self.buildSystem || self;
};
// Module:
System.prototype.normalizeIdentifier = function (id) {
var self = this;
var extension = Identifier.extension(id);
if (
!has.call(self.translators, extension) &&
!has.call(self.analyzers, extension) &&
extension !== "js" &&
extension !== "json"
) {
id += ".js";
}
return id;
};
System.prototype.load = function load(rel, abs) {
var self = this;
return self.deepLoad(rel, abs)
.then(function () {
return self.deepCompile(rel, abs, {});
});
};
System.prototype.deepCompile = function deepCompile(rel, abs, memo) {
var self = this;
var res = Identifier.resolve(rel, abs);
if (Identifier.isAbsolute(rel)) {
if (self.externalRedirects[res]) {
return self.deepCompile(self.externalRedirects[res], res, memo);
}
var head = Identifier.head(rel);
var tail = Identifier.tail(rel);
if (self.dependencies[head]) {
var system = self.getSystem(head, abs);
return system.compileInternalModule(tail, "", memo);
} else {
// XXX no clear idea what to do in this load case.
// Should never reject, but should cause require to produce an
// error.
return Promise.resolve();
}
} else {
return self.compileInternalModule(rel, abs, memo);
}
};
System.prototype.compileInternalModule = function compileInternalModule(rel, abs, memo) {
var self = this;
var res = Identifier.resolve(rel, abs);
var id = self.normalizeIdentifier(res);
if (self.internalRedirects[id]) {
return self.deepCompile(self.internalRedirects[id], "", memo);
}
var module = self.lookupInternalModule(id, abs);
// Break the cycle of violence
if (memo[module.key]) {
return Promise.resolve();
}
memo[module.key] = true;
if (module.compiled) {
return Promise.resolve();
}
module.compiled = true;
return Promise.resolve().then(function () {
return Promise.all(module.dependencies.map(function (dependency) {
return self.deepCompile(dependency, module.id, memo);
}));
}).then(function () {
return self.translate(module);
}).then(function () {
return self.compile(module);
}).catch(function (error) {
module.error = error;
});
};
// Loads a module and its transitive dependencies.
System.prototype.deepLoad = function deepLoad(rel, abs, memo) {
var self = this;
var res = Identifier.resolve(rel, abs);
if (Identifier.isAbsolute(rel)) {
if (self.externalRedirects[res]) {
return self.deepLoad(self.externalRedirects[res], res, memo);
}
var head = Identifier.head(rel);
var tail = Identifier.tail(rel);
if (self.dependencies[head]) {
return self.loadSystem(head, abs)
.then(function (system) {
return system.loadInternalModule(tail, "", memo);
});
} else {
// XXX no clear idea what to do in this load case.
// Should never reject, but should cause require to produce an
// error.
return Promise.resolve();
}
} else {
return self.loadInternalModule(rel, abs, memo);
}
};
System.prototype.loadInternalModule = function loadInternalModule(rel, abs, memo) {
var self = this;
var res = Identifier.resolve(rel, abs);
var id = self.normalizeIdentifier(res);
if (self.internalRedirects[id]) {
return self.deepLoad(self.internalRedirects[id], "", memo);
}
// Extension must be captured before normalization since it is used to
// determine whether to attempt to fallback to index.js for identifiers
// that might refer to directories.
var extension = Identifier.extension(res);
var module = self.lookupInternalModule(id, abs);
// Break the cycle of violence
memo = memo || {};
if (memo[module.key]) {
return Promise.resolve();
}
memo[module.key] = true;
// Return a memoized load
if (module.loadedPromise) {
return module.loadedPromise;
}
module.loadedPromise = Promise.resolve()
.then(function () {
if (module.factory == null && module.exports == null) {
return self.read(module.location, "utf-8")
.then(function (text) {
module.text = text;
return self.finishLoadingModule(module, memo);
}, fallback);
}
});
function fallback(error) {
var redirect = Identifier.resolve("./index.js", res);
module.redirect = redirect;
if (!error || error.notFound && extension === "") {
return self.loadInternalModule(redirect, abs, memo)
.catch(function (fallbackError) {
module.redirect = null;
// Prefer the original error
module.error = error || fallbackError;
});
} else {
module.error = error;
}
}
return module.loadedPromise;
};
System.prototype.finishLoadingModule = function finishLoadingModule(module, memo) {
var self = this;
return Promise.resolve().then(function () {
return self.analyze(module);
}).then(function () {
return Promise.all(module.dependencies.map(function onDependency(dependency) {
return self.deepLoad(dependency, module.id, memo);
}));
});
};
System.prototype.lookup = function lookup(rel, abs) {
var self = this;
var res = Identifier.resolve(rel, abs);
if (Identifier.isAbsolute(rel)) {
if (self.externalRedirects[res]) {
return self.lookup(self.externalRedirects[res], res);
}
var head = Identifier.head(res);
var tail = Identifier.tail(res);
if (self.dependencies[head]) {
return self.getSystem(head, abs).lookupInternalModule(tail, "");
} else if (self.modules[head] && !tail) {
return self.modules[head];
} else {
var via = abs ? " via " + JSON.stringify(abs) : "";
throw new Error(
"Can't look up " + JSON.stringify(rel) + via +
" in " + JSON.stringify(self.location) +
" because there is no external module or dependency by that name"
);
}
} else {
return self.lookupInternalModule(rel, abs);
}
};
System.prototype.lookupInternalModule = function lookupInternalModule(rel, abs) {
var self = this;
var res = Identifier.resolve(rel, abs);
var id = self.normalizeIdentifier(res);
if (self.internalRedirects[id]) {
return self.lookup(self.internalRedirects[id], res);
}
var filename = self.name + "/" + id;
// This module system is case-insensitive, but mandates that a module must
// be consistently identified by the same case convention to avoid problems
// when migrating to case-sensitive file systems.
var key = filename.toLowerCase();
var module = self.modules[key];
if (module && module.redirect && module.redirect !== module.id) {
return self.lookupInternalModule(module.redirect);
}
if (!module) {
module = new Module();
module.id = id;
module.extension = Identifier.extension(id);
module.location = URL.resolve(self.location, id);
module.filename = filename;
module.dirname = Identifier.dirname(filename);
module.key = key;
module.system = self;
module.modules = self.modules;
self.modules[key] = module;
}
if (module.filename !== filename) {
module.error = new Error(
"Can't refer to single module with multiple case conventions: " +
JSON.stringify(filename) + " and " +
JSON.stringify(module.filename)
);
}
return module;
};
System.prototype.addExtensions = function (map) {
var extensions = Object.keys(map);
for (var index = 0; index < extensions.length; index++) {
var extension = extensions[index];
var id = map[extension];
this.analyzers[extension] = this.makeLoadStep(id, "analyze");
this.translators[extension] = this.makeLoadStep(id, "translate");
}
};
System.prototype.makeLoadStep = function makeLoadStep(id, name) {
var self = this;
return function moduleLoaderStep(module) {
return self.getBuildSystem()
.import(id)
.then(function (exports) {
if (exports[name]) {
return exports[name](module);
}
});
};
};
// Translate:
System.prototype.translate = function translate(module) {
var self = this;
if (
module.text != null &&
module.extension != null &&
self.translators[module.extension]
) {
return self.translators[module.extension](module);
}
};
System.prototype.translateJson = function translateJson(module) {
module.text = "module.exports = " + module.text.trim() + ";\n";
};
// Analyze:
System.prototype.analyze = function analyze(module) {
if (
module.text != null &&
module.extension != null &&
this.analyzers[module.extension]
) {
return this.analyzers[module.extension](module);
}
};
System.prototype.analyzeJavaScript = function analyzeJavaScript(module) {
module.dependencies.push.apply(module.dependencies, parseDependencies(module.text));
};
// Compile:
System.prototype.compile = function (module) {
if (
module.factory == null &&
module.redirect == null &&
module.exports == null
) {
compile(module);
}
};
// Resource:
System.prototype.getResource = function getResource(rel, abs) {
var self = this;
if (Identifier.isAbsolute(rel)) {
var head = Identifier.head(rel);
var tail = Identifier.tail(rel);
return self.getSystem(head, abs).getInternalResource(tail);
} else {
return self.getInternalResource(Identifier.resolve(rel, abs));
}
};
System.prototype.locateResource = function locateResource(rel, abs) {
var self = this;
if (Identifier.isAbsolute(rel)) {
var head = Identifier.head(rel);
var tail = Identifier.tail(rel);
return self.loadSystem(head, abs)
.then(function onSystemLoaded(subsystem) {
return subsystem.getInternalResource(tail);
});
} else {
return Promise.resolve(self.getInternalResource(Identifier.resolve(rel, abs)));
}
};
System.prototype.getInternalResource = function getInternalResource(id) {
var self = this;
// TODO redirects
var filename = self.name + "/" + id;
var key = filename.toLowerCase();
var resource = self.resources[key];
if (!resource) {
resource = new Resource();
resource.id = id;
resource.filename = filename;
resource.dirname = Identifier.dirname(filename);
resource.key = key;
resource.location = URL.resolve(self.location, id);
resource.system = self;
self.resources[key] = resource;
}
return resource;
};
// Dependencies:
System.prototype.addDependencies = function addDependencies(dependencies) {
var self = this;
var names = Object.keys(dependencies);
for (var index = 0; index < names.length; index++) {
var name = names[index];
self.dependencies[name] = true;
if (!self.systemLocations[name]) {
var location;
if (this.strategy === "flat") {
location = URL.resolve(self.root.location, "node_modules/" + name + "/");
} else {
location = URL.resolve(self.location, "node_modules/" + name + "/");
}
self.systemLocations[name] = location;
}
}
};
// introduce allows an analyzer module to introduce a package to a dependency
// of the analyzer's package.
System.prototype.introduce = function introduce(system, name) {
if (!this.dependencies[name]) {
throw new Error("Extension package cannot introduce a module to a package that the analyzer does not directly depend upon.");
}
system.dependencies[name] = true;
if (!system.systemLocations[name]) {
system.systemLocations[name] = this.systemLocations[name];
}
};
// Redirects:
System.prototype.addRedirects = function addRedirects(redirects) {
var self = this;
var sources = Object.keys(redirects);
for (var index = 0; index < sources.length; index++) {
var source = sources[index];
var target = redirects[source];
self.addRedirect(source, target);
}
};
System.prototype.addRedirect = function addRedirect(source, target) {
var self = this;
if (Identifier.isAbsolute(source)) {
self.externalRedirects[source] = target;
} else {
source = self.normalizeIdentifier(Identifier.resolve(source));
self.internalRedirects[source] = target;
}
};
// Etc:
System.prototype.overlayBrowser = function overlayBrowser(description) {
var self = this;
if (typeof description.browser === "string") {
self.addRedirect("", description.browser);
} else if (description.browser && typeof description.browser === "object") {
self.addRedirects(description.browser);
}
};
System.prototype.inspect = function () {
var self = this;
return {type: "system", location: self.location};
};
function inferStrategy(description) {
// The existence of an _args property in package.json distinguishes
// packages that were installed with npm version 3 or higher.
if (description._args) {
return "flat";
}
return "nested";
}
}],["compile.js","system","compile.js",{},function (require, exports, module, __filename, __dirname){
// system/compile.js
// -----------------
"use strict";
module.exports = compile;
// By using a named "eval" most browsers will execute in the global scope.
// http://www.davidflanagan.com/2010/12/global-eval-in.html
// Unfortunately execScript doesn't always return the value of the evaluated expression (at least in Chrome)
var globalEval = /*this.execScript ||*/eval;
// For Firebug evaled code isn't debuggable otherwise
// http://code.google.com/p/fbug/issues/detail?id=2198
if (global.navigator && global.navigator.userAgent.indexOf("Firefox") >= 0) {
globalEval = new Function("_", "return eval(_)");
}
function compile(module) {
// Here we use a couple tricks to make debugging better in various browsers:
// TODO: determine if these are all necessary / the best options
// 1. name the function with something inteligible since some debuggers display the first part of each eval (Firebug)
// 2. append the "//# sourceURL=filename" hack (Safari, Chrome, Firebug)
// * http://pmuellr.blogspot.com/2009/06/debugger-friendly.html
// * http://blog.getfirebug.com/2009/08/11/give-your-eval-a-name-with-sourceurl/
// TODO: investigate why this isn't working in Firebug.
// 3. set displayName property on the factory function (Safari, Chrome)
var displayName = module.filename.replace(/[^\w\d]|^\d/g, "_");
try {
module.factory = globalEval(
"(function " +
displayName +
"(require, exports, module, __filename, __dirname) {" +
module.text +
"//*/\n})\n//# sourceURL=" +
module.system.location + module.id
);
} catch (exception) {
exception.message = exception.message + " in " + module.filename;
throw exception;
}
// This should work and would be simpler, but Firebug does not show scripts executed via "new Function()" constructor.
// TODO: sniff browser?
// module.factory = new Function("require", "exports", "module", module.text + "\n//*/"+sourceURLComment);
module.factory.displayName = module.filename;
}
}],["identifier.js","system","identifier.js",{},function (require, exports, module, __filename, __dirname){
// system/identifier.js
// --------------------
"use strict";
exports.isAbsolute = isAbsolute;
function isAbsolute(path) {
return (
path !== "" &&
path.lastIndexOf("./", 0) < 0 &&
path.lastIndexOf("../", 0) < 0
);
}
exports.isBare = isBare;
function isBare(id) {
var lastSlash = id.lastIndexOf("/");
return id.indexOf(".", lastSlash) < 0;
}
// TODO @user/name package names
exports.head = head;
function head(id) {
var firstSlash = id.indexOf("/");
if (firstSlash < 0) { return id; }