This repository has been archived by the owner on Jul 13, 2020. It is now read-only.
/
system.js
256 lines (221 loc) · 8.1 KB
/
system.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
/*
*********************************************************************************************
System Loader Implementation
- Implemented to https://github.com/jorendorff/js-loaders/blob/master/browser-loader.js
- <script type="module"> supported
*********************************************************************************************
*/
(function (global) {
var isBrowser = typeof window != 'undefined';
var Loader = global.Loader || require('./loader');
var Promise = global.Promise || require('./promise');
// Helpers
// Absolute URL parsing, from https://gist.github.com/Yaffle/1088850
function parseURI(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);
}
function toAbsoluteURL(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;
}
var fetchTextFromURL;
if (isBrowser) {
fetchTextFromURL = function(url, fulfill, reject) {
var xhr = new XMLHttpRequest();
var sameDomain = true;
if (!('withCredentials' in xhr)) {
// check if same domain
var domainCheck = /^(\w+:)?\/\/([^\/]+)/.exec(url);
if (domainCheck) {
sameDomain = domainCheck[2] === window.location.host;
if (domainCheck[1])
sameDomain &= domainCheck[1] === window.location.protocol;
}
}
if (!sameDomain) {
xhr = new XDomainRequest();
xhr.onload = load;
xhr.onerror = error;
xhr.ontimeout = error;
}
function load() {
fulfill(xhr.responseText);
}
function error() {
reject(xhr.statusText + ': ' + url || 'XHR error');
}
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200 || (xhr.status == 0 && xhr.responseText)) {
load();
} else {
error();
}
}
};
xhr.open("GET", url, true);
xhr.send(null);
}
}
else {
var fs = require('fs');
fetchTextFromURL = function(url, fulfill, reject) {
return fs.readFile(url, function(err, data) {
if (err)
return reject(err);
else
fulfill(data + '');
});
}
}
var System = new Loader({
global: isBrowser ? window : global,
strict: true,
normalize: function(name, parentName, parentAddress) {
if (typeof name != 'string')
throw new TypeError('Module name must be a string');
var segments = name.split('/');
if (segments.length == 0)
throw new TypeError('No module name provided');
// current segment
var i = 0;
// is the module name relative
var rel = false;
// number of backtracking segments
var dotdots = 0;
if (segments[0] == '.') {
i++;
if (i == segments.length)
throw new TypeError('Illegal module name "' + name + '"');
rel = true;
}
else {
while (segments[i] == '..') {
i++;
if (i == segments.length)
throw new TypeError('Illegal module name "' + name + '"');
}
if (i)
rel = true;
dotdots = i;
}
for (var j = i; j < segments.length; j++) {
var segment = segments[j];
if (segment == '' || segment == '.' || segment == '..')
throw new TypeError('Illegal module name"' + name + '"');
}
if (!rel)
return name;
// build the full module name
var normalizedParts = [];
var parentParts = (parentName || '').split('/');
var normalizedLen = parentParts.length - 1 - dotdots;
normalizedParts = normalizedParts.concat(parentParts.splice(0, parentParts.length - 1 - dotdots));
normalizedParts = normalizedParts.concat(segments.splice(i));
return normalizedParts.join('/');
},
locate: function(load) {
var name = load.name;
// NB no specification provided for System.paths, used ideas discussed in https://github.com/jorendorff/js-loaders/issues/25
// most specific (longest) match wins
var pathMatch = '', wildcard;
// check to see if we have a paths entry
for (var p in this.paths) {
var pathParts = p.split('*');
if (pathParts.length > 2)
throw new TypeError('Only one wildcard in a path is permitted');
// exact path match
if (pathParts.length == 1) {
if (name == p && p.length > pathMatch.length)
pathMatch = p;
}
// wildcard path match
else {
if (name.substr(0, pathParts[0].length) == pathParts[0] && name.substr(name.length - pathParts[1].length) == pathParts[1]) {
pathMatch = p;
wildcard = name.substr(pathParts[0].length, name.length - pathParts[1].length - pathParts[0].length);
}
}
}
var outPath = this.paths[pathMatch];
if (wildcard)
outPath = outPath.replace('*', wildcard);
return toAbsoluteURL(this.baseURL, outPath);
},
fetch: function(load) {
var resolve, reject, promise = new Promise(function(_resolve, _reject) { resolve = _resolve; reject = _reject; });
fetchTextFromURL(toAbsoluteURL(this.baseURL, load.address), function(source) {
resolve(source);
}, reject);
return promise;
}
});
if (isBrowser) {
var href = window.location.href.split('#')[0].split('?')[0];
System.baseURL = href.substring(0, href.lastIndexOf('/') + 1);
}
else {
System.baseURL = './';
}
System.paths = { '*': '*.js' };
// <script type="module"> support
// allow a data-init function callback once loaded
if (isBrowser) {
var curScript = document.getElementsByTagName('script');
curScript = curScript[curScript.length - 1];
document.onreadystatechange = function() {
if (document.readyState == 'interactive') {
var scripts = document.getElementsByTagName('script');
for (var i = 0; i < scripts.length; i++) {
var script = scripts[i];
if (script.type == 'module') {
// <script type="module" name="" src=""> support
var name = script.getAttribute('name');
var address = script.getAttribute('src');
var source = script.innerHTML;
(name
? System.define(name, source, { address: address })
: System.module(source, { address: address })
).then(function() {}, function(err) { nextTick(function() { throw err; }); });
}
}
}
}
// run the data-init function on the script tag
if (curScript.getAttribute('data-init'))
window[curScript.getAttribute('data-init')]();
}
if (typeof exports === 'object')
module.exports = System;
global.System = System;
})(typeof global !== 'undefined' ? global : this);