@jspm/import-map
Version:
Package Import Map Utility
73 lines • 2.67 kB
JavaScript
var _a;
export let baseUrl;
// @ts-ignore
if (typeof Deno !== 'undefined') {
// @ts-ignore
baseUrl = new URL('file://' + Deno.cwd() + '/');
}
else if (typeof process !== 'undefined' && ((_a = process.versions) === null || _a === void 0 ? void 0 : _a.node)) {
baseUrl = new URL('file://' + process.cwd() + '/');
}
else if (typeof document !== 'undefined') {
const baseEl = document.querySelector('base[href]');
if (baseEl)
baseUrl = new URL(baseEl.href + (baseEl.href.endsWith('/') ? '' : '/'));
else if (typeof location !== 'undefined')
baseUrl = new URL('../', new URL(location.href));
}
export function importedFrom(parentUrl) {
if (!parentUrl)
return '';
return ` imported from ${parentUrl}`;
}
function matchesRoot(url, baseUrl) {
return url.protocol === baseUrl.protocol && url.host === baseUrl.host && url.port === baseUrl.port && url.username === baseUrl.username && url.password === baseUrl.password;
}
export function rebase(url, baseUrl = new URL('/', url), outBase = false) {
if (typeof outBase === 'boolean')
outBase = outBase ? '/' : './';
else if (!outBase.endsWith('/'))
outBase += '/';
baseUrl.search = baseUrl.hash = '';
if (!baseUrl.pathname.endsWith('/'))
baseUrl.pathname += '/';
const href = url.href;
const baseHref = baseUrl.href;
if (href.startsWith(baseHref))
return outBase + href.slice(baseHref.length);
if (!matchesRoot(url, baseUrl))
return url.href;
const baseUrlPath = baseUrl.pathname;
const urlPath = url.pathname;
const minLen = Math.min(baseUrlPath.length, urlPath.length);
let sharedBaseIndex = -1;
for (let i = 0; i < minLen; i++) {
if (baseUrlPath[i] !== urlPath[i])
break;
if (urlPath[i] === '/')
sharedBaseIndex = i;
}
return '../'.repeat(baseUrlPath.slice(sharedBaseIndex + 1).split('/').length - 1) + urlPath.slice(sharedBaseIndex + 1) + url.search + url.hash;
}
export function isURL(specifier) {
try {
if (specifier[0] === '#')
return false;
new URL(specifier);
}
catch {
return false;
}
return true;
}
export function isPlain(specifier) {
return !isRelative(specifier) && !isURL(specifier);
}
export function isRelative(specifier) {
return specifier.startsWith('./') || specifier.startsWith('../') || specifier.startsWith('/');
}
export function urlToNiceStr(url) {
if (url.startsWith(baseUrl.href))
return './' + url.slice(baseUrl.href.length);
}
//# sourceMappingURL=url.js.map