@wordpress/interactivity-router
Version:
Package that exposes state and actions from the `core/router` store, part of the Interactivity API.
185 lines (184 loc) • 5.49 kB
JavaScript
// packages/interactivity-router/src/assets/dynamic-importmap/resolver.ts
var backslashRegEx = /\\/g;
function isURL(url) {
if (url.indexOf(":") === -1) {
return false;
}
try {
new URL(url);
return true;
} catch {
return false;
}
}
function resolveIfNotPlainOrUrl(relUrl, parentUrl) {
const hIdx = parentUrl.indexOf("#"), qIdx = parentUrl.indexOf("?");
if (hIdx + qIdx > -2) {
parentUrl = parentUrl.slice(
0,
// eslint-disable-next-line no-nested-ternary
hIdx === -1 ? qIdx : qIdx === -1 || qIdx > hIdx ? hIdx : qIdx
);
}
if (relUrl.indexOf("\\") !== -1) {
relUrl = relUrl.replace(backslashRegEx, "/");
}
if (relUrl[0] === "/" && relUrl[1] === "/") {
return parentUrl.slice(0, parentUrl.indexOf(":") + 1) + relUrl;
} else if (relUrl[0] === "." && (relUrl[1] === "/" || relUrl[1] === "." && (relUrl[2] === "/" || relUrl.length === 2 && (relUrl += "/")) || relUrl.length === 1 && (relUrl += "/")) || relUrl[0] === "/") {
const parentProtocol = parentUrl.slice(
0,
parentUrl.indexOf(":") + 1
);
let pathname;
if (parentUrl[parentProtocol.length + 1] === "/") {
if (parentProtocol !== "file:") {
pathname = parentUrl.slice(parentProtocol.length + 2);
pathname = pathname.slice(pathname.indexOf("/") + 1);
} else {
pathname = parentUrl.slice(8);
}
} else {
pathname = parentUrl.slice(
parentProtocol.length + (parentUrl[parentProtocol.length] === "/")
);
}
if (relUrl[0] === "/") {
return parentUrl.slice(0, parentUrl.length - pathname.length - 1) + relUrl;
}
const segmented = pathname.slice(0, pathname.lastIndexOf("/") + 1) + relUrl;
const output = [];
let segmentIndex = -1;
for (let i = 0; i < segmented.length; i++) {
if (segmentIndex !== -1) {
if (segmented[i] === "/") {
output.push(segmented.slice(segmentIndex, i + 1));
segmentIndex = -1;
}
continue;
} else if (segmented[i] === ".") {
if (segmented[i + 1] === "." && (segmented[i + 2] === "/" || i + 2 === segmented.length)) {
output.pop();
i += 2;
continue;
} else if (segmented[i + 1] === "/" || i + 1 === segmented.length) {
i += 1;
continue;
}
}
while (segmented[i] === "/") {
i++;
}
segmentIndex = i;
}
if (segmentIndex !== -1) {
output.push(segmented.slice(segmentIndex));
}
return parentUrl.slice(0, parentUrl.length - pathname.length) + output.join("");
}
}
function resolveUrl(relUrl, parentUrl) {
return resolveIfNotPlainOrUrl(relUrl, parentUrl) || (isURL(relUrl) ? relUrl : resolveIfNotPlainOrUrl("./" + relUrl, parentUrl));
}
function getMatch(path, matchObj) {
if (matchObj[path]) {
return path;
}
let sepIndex = path.length;
do {
const segment = path.slice(0, sepIndex + 1);
if (segment in matchObj) {
return segment;
}
} while ((sepIndex = path.lastIndexOf("/", sepIndex - 1)) !== -1);
}
function applyPackages(id, packages) {
const pkgName = getMatch(id, packages);
if (pkgName) {
const pkg = packages[pkgName];
if (pkg === null) {
return;
}
return pkg + id.slice(pkgName.length);
}
}
function resolveImportMap(importMap2, resolvedOrPlain, parentUrl) {
let scopeUrl = parentUrl && getMatch(parentUrl, importMap2.scopes);
while (scopeUrl) {
const packageResolution = applyPackages(
resolvedOrPlain,
importMap2.scopes[scopeUrl]
);
if (packageResolution) {
return packageResolution;
}
scopeUrl = getMatch(
scopeUrl.slice(0, scopeUrl.lastIndexOf("/")),
importMap2.scopes
);
}
return applyPackages(resolvedOrPlain, importMap2.imports) || resolvedOrPlain.indexOf(":") !== -1 && resolvedOrPlain;
}
function resolveAndComposePackages(packages, outPackages, baseUrl2, parentMap) {
for (const p in packages) {
const resolvedLhs = resolveIfNotPlainOrUrl(p, baseUrl2) || p;
const target = packages[p];
if (typeof target !== "string") {
continue;
}
const mapped = resolveImportMap(
parentMap,
resolveIfNotPlainOrUrl(target, baseUrl2) || target,
baseUrl2
);
if (mapped) {
outPackages[resolvedLhs] = mapped;
continue;
}
}
}
function resolveAndComposeImportMap(json, baseUrl2, parentMap) {
const outMap = {
imports: Object.assign({}, parentMap.imports),
scopes: Object.assign({}, parentMap.scopes)
};
if (json.imports) {
resolveAndComposePackages(
json.imports,
outMap.imports,
baseUrl2,
parentMap
);
}
if (json.scopes) {
for (const s in json.scopes) {
const resolvedScope = resolveUrl(s, baseUrl2);
resolveAndComposePackages(
json.scopes[s],
outMap.scopes[resolvedScope] || (outMap.scopes[resolvedScope] = {}),
baseUrl2,
parentMap
);
}
}
return outMap;
}
var importMap = { imports: {}, scopes: {} };
var baseUrl = document.baseURI;
var pageBaseUrl = baseUrl;
function addImportMap(importMapIn) {
importMap = resolveAndComposeImportMap(
importMapIn,
pageBaseUrl,
importMap
);
}
function resolve(id, parentUrl) {
const urlResolved = resolveIfNotPlainOrUrl(id, parentUrl);
return resolveImportMap(importMap, urlResolved || id, parentUrl) || id;
}
export {
addImportMap,
resolve
};
//# sourceMappingURL=resolver.mjs.map