-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
core.js
235 lines (184 loc) · 5.96 KB
/
core.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
var absURLRegEx = /^[^\/]+:\/\//;
function readMemberExpression(p, value) {
var pParts = p.split('.');
while (pParts.length)
value = value[pParts.shift()];
return value;
}
var baseURLCache = {};
function getBaseURLObj() {
if (baseURLCache[this.baseURL])
return baseURLCache[this.baseURL];
// normalize baseURL if not already
if (this.baseURL[this.baseURL.length - 1] != '/')
this.baseURL += '/';
var baseURL = new URL(this.baseURL, baseURI);
this.baseURL = baseURL.href;
return (baseURLCache[this.baseURL] = baseURL);
}
var baseURIObj = new URL(baseURI);
(function() {
hookConstructor(function(constructor) {
return function() {
constructor.call(this);
// support baseURL
this.baseURL = baseURI.substr(0, baseURI.lastIndexOf('/') + 1);
// support the empty module, as a concept
this.set('@empty', this.newModule({}));
};
});
/*
Normalization
If a name is relative, we apply URL normalization to the page
If a name is an absolute URL, we leave it as-is
Plain names (neither of the above) run through the map and package
normalization phases (applying before and after this one).
The paths normalization phase applies last (paths extension), which
defines the `normalizeSync` function and normalizes everything into
a URL.
The final normalization
*/
hook('normalize', function() {
return function(name, parentName) {
// relative URL-normalization
if (name[0] == '.' || name[0] == '/')
return new URL(name, parentName || baseURIObj).href;
return name;
};
});
/*
__useDefault
When a module object looks like:
newModule(
__useDefault: true,
default: 'some-module'
})
Then importing that module provides the 'some-module'
result directly instead of the full module.
Useful for eg module.exports = function() {}
*/
hook('import', function(systemImport) {
return function(name, parentName, parentAddress) {
return systemImport.call(this, name, parentName, parentAddress).then(function(module) {
return module.__useDefault ? module['default'] : module;
});
};
});
/*
Extend config merging one deep only
loader.config({
some: 'random',
config: 'here',
deep: {
config: { too: 'too' }
}
});
<=>
loader.some = 'random';
loader.config = 'here'
loader.deep = loader.deep || {};
loader.deep.config = { too: 'too' };
Normalizes meta and package configs allowing for:
System.config({
meta: {
'./index.js': {}
}
});
To become
System.meta['https://thissite.com/index.js'] = {};
For easy normalization canonicalization with latest URL support.
*/
var packageProperties = ['main', 'format', 'defaultExtension', 'meta', 'map'];
SystemJSLoader.prototype.config = function(cfg) {
// always configure baseURL first
if (cfg.baseURL) {
var hasConfig = false;
function checkHasConfig(obj) {
for (var p in obj)
return true;
}
if (checkHasConfig(this.packages) || checkHasConfig(this.meta) || checkHasConfig(this.depCache) || checkHasConfig(this.bundles))
throw new TypeError('baseURL should only be configured once and must be configured first.');
this.baseURL = cfg.baseURL;
// sanitize baseURL
getBaseURLObj.call(this);
}
if (cfg.defaultJSExtensions)
this.defaultJSExtensions = cfg.defaultJSExtensions;
if (cfg.paths) {
for (var p in cfg.paths)
this.paths[p] = cfg.paths[p];
}
if (cfg.map) {
for (var p in cfg.map) {
var v = cfg.map[p];
// object map backwards-compat into packages configuration
if (typeof v !== 'string') {
var normalized = this.normalizeSync(p);
// if doing default js extensions, undo to get package name
if (this.defaultJSExtensions && p.substr(p.length - 3, 3) != '.js')
normalized = normalized.substr(0, normalized.length - 3);
// if a package main, revert it
var pkgMatch = '';
for (var pkg in this.packages) {
if (normalized.substr(0, pkg.length) == pkg
&& (!normalized[pkg.length] || normalized[pkg.length] == '/')
&& pkgMatch.split('/').length < pkg.split('/').length)
pkgMatch = pkg;
}
if (pkgMatch && this.packages[pkgMatch].main)
normalized = normalized.substr(0, normalized.length - this.packages[pkgMatch].main.length - 1);
var pkg = this.packages[normalized] = this.packages[normalized] || {};
pkg.map = v;
}
else {
this.map[p] = v;
}
}
}
if (cfg.packages) {
for (var p in cfg.packages) {
var prop = this.normalizeSync(p);
// if doing default js extensions, undo to get package name
if (this.defaultJSExtensions && p.substr(p.length - 3, 3) != '.js')
prop = prop.substr(0, prop.length - 3);
this.packages[prop]= this.packages[prop] || {};
for (var q in cfg.packages[p]) {
if (indexOf.call(packageProperties, q) == -1 && typeof console != 'undefined' && console.warn)
console.warn('"' + q + '" is not a valid package configuration option in package ' + p);
this.packages[prop][q] = cfg.packages[p][q];
}
}
}
if (cfg.bundles) {
for (var p in cfg.bundles) {
var bundle = [];
for (var i = 0; i < cfg.bundles[p].length; i++)
bundle.push(this.normalizeSync(cfg.bundles[p][i]));
this.bundles[p] = bundle;
}
}
for (var c in cfg) {
var v = cfg[c];
var normalizeProp = false, normalizeValArray = false;
if (c == 'baseURL' || c == 'map' || c == 'packages' || c == 'bundles' || c == 'paths')
continue;
if (typeof v != 'object' || v instanceof Array) {
this[c] = v;
}
else {
this[c] = this[c] || {};
if (c == 'meta' || c == 'depCache')
normalizeProp = true;
for (var p in v) {
if (c == 'meta' && p[0] == '*')
this[c][p] = v[p];
else if (normalizeProp)
this[c][this.normalizeSync(p)] = v[p];
else
this[c][p] = v[p];
}
}
}
};
})();