forked from systemjs/builder
/
utils.js
376 lines (315 loc) · 12.8 KB
/
utils.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
var path = require('path');
var url = require('url');
exports.traceurGet = function(module) {
var traceur = require('traceur');
var traceurVersion = traceur.loader.NodeTraceurLoader.prototype.version;
return $traceurRuntime.ModuleStore.get('traceur@' + traceurVersion + '/src/' + module);
};
exports.extend = extend;
function extend(a, b) {
for (var p in b)
a[p] = b[p];
return a;
}
exports.dextend = dextend;
function dextend(a, b) {
for (var p in b) {
if (!b.hasOwnProperty(p))
continue;
var val = b[p];
if (typeof val === 'object')
dextend(a[p] = typeof a[p] === 'object' ? a[p] : {}, val);
else
a[p] = val;
}
return a;
}
exports.getFormatHint = getFormatHint;
function getFormatHint(compileOpts) {
var formatHint = '';
var format = compileOpts.format;
if (format == 'umd')
format = 'amd';
if (format == 'amd' || format == 'cjs' || format == 'global')
formatHint = formatHint + '"format ' + format + '";\n';
if (compileOpts.format == 'global') {
for (var g in compileOpts.globalDeps)
formatHint = formatHint + '"globals.' + compileOpts.globalDeps[g] + ' ' + g + '";\n';
if (compileOpts.globalName)
formatHint = formatHint + '"exports ' + compileOpts.globalName + '";\n';
}
return formatHint;
}
var isWin = process.platform.match(/^win/);
exports.fromFileURL = fromFileURL;
function fromFileURL(url) {
return url.substr(7 + !!isWin).replace(/\//g, path.sep);
}
exports.toFileURL = toFileURL;
function toFileURL(path) {
return 'file://' + (isWin ? '/' : '') + path.replace(/\\/g, '/');
}
exports.getAlias = getAlias
function getAlias(loader, canonicalName) {
var pluginIndex = loader.pluginFirst ? canonicalName.indexOf('!') : canonicalName.lastIndexOf('!');
if (pluginIndex != -1)
return getAlias(loader, canonicalName.substr(0, pluginIndex)) + '!' + getAlias(loader, canonicalName.substr(pluginIndex + 1));
// replace subpath conditionals with subpath
canonicalName = canonicalName.replace('/#:./', '/');
if (canonicalName.match(/\#[\:\{\?]/))
throw new Error('Unable to alias the conditional dependency "' + canonicalName + '".');
var packageName = getPackage(loader.packages, loader.decanonicalize(canonicalName));
var packageMain = packageName && loader.packages[packageName].main;
if (packageMain && canonicalName.substr(canonicalName.length - packageMain.length - 1) == '/' + packageMain)
canonicalName = canonicalName.substr(0, canonicalName.length - packageMain.length - 1);
var bestAliasLength = 0;
var bestAliasSubpath;
var bestAlias;
Object.keys(loader.map).forEach(function(alias) {
if (alias.split('/').length <= bestAliasLength)
return;
// get mapped without defaultJSExtension
var mapped = getCanonicalName(loader, loader.decanonicalize(loader.map[alias]));
// do matching with defaultJSExtension checking
if (loader.defaultJSExtensions && canonicalName == mapped + '.js') {
bestAlias = alias;
bestAliasSubpath = '';
bestAliasLength = alias.split('/').length;
}
else if (canonicalName.substr(0, mapped.length) == mapped &&
(canonicalName.length == mapped.length || canonicalName[mapped.length] == '/')) {
bestAlias = alias;
bestAliasSubpath = canonicalName.substr(mapped.length);
bestAliasLength = alias.split('/').length;
}
});
if (bestAlias)
return bestAlias + bestAliasSubpath;
return canonicalName;
}
exports.verifyTree = verifyTree;
function verifyTree(tree) {
if (typeof tree != 'object' || tree instanceof Array)
throw new TypeError('Expected a trace tree object');
Object.keys(tree).forEach(function(key) {
var load = tree[key];
if (typeof load === 'boolean')
return;
if (load && typeof load != 'object' || !load.name || !(load.conditional || load.deps))
throw new TypeError('Expected a trace tree object, but "' + key + '" is not a load record.');
});
}
exports.getCanonicalName = getCanonicalName;
function getCanonicalName(loader, normalized, isPlugin) {
// 1. Boolean conditional
var booleanIndex = normalized.lastIndexOf('#?');
if (booleanIndex != -1) {
var booleanModule = normalized.substr(booleanIndex + 2);
var negate = booleanModule[0] == '~';
if (negate)
booleanModule = booleanModule.substr(1);
return getCanonicalName(loader, normalized.substr(0, booleanIndex)) + '#?' + (negate ? '~' : '') + canonicalizeCondition(loader, booleanModule);
}
// 2. Plugins
var pluginIndex = loader.pluginFirst ? normalized.indexOf('!') : normalized.lastIndexOf('!');
if (pluginIndex != -1)
return getCanonicalName(loader, normalized.substr(0, pluginIndex), !loader.pluginFirst) + '!' + getCanonicalName(loader, normalized.substr(pluginIndex + 1), loader.pluginFirst);
// 3. Package environment map
var pkgEnvIndex = normalized.indexOf('/#:');
if (pkgEnvIndex != -1)
return getCanonicalName(loader, normalized.substr(0, pkgEnvIndex), isPlugin) + '/#:' + normalized.substr(pkgEnvIndex + 3);
// Finally get canonical plain
var canonical = getCanonicalNamePlain(loader, normalized, isPlugin);
// 4. Canonicalize conditional interpolation
var conditionalMatch = canonical.match(interpolationRegEx);
if (conditionalMatch)
return getCanonicalNamePlain(loader, normalized, isPlugin).replace(interpolationRegEx, '#{' + canonicalizeCondition(loader, conditionalMatch[0].substr(2, conditionalMatch[0].length - 3)) + '}');
return canonical;
}
// calculate the canonical name of the normalized module
// unwraps loader syntaxes to derive component parts
var interpolationRegEx = /#\{[^\}]+\}/;
function canonicalizeCondition(loader, conditionModule) {
var conditionExport;
var exportIndex = conditionModule.lastIndexOf('|');
if (exportIndex != -1) {
conditionExport = conditionModule.substr(exportIndex + 1)
conditionModule = conditionModule.substr(0, exportIndex) || '@system-env';
}
return getCanonicalName(loader, conditionModule) + (conditionExport ? '|' + conditionExport : '');
}
// syntax-free getCanonicalName
// just reverse-applies paths and defulatJSExtension to determine the canonical
function getCanonicalNamePlain(loader, normalized, isPlugin) {
// now just reverse apply paths rules to get canonical name
var pathMatch;
// first check exact path matches
for (var p in loader.paths) {
if (loader.paths[p].indexOf('*') != -1)
continue;
var curPath = normalizePath(loader, loader.paths[p], isPlugin);
// always stop on first exact match
if (normalized === curPath)
return p;
// support trailing / in paths rules
else if (curPath[curPath.length - 1] == '/' &&
normalized.substr(0, curPath.length - 1) == curPath.substr(0, curPath.length - 1) &&
(normalized.length < curPath.length || normalized[curPath.length - 1] == curPath[curPath.length - 1])) {
// first case is that canonicalize('src') = 'app' for 'app/': 'src/'
return normalized.length < curPath.length ? p.substr(0, p.length - 1) : p + normalized.substr(curPath.length);
}
}
// then wildcard matches
var pathMatchLength = 0;
var curMatchLength;
if (!pathMatch)
for (var p in loader.paths) {
if (loader.paths[p].indexOf('*') == -1)
continue;
// normalize the output path
var curPath = normalizePath(loader, loader.paths[p], true);
// do reverse match
var wIndex = curPath.indexOf('*');
if (normalized.substr(0, wIndex) === curPath.substr(0, wIndex)
&& normalized.substr(normalized.length - curPath.length + wIndex + 1) === curPath.substr(wIndex + 1)) {
curMatchLength = curPath.split('/').length;
if (curMatchLength >= pathMatchLength) {
pathMatch = p.replace('*', normalized.substr(wIndex, normalized.length - curPath.length + 1));
pathMatchLength = curMatchLength;
}
}
}
// when no path was matched, act like the standard rule is *: baseURL/*
if (!pathMatch) {
if (normalized.substr(0, loader.baseURL.length) == loader.baseURL)
pathMatch = normalized.substr(loader.baseURL.length);
else if (normalized.match(absURLRegEx))
throw new Error('Unable to calculate canonical name to bundle ' + normalized + '. Ensure that this module sits within the baseURL or a wildcard path config.');
else
pathMatch = normalized;
}
return pathMatch;
}
exports.getPackageConfigPath = getPackageConfigPath;
// check if the given normalized name matches a packageConfigPath
// if so, loads the config
var packageConfigPaths = {};
// data object for quick checks against package paths
function createPkgConfigPathObj(path) {
var lastWildcard = path.lastIndexOf('*');
var length = Math.max(lastWildcard + 1, path.lastIndexOf('/'));
return {
length: length,
// NB handle regex control character escapes or simply create a test function here
regEx: new RegExp('^(' + path.substr(0, length).replace(/\*/g, '[^\\/]+') + ')(\\/|$)'),
wildcard: lastWildcard != -1
};
}
// most specific match wins
exports.getPackageConfigPath = getPackageConfigPath;
function getPackageConfigPath(packageConfigPaths, normalized) {
var pkgName, exactMatch = false, configPath;
for (var i = 0; i < packageConfigPaths.length; i++) {
var packageConfigPath = packageConfigPaths[i];
var p = packageConfigPaths[packageConfigPath] || (packageConfigPaths[packageConfigPath] = createPkgConfigPathObj(packageConfigPath));
if (normalized.length < p.length)
continue;
var match = normalized.match(p.regEx);
if (match && (!pkgName || (!(exactMatch && p.wildcard) && pkgName.length < match[1].length))) {
pkgName = match[1];
exactMatch = !p.wildcard;
configPath = pkgName + packageConfigPath.substr(p.length);
}
}
if (!pkgName)
return;
// return value is only part modified from SystemJS implementation of getPackageConfigMatch
return configPath;
}
// determine whether the given module name is a package config file
exports.isPackageConfig = isPackageConfig;
var curHash;
var configPathCache = null;
var canonicalConfigPaths = null;
function isPackageConfig(loader, canonical) {
if (loader.configHash != curHash) {
configPathCache = null;
curHash = loader.configHash;
}
// generate canonical packageConfigPaths for matching
if (!configPathCache) {
canonicalConfigPaths = loader.packageConfigPaths.map(function(configPath) {
return getCanonicalName(loader, configPath);
});
configPathCache = {};
}
if (canonical in configPathCache)
return configPathCache[canonical];
// check if the given canonical matches the canonical package config paths
var cfgPathMatch = getPackageConfigPath(canonicalConfigPaths, canonical);
configPathCache[canonical] = cfgPathMatch && cfgPathMatch.split('/').length == canonical.split('/').length;
return configPathCache[canonical];
}
exports.getPackage = getPackage;
function getPackage(packages, name) {
// use most specific package
var curPkg, curPkgLen = 0, pkgLen;
for (var p in packages) {
if (name.substr(0, p.length) === p && (name.length === p.length || name[p.length] === '/')) {
pkgLen = p.split('/').length;
if (pkgLen > curPkgLen) {
curPkg = p;
curPkgLen = pkgLen;
}
}
}
return curPkg;
}
var absURLRegEx = /^[^\/]+:\/\//;
function normalizePath(loader, path, skipExtension) {
var curMap = loader.map;
var curPaths = loader.paths;
var curPackages = loader.packages;
loader.map = {};
loader.paths = {};
loader.packages = {};
var normalized = loader.normalizeSync(path);
if (skipExtension && path.substr(path.length - 3, 3) != '.js' && normalized.substr(normalized.length - 3, 3) == '.js')
normalized = normalized.substr(0, normalized.length - 3);
loader.map = curMap;
loader.paths = curPaths;
loader.packages = curPackages;
return normalized;
}
var sysConditions = ['browser', 'node', 'dev', 'production', 'default'];
exports.parseCondition = parseCondition;
function parseCondition(condition) {
var conditionExport, conditionModule, negation;
var negation = condition[0] == '~';
var conditionExportIndex = condition.lastIndexOf('|');
if (conditionExportIndex != -1) {
conditionExport = condition.substr(conditionExportIndex + 1);
conditionModule = condition.substr(negation, conditionExportIndex - negation);
if (conditionExport[0] == '~') {
negation = true;
conditionExport = conditionExport.substr(1);
}
}
else {
conditionExport = 'default';
conditionModule = condition.substr(negation);
if (sysConditions.indexOf(conditionModule) != -1) {
conditionExport = conditionModule;
conditionModule = '@system-env';
}
}
return {
module: conditionModule,
prop: conditionExport,
negate: negation
};
}
exports.serializeCondition = serializeCondition;
function serializeCondition(conditionObj) {
return conditionObj.module + '|' + (conditionObj.negate ? '~' : '') + conditionObj.prop;
}