-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmap.ts
633 lines (605 loc) · 20.2 KB
/
map.ts
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
import {
baseUrl,
rebase,
isPlain,
isURL,
getCommonBase,
resolve,
sameOrigin,
} from "./url.js";
import { alphabetize } from "./alphabetize.js";
let crypto;
export interface IImportMap {
imports?: Record<string, string>;
scopes?: {
[scope: string]: Record<string, string>;
};
integrity?: {
[url: string]: string
}
}
export class ImportMap implements IImportMap {
imports: Record<string, string> = Object.create(null);
scopes: Record<string, Record<string, string>> = Object.create(null);
integrity: Record<string, string> = Object.create(null);
/**
* The absolute URL of the import map, for determining relative resolutions
* When using file:/// URLs this allows relative modules to be co-located
*/
mapUrl: URL;
/**
* The URL to use for root-level resolutions in the import map
* If null, root resolutions are not resolved and instead left as-is
*
* By default, rootUrl is null unless the mapUrl is an http or https URL,
* in which case it is taken to be the root of the mapUrl.
*/
rootUrl: URL | null;
/**
* Create a new import map instance
*
* @param opts import map options, can be an optional bag of { map?, mapUrl?, rootUrl? } or just a direct mapUrl
*/
constructor(
opts:
| {
map?: IImportMap;
mapUrl?: string | URL;
rootUrl?: string | URL | null;
}
| string
| URL
) {
let {
map,
mapUrl = baseUrl,
rootUrl,
} = opts instanceof URL ||
typeof opts === "string" ||
typeof opts === "undefined"
? { mapUrl: opts, map: undefined, rootUrl: undefined }
: opts;
if (typeof mapUrl === "string") mapUrl = new URL(mapUrl);
this.mapUrl = mapUrl;
if (
rootUrl === undefined &&
(this.mapUrl.protocol === "http:" || this.mapUrl.protocol === "https:")
)
rootUrl = new URL("/", this.mapUrl);
else if (typeof rootUrl === "string") rootUrl = new URL(rootUrl);
this.rootUrl = rootUrl || null;
if (map) this.extend(map);
}
/**
* Clones the import map
* @returns Cloned import map
*/
clone() {
return new ImportMap({ mapUrl: this.mapUrl, rootUrl: this.rootUrl }).extend(
this
);
}
/**
* Extends the import map mappings
* @param map Import map to extend with
* @param overrideScopes Set to true to have scopes be replacing instead of extending
* @returns ImportMap for chaining
*/
extend(map: IImportMap, overrideScopes = false) {
Object.assign(this.imports, map.imports);
if (overrideScopes) {
Object.assign(this.scopes, map.scopes);
} else if (map.scopes) {
for (const scope of Object.keys(map.scopes))
Object.assign(
(this.scopes[scope] = this.scopes[scope] || Object.create(null)),
map.scopes[scope]
);
}
Object.assign(this.integrity, map.integrity);
this.rebase();
return this;
}
/**
* Performs an alphanumerical sort on the import map imports and scopes
* @returns ImportMap for chaining
*/
sort() {
this.imports = alphabetize(this.imports);
this.scopes = alphabetize(this.scopes);
for (const scope of Object.keys(this.scopes))
this.scopes[scope] = alphabetize(this.scopes[scope]);
this.integrity = alphabetize(this.integrity);
return this;
}
/**
* Set a specific entry in the import map
*
* @param name Specifier to set
* @param target Target of the map
* @param parent Optional parent scope
* @returns ImportMap for chaining
*/
set(name: string, target: string, parent?: string) {
if (!parent) {
this.imports[name] = target;
} else {
this.scopes[parent] = this.scopes[parent] || Object.create(null);
this.scopes[parent][name] = target;
}
return this;
}
/**
* @param target URL target
* @param integrity Integrity
*/
setIntegrity(target: string, integrity: string) {
this.integrity[target] = integrity;
const targetRebased = rebase(target, this.mapUrl, this.rootUrl);
if (targetRebased !== target && this.integrity[targetRebased])
delete this.integrity[targetRebased];
if (targetRebased.startsWith('./') && target !== targetRebased.slice(2) && this.integrity[targetRebased.slice(2)])
delete this.integrity[targetRebased.slice(2)];
}
/**
* @param target URL target
* @param integrity Integrity
*/
getIntegrity(target: string, integrity: string) {
const targetResolved = resolve(target, this.mapUrl, this.rootUrl);
if (this.integrity[targetResolved]) return this.integrity[targetResolved];
const targetRebased = rebase(targetResolved, this.mapUrl, this.rootUrl);
if (this.integrity[targetRebased]) return this.integrity[targetRebased];
if (this.integrity[targetRebased.slice(2)]) return this.integrity[targetRebased.slice(2)];
}
/**
* Bulk replace URLs in the import map
* Provide a URL ending in "/" to perform path replacements
*
* @param url {String} The URL to replace
* @param newUrl {String} The URL to replace it with
* @returns ImportMap for chaining
*/
replace(url: string, newUrl: string) {
const replaceSubpaths = url.endsWith("/");
if (!isURL(url)) throw new Error("URL remapping only supports URLs");
const newRelPkgUrl = rebase(newUrl, this.mapUrl, this.rootUrl);
if (this.imports[url]) {
this.imports[newRelPkgUrl] = this.imports[url];
delete this.imports[url];
}
if (replaceSubpaths) {
for (const impt of Object.keys(this.imports)) {
const target = this.imports[impt];
if (target.startsWith(url)) {
this.imports[impt] = newRelPkgUrl + target.slice(url.length);
}
}
}
for (const scope of Object.keys(this.scopes)) {
const scopeImports = this.scopes[scope];
const scopeUrl = resolve(scope, this.mapUrl, this.rootUrl);
if ((replaceSubpaths && scopeUrl.startsWith(url)) || scopeUrl === url) {
const newScope = newRelPkgUrl + scopeUrl.slice(url.length);
delete this.scopes[scope];
this.scopes[newScope] = scopeImports;
}
for (const impt of Object.keys(scopeImports)) {
const target = scopeImports[impt];
if ((replaceSubpaths && target.startsWith(url)) || target === url)
scopeImports[impt] = newRelPkgUrl + target.slice(url.length);
}
}
if (this.integrity[url]) {
this.integrity[newRelPkgUrl] = this.integrity[url];
delete this.integrity[url];
}
return this;
}
/**
* Groups subpath mappings into path mappings when multiple exact subpaths
* exist under the same path.
*
* For two mappings like { "base/a.js": "/a.js", "base/b.js": "/b.js" },
* these will be replaced with a single path mapping { "base/": "/" }.
* Groupings are done throughout all import scopes individually.
*
* @returns ImportMap for chaining
*/
combineSubpaths() {
// iterate possible bases and submappings, grouping bases greedily
const combineSubpathMappings = (mappings: Record<string, string>) => {
let outMappings: Record<string, string> = Object.create(null);
for (let impt of Object.keys(mappings)) {
const target = mappings[impt];
// Check if this import is already handled by an existing path mapping
// If so, either merge with it or continue on
let mapMatch;
if (isPlain(impt)) {
mapMatch = getMapMatch(impt, outMappings);
} else {
mapMatch =
getMapMatch(
(impt = rebase(impt, this.mapUrl, this.rootUrl)),
outMappings
) ||
(this.rootUrl &&
getMapMatch(
(impt = rebase(impt, this.mapUrl, null)),
outMappings
)) ||
undefined;
}
if (
mapMatch &&
impt.slice(mapMatch.length) ===
resolve(target, this.mapUrl, this.rootUrl).slice(
resolve(outMappings[mapMatch], this.mapUrl, this.rootUrl).length
)
) {
continue;
}
let newbase = false;
const targetParts = mappings[impt].split("/");
const imptParts = impt.split("/");
for (let j = imptParts.length - 1; j > 0; j--) {
const subpath = imptParts.slice(j).join("/");
const subpathTarget = targetParts
.slice(targetParts.length - (imptParts.length - j))
.join("/");
if (subpath !== subpathTarget) {
outMappings[impt] = mappings[impt];
break;
}
const base = imptParts.slice(0, j).join("/") + "/";
if (outMappings[base]) continue;
const baseTarget =
targetParts
.slice(0, targetParts.length - (imptParts.length - j))
.join("/") + "/";
// Dedupe existing mappings against the new base to remove them
// And if we dont dedupe against anything then dont perform this basing
for (let impt of Object.keys(outMappings)) {
const target = outMappings[impt];
let matches = false;
if (isPlain(impt)) {
matches = impt.startsWith(base);
} else {
matches =
(impt = rebase(impt, this.mapUrl, this.rootUrl)).startsWith(
base
) ||
(impt = rebase(impt, this.mapUrl, this.rootUrl)).startsWith(
base
);
}
if (
matches &&
impt.slice(base.length) ===
resolve(target, this.mapUrl, this.rootUrl).slice(
resolve(baseTarget, this.mapUrl, this.rootUrl).length
)
) {
newbase = true;
delete outMappings[impt];
}
}
if (newbase) {
outMappings[base] = baseTarget;
break;
}
}
if (!newbase) outMappings[impt] = target;
}
return outMappings;
};
// Only applies for scopes since "imports" are generally treated as
// an authoritative entry point list
for (const scope of Object.keys(this.scopes)) {
this.scopes[scope] = combineSubpathMappings(this.scopes[scope]);
}
return this;
}
/**
* Groups the import map scopes to shared URLs to reduce duplicate mappings.
*
* For two given scopes, "https://site.com/x/" and "https://site.com/y/",
* a single scope will be constructed for "https://site.com/" including
* their shared mappings, only retaining the scopes if they have differences.
*
* In the case where the scope is on the same origin as the mapUrl, the grouped
* scope is determined based on determining the common baseline over all local scopes
*
* @returns ImportMap for chaining
*/
flatten() {
// First, determine the common base for the local mappings if any
let localScopemapUrl: string | null = null;
for (const scope of Object.keys(this.scopes)) {
const resolvedScope = resolve(scope, this.mapUrl, this.rootUrl);
if (isURL(resolvedScope)) {
const scopeUrl = new URL(resolvedScope);
if (sameOrigin(scopeUrl, this.mapUrl)) {
if (!localScopemapUrl) localScopemapUrl = scopeUrl.href;
else
localScopemapUrl = getCommonBase(scopeUrl.href, localScopemapUrl);
}
} else {
if (!localScopemapUrl) localScopemapUrl = resolvedScope;
else localScopemapUrl = getCommonBase(resolvedScope, localScopemapUrl);
}
}
// for each scope, update its mappings to be in the shared base where possible
const relativeLocalScopemapUrl = localScopemapUrl
? rebase(localScopemapUrl, this.mapUrl, this.rootUrl)
: null;
for (const scope of Object.keys(this.scopes)) {
const scopeImports = this.scopes[scope];
let scopemapUrl: string;
const resolvedScope = resolve(scope, this.mapUrl, this.rootUrl);
if (isURL(resolvedScope)) {
const scopeUrl = new URL(resolvedScope);
if (sameOrigin(scopeUrl, this.mapUrl)) {
scopemapUrl = relativeLocalScopemapUrl;
} else {
scopemapUrl =
scopeUrl.protocol +
"//" +
scopeUrl.hostname +
(scopeUrl.port ? ":" + scopeUrl.port : "") +
"/";
}
} else {
scopemapUrl = relativeLocalScopemapUrl;
}
let scopeBase: Record<string, string> | null =
this.scopes[scopemapUrl] || Object.create(null);
if (scopeBase === scopeImports) scopeBase = null;
let flattenedAll = true;
for (const name of Object.keys(scopeImports)) {
const target = scopeImports[name];
if (
this.imports[name] &&
resolve(this.imports[name], this.mapUrl, this.rootUrl) ===
resolve(target, this.mapUrl, this.rootUrl)
) {
delete scopeImports[name];
} else if (
scopeBase &&
(!scopeBase[name] ||
resolve(scopeBase[name], this.mapUrl, this.rootUrl) ===
resolve(target, this.mapUrl, this.rootUrl))
) {
scopeBase[name] = rebase(target, this.mapUrl, this.rootUrl);
delete scopeImports[name];
this.scopes[<string>scopemapUrl] = alphabetize(scopeBase);
} else {
flattenedAll = false;
}
}
if (flattenedAll) delete this.scopes[scope];
}
return this;
}
/**
* Rebase the entire import map to a new mapUrl and rootUrl
*
* If the rootUrl is not provided, it will remain null if it was
* already set to null.
*
* Otherwise, just like the constructor options, the rootUrl
* will default to the mapUrl base if it is an http: or https:
* scheme URL, and null otherwise keeping absolute URLs entirely
* in-tact.
*
* @param mapUrl The new map URL to use
* @param rootUrl The new root URL to use
* @returns ImportMap for chaining
*/
rebase(mapUrl: URL | string = this.mapUrl, rootUrl?: URL | string | null) {
if (typeof mapUrl === "string") mapUrl = new URL(mapUrl);
if (rootUrl === undefined) {
if (mapUrl.href === this.mapUrl.href) rootUrl = this.rootUrl;
else
rootUrl =
this.rootUrl === null ||
(mapUrl.protocol !== "https:" && mapUrl.protocol !== "http:")
? null
: new URL("/", mapUrl);
} else if (typeof rootUrl === "string") rootUrl = new URL(rootUrl);
let changedImportProps = false;
for (const impt of Object.keys(this.imports)) {
const target = this.imports[impt];
this.imports[impt] = rebase(
resolve(target, this.mapUrl, this.rootUrl),
mapUrl,
rootUrl
);
if (!isPlain(impt)) {
const newImpt = rebase(
resolve(impt, this.mapUrl, this.rootUrl),
mapUrl,
rootUrl
);
if (newImpt !== impt) {
changedImportProps = true;
this.imports[newImpt] = this.imports[impt];
delete this.imports[impt];
}
}
}
if (changedImportProps) this.imports = alphabetize(this.imports);
let changedScopeProps = false;
for (const scope of Object.keys(this.scopes)) {
const scopeImports = this.scopes[scope];
let changedScopeImportProps = false;
for (const impt of Object.keys(scopeImports)) {
const target = scopeImports[impt];
scopeImports[impt] = rebase(
resolve(target, this.mapUrl, this.rootUrl),
mapUrl,
rootUrl
);
if (!isPlain(impt)) {
const newName = rebase(
resolve(impt, this.mapUrl, this.rootUrl),
mapUrl,
rootUrl
);
if (newName !== impt) {
changedScopeImportProps = true;
scopeImports[newName] = scopeImports[impt];
delete scopeImports[impt];
}
}
}
if (changedScopeImportProps)
this.scopes[scope] = alphabetize(scopeImports);
const newScope = rebase(
resolve(scope, this.mapUrl, this.rootUrl),
mapUrl,
rootUrl
);
if (scope !== newScope) {
changedScopeProps = true;
delete this.scopes[scope];
this.scopes[newScope] = scopeImports;
}
}
if (changedScopeProps) this.scopes = alphabetize(this.scopes);
let changedIntegrityProps = false;
for (const target of Object.keys(this.integrity)) {
const newTarget = rebase(
resolve(target, this.mapUrl, this.rootUrl),
mapUrl,
rootUrl
);
if (target !== newTarget) {
this.integrity[newTarget] = this.integrity[target];
delete this.integrity[target];
changedIntegrityProps = true;
}
}
if (changedIntegrityProps) this.integrity = alphabetize(this.integrity);
this.mapUrl = mapUrl;
this.rootUrl = rootUrl;
return this;
}
/**
* Perform a module resolution against the import map
*
* @param specifier Specifier to resolve
* @param parentUrl Parent URL to resolve against
* @returns Resolved URL string
*/
resolve(specifier: string, parentUrl: string | URL = this.mapUrl): string {
if (typeof parentUrl !== "string") parentUrl = parentUrl.toString();
parentUrl = resolve(parentUrl, this.mapUrl, this.rootUrl);
let specifierUrl: URL | undefined;
if (!isPlain(specifier)) {
specifierUrl = new URL(specifier, parentUrl);
specifier = specifierUrl.href;
}
const scopeMatches = getScopeMatches(
parentUrl,
this.scopes,
this.mapUrl,
this.rootUrl
);
for (const [scope] of scopeMatches) {
let mapMatch = getMapMatch(specifier, this.scopes[scope]);
if (!mapMatch && specifierUrl) {
mapMatch =
getMapMatch(
(specifier = rebase(specifier, this.mapUrl, this.rootUrl)),
this.scopes[scope]
) ||
(this.rootUrl &&
getMapMatch(
(specifier = rebase(specifier, this.mapUrl, null)),
this.scopes[scope]
)) ||
undefined;
}
if (mapMatch) {
const target = this.scopes[scope][mapMatch];
return resolve(
target + specifier.slice(mapMatch.length),
this.mapUrl,
this.rootUrl
);
}
}
let mapMatch = getMapMatch(specifier, this.imports);
if (!mapMatch && specifierUrl) {
mapMatch =
getMapMatch(
(specifier = rebase(specifier, this.mapUrl, this.rootUrl)),
this.imports
) ||
(this.rootUrl &&
getMapMatch(
(specifier = rebase(specifier, this.mapUrl, null)),
this.imports
)) ||
undefined;
}
if (mapMatch) {
const target = this.imports[mapMatch];
return resolve(
target + specifier.slice(mapMatch.length),
this.mapUrl,
this.rootUrl
);
}
if (specifierUrl) return specifierUrl.href;
throw new Error(`Unable to resolve ${specifier} in ${parentUrl}`);
}
/**
* Get the import map JSON data
*
* @returns Import map data
*/
toJSON(): IImportMap {
const obj: any = {};
if (Object.keys(this.imports).length) obj.imports = this.imports;
if (Object.keys(this.scopes).length) obj.scopes = this.scopes;
if (Object.keys(this.integrity).length) obj.integrity = this.integrity;
return JSON.parse(JSON.stringify(obj));
}
}
export function getScopeMatches(
parentUrl: string,
scopes: Record<string, Record<string, string>>,
mapUrl: URL,
rootUrl?: URL
): [string, string][] {
let scopeCandidates = Object.keys(scopes).map((scope) => [
scope,
resolve(scope, mapUrl, rootUrl),
]);
scopeCandidates = scopeCandidates.sort(([, matchA], [, matchB]) =>
matchA.length < matchB.length ? 1 : -1
);
return scopeCandidates.filter(([, scopeUrl]) => {
return (
scopeUrl === parentUrl ||
(scopeUrl.endsWith("/") && parentUrl.startsWith(scopeUrl))
);
}) as [string, string][];
}
export function getMapMatch<T = any>(
specifier: string,
map: Record<string, T>
): string | undefined {
if (specifier in map) return specifier;
let curMatch;
for (const match of Object.keys(map)) {
const wildcard = match.endsWith("*");
if (!match.endsWith("/") && !wildcard) continue;
if (specifier.startsWith(wildcard ? match.slice(0, -1) : match)) {
if (!curMatch || match.length > curMatch.length) curMatch = match;
}
}
return curMatch;
}