-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.js
63 lines (58 loc) · 2.14 KB
/
module.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
const maps = new Map();
const cache = new Map();
export const URL_PREFIXES = ['http:', 'https:', 'blob:', 'data:', 'file:'];
export const PATH_PREFIXES = ['/', './', '../'];
export const isString = str => typeof str === 'string';
export const isURL = str => isString(str) && URL_PREFIXES.some(pre => str.startsWith(pre));
export const isPath = str => isString(str) && PATH_PREFIXES.some(pre => str.startsWith(pre));
export const isBare = str => isString(str)
&& ! [...URL_PREFIXES, ...PATH_PREFIXES].some(pre => str.startsWith(pre));
export function getImportmap() {
if (maps.has(document)) {
return maps.get(document);
} else {
const script = document.head.querySelector('script[type="importmap"]');
if (script instanceof HTMLScriptElement) {
const { imports = {}, scope = {} } = JSON.parse(script.textContent);
return { imports, scope };
} else {
return { imports: {}, scope: {} };
}
}
}
export function resolveModule(specifier, { imports } = getImportmap()) {
if (specifier instanceof URL) {
return specifier.href;
} else if (typeof specifier !== 'string') {
throw new TypeError('Module specifier must be a string or URL.');
} else if (cache.has(specifier)) {
return cache.get(specifier);
} else if (imports.hasOwnProperty(specifier)) {
return imports[specifier];
} else if (URL.canParse(specifier) || isURL(specifier)) {
return specifier;
} else if (specifier.includes('/')) {
let found = false;
const longest = Object.keys(imports)
.filter(spec => spec.endsWith('/'))
.reduce((match, spec) => {
if (! found && specifier.startsWith(spec)) {
found = true;
return spec;
} else if (found && spec.length > match && specifier.startsWith(spec)) {
return spec;
} else {
return match;
}
}, null);
if (! found) {
throw new TypeError(`Resolution of specifier “${specifier}” was blocked by a null entry.`);
} else if (found) {
const resolved = new URL(specifier.replace(longest, imports[longest]), document.baseURI).href;
cache.set(specifier, resolved);
return resolved;
}
} else {
throw new TypeError(`Resolution of specifier “${specifier}” was blocked by a null entry.`);
}
}