@jspm/import-map
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -1,2 +0,2 @@ | ||
var ref; | ||
var _process_versions; | ||
let baseUrl; | ||
@@ -7,3 +7,3 @@ // @ts-ignore | ||
baseUrl = new URL('file://' + Deno.cwd() + '/'); | ||
} else if (typeof process !== 'undefined' && ((ref = process.versions) === null || ref === void 0 ? void 0 : ref.node)) { | ||
} else if (typeof process !== 'undefined' && ((_process_versions = process.versions) === null || _process_versions === void 0 ? void 0 : _process_versions.node)) { | ||
baseUrl = new URL('file://' + process.cwd() + '/'); | ||
@@ -40,2 +40,5 @@ } else if (typeof document !== 'undefined') { | ||
if (rootUrl && resolved.href.startsWith(rootUrl.href)) return resolved.href.slice(rootUrl.href.length - 1); | ||
if (rootUrl && rootUrl.href.startsWith(resolved.href)) { | ||
return '/' + relative(resolved, rootUrl); | ||
} | ||
if (sameOrigin(resolved, baseUrl)) return relative(resolved, baseUrl); | ||
@@ -145,3 +148,3 @@ return resolved.href; | ||
const scopeImports = this.scopes[scope]; | ||
const scopeUrl = new URL(scope, this.mapUrl).href; | ||
const scopeUrl = resolve(scope, this.mapUrl, this.rootUrl); | ||
if (replaceSubpaths && scopeUrl.startsWith(url) || scopeUrl === url) { | ||
@@ -152,5 +155,5 @@ const newScope = newRelPkgUrl + scopeUrl.slice(url.length); | ||
} | ||
for (const impt1 of Object.keys(scopeImports)){ | ||
const target1 = scopeImports[impt1]; | ||
if (replaceSubpaths && target1.startsWith(url) || target1 === url) scopeImports[impt1] = newRelPkgUrl + target1.slice(url.length); | ||
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); | ||
} | ||
@@ -201,13 +204,13 @@ } | ||
// And if we dont dedupe against anything then dont perform this basing | ||
for (let impt1 of Object.keys(outMappings)){ | ||
const target1 = outMappings[impt1]; | ||
for (let impt of Object.keys(outMappings)){ | ||
const target = outMappings[impt]; | ||
let matches = false; | ||
if (isPlain(impt1)) { | ||
matches = impt1.startsWith(base); | ||
if (isPlain(impt)) { | ||
matches = impt.startsWith(base); | ||
} else { | ||
matches = (impt1 = rebase(impt1, this.mapUrl, this.rootUrl)).startsWith(base) || (impt1 = rebase(impt1, this.mapUrl, this.rootUrl)).startsWith(base); | ||
matches = (impt = rebase(impt, this.mapUrl, this.rootUrl)).startsWith(base) || (impt = rebase(impt, this.mapUrl, this.rootUrl)).startsWith(base); | ||
} | ||
if (matches && impt1.slice(base.length) === resolve(target1, this.mapUrl, this.rootUrl).slice(resolve(baseTarget, this.mapUrl, this.rootUrl).length)) { | ||
if (matches && impt.slice(base.length) === resolve(target, this.mapUrl, this.rootUrl).slice(resolve(baseTarget, this.mapUrl, this.rootUrl).length)) { | ||
newbase = true; | ||
delete outMappings[impt1]; | ||
delete outMappings[impt]; | ||
} | ||
@@ -246,3 +249,3 @@ } | ||
for (const scope of Object.keys(this.scopes)){ | ||
const scopeUrl = new URL(scope, this.mapUrl); | ||
const scopeUrl = new URL(resolve(scope, this.mapUrl, this.rootUrl)); | ||
if (scopeUrl.protocol === this.mapUrl.protocol && scopeUrl.hostname === this.mapUrl.hostname && scopeUrl.port === this.mapUrl.port) { | ||
@@ -255,10 +258,10 @@ if (!localScopemapUrl) localScopemapUrl = scopeUrl.href; | ||
// for each scope, update its mappings to be in the shared base where possible | ||
for (const scope1 of Object.keys(this.scopes)){ | ||
const scopeImports = this.scopes[scope1]; | ||
const scopeUrl1 = new URL(scope1, this.mapUrl); | ||
for (const scope of Object.keys(this.scopes)){ | ||
const scopeImports = this.scopes[scope]; | ||
const scopeUrl = new URL(resolve(scope, this.mapUrl, this.rootUrl)); | ||
let scopemapUrl; | ||
if (sameOrigin(scopeUrl1, this.mapUrl)) { | ||
if (sameOrigin(scopeUrl, this.mapUrl)) { | ||
scopemapUrl = relativeLocalScopemapUrl; | ||
} else { | ||
scopemapUrl = scopeUrl1.protocol + '//' + scopeUrl1.hostname + (scopeUrl1.port ? ':' + scopeUrl1.port : '') + '/'; | ||
scopemapUrl = scopeUrl.protocol + '//' + scopeUrl.hostname + (scopeUrl.port ? ':' + scopeUrl.port : '') + '/'; | ||
} | ||
@@ -270,5 +273,5 @@ let scopeBase = this.scopes[scopemapUrl] || Object.create(null); | ||
const target = scopeImports[name]; | ||
if (this.imports[name] && new URL(this.imports[name], this.mapUrl).href === new URL(target, this.mapUrl).href) { | ||
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] || new URL(scopeBase[name], this.mapUrl).href === new URL(target, this.mapUrl).href)) { | ||
} 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); | ||
@@ -281,3 +284,3 @@ delete scopeImports[name]; | ||
} | ||
if (flattenedAll) delete this.scopes[scope1]; | ||
if (flattenedAll) delete this.scopes[scope]; | ||
} | ||
@@ -324,11 +327,11 @@ return this; | ||
let changedScopeImportProps = false; | ||
for (const impt1 of Object.keys(scopeImports)){ | ||
const target1 = scopeImports[impt1]; | ||
scopeImports[impt1] = rebase(resolve(target1, this.mapUrl, this.rootUrl), mapUrl, rootUrl); | ||
if (!isPlain(impt1)) { | ||
const newName = rebase(resolve(impt1, this.mapUrl, this.rootUrl), mapUrl, rootUrl); | ||
if (newName !== impt1) { | ||
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[impt1]; | ||
delete scopeImports[impt1]; | ||
scopeImports[newName] = scopeImports[impt]; | ||
delete scopeImports[impt]; | ||
} | ||
@@ -364,3 +367,3 @@ } | ||
} | ||
const scopeMatches = getScopeMatches(parentUrl, this.scopes, this.mapUrl); | ||
const scopeMatches = getScopeMatches(parentUrl, this.scopes, this.mapUrl, this.rootUrl); | ||
for (const [scope] of scopeMatches){ | ||
@@ -376,9 +379,9 @@ let mapMatch = getMapMatch(specifier, this.scopes[scope]); | ||
} | ||
let mapMatch1 = getMapMatch(specifier, this.imports); | ||
if (!mapMatch1 && specifierUrl) { | ||
mapMatch1 = getMapMatch(specifier = rebase(specifier, this.mapUrl, this.rootUrl), this.imports) || this.rootUrl && getMapMatch(specifier = rebase(specifier, this.mapUrl, null), this.imports) || undefined; | ||
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 (mapMatch1) { | ||
const target1 = this.imports[mapMatch1]; | ||
return resolve(target1 + specifier.slice(mapMatch1.length), this.mapUrl, this.rootUrl); | ||
if (mapMatch) { | ||
const target = this.imports[mapMatch]; | ||
return resolve(target + specifier.slice(mapMatch.length), this.mapUrl, this.rootUrl); | ||
} | ||
@@ -405,3 +408,3 @@ if (specifierUrl) return specifierUrl.href; | ||
this.scopes = Object.create(null); | ||
let { map , mapUrl =baseUrl , rootUrl } = opts instanceof URL || typeof opts === 'string' || opts === undefined ? { | ||
let { map , mapUrl =baseUrl , rootUrl } = opts instanceof URL || typeof opts === 'string' || typeof opts === 'undefined' ? { | ||
mapUrl: opts, | ||
@@ -419,6 +422,6 @@ map: undefined, | ||
} | ||
function getScopeMatches(parentUrl, scopes, mapUrl) { | ||
function getScopeMatches(parentUrl, scopes, mapUrl, rootUrl) { | ||
let scopeCandidates = Object.keys(scopes).map((scope)=>[ | ||
scope, | ||
new URL(scope, mapUrl).href | ||
resolve(scope, mapUrl, rootUrl) | ||
]); | ||
@@ -425,0 +428,0 @@ scopeCandidates = scopeCandidates.sort(([, matchA], [, matchB])=>matchA.length < matchB.length ? 1 : -1); |
export function alphabetize(obj) { | ||
const out = {}; | ||
for (const key of Object.keys(obj).sort())out[key] = obj[key]; | ||
for (const key of Object.keys(obj).sort()) | ||
out[key] = obj[key]; | ||
return out; | ||
} | ||
//# sourceMappingURL=alphabetize.js.map |
@@ -8,3 +8,3 @@ declare global { | ||
export declare function importedFrom(parentUrl?: string | URL): string; | ||
export declare function rebase(url: URL, baseUrl?: URL, outBase?: boolean | string): string; | ||
export declare function relativeUrl(url: URL, baseUrl: URL, absolute?: boolean): string; | ||
export declare function isURL(specifier: string): boolean; | ||
@@ -11,0 +11,0 @@ export declare function isPlain(specifier: string): boolean; |
@@ -1,2 +0,1 @@ | ||
var ref; | ||
export let baseUrl; | ||
@@ -7,34 +6,30 @@ // @ts-ignore | ||
baseUrl = new URL('file://' + Deno.cwd() + '/'); | ||
} else if (typeof process !== 'undefined' && ((ref = process.versions) === null || ref === void 0 ? void 0 : ref.node)) { | ||
} | ||
else if (typeof process !== 'undefined' && process.versions.node) { | ||
baseUrl = new URL('file://' + process.cwd() + '/'); | ||
} else if (typeof document !== 'undefined') { | ||
} | ||
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)); | ||
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 ''; | ||
if (!parentUrl) | ||
return ''; | ||
return ` imported from ${parentUrl}`; | ||
} | ||
export function getCommonBase(urlA, urlB) { | ||
if (urlA.startsWith(urlB)) return urlB; | ||
if (urlB.startsWith(urlA)) return urlA; | ||
const aSegments = urlA.split('/'); | ||
const bSegments = urlB.split('/'); | ||
let i = 0; | ||
while(aSegments[i] === bSegments[i])i++; | ||
return aSegments.slice(0, i).join('/') + '/'; | ||
} | ||
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 += '/'; | ||
export function relativeUrl(url, baseUrl, absolute = false) { | ||
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 baseUrlHref = baseUrl.href; | ||
if (href.startsWith(baseUrlHref)) | ||
return (absolute ? '/' : './') + href.slice(baseUrlHref.length); | ||
if (!matchesRoot(url, baseUrl)) | ||
return url.href; | ||
if (absolute) | ||
return url.href; | ||
const baseUrlPath = baseUrl.pathname; | ||
@@ -44,5 +39,7 @@ const urlPath = url.pathname; | ||
let sharedBaseIndex = -1; | ||
for(let i = 0; i < minLen; i++){ | ||
if (baseUrlPath[i] !== urlPath[i]) break; | ||
if (urlPath[i] === '/') sharedBaseIndex = i; | ||
for (let i = 0; i < minLen; i++) { | ||
if (baseUrlPath[i] !== urlPath[i]) | ||
break; | ||
if (urlPath[i] === '/') | ||
sharedBaseIndex = i; | ||
} | ||
@@ -53,5 +50,7 @@ return '../'.repeat(baseUrlPath.slice(sharedBaseIndex + 1).split('/').length - 1) + urlPath.slice(sharedBaseIndex + 1) + url.search + url.hash; | ||
try { | ||
if (specifier[0] === '#') return false; | ||
if (specifier[0] === '#') | ||
return false; | ||
new URL(specifier); | ||
} catch (e) { | ||
} | ||
catch { | ||
return false; | ||
@@ -68,6 +67,5 @@ } | ||
export function urlToNiceStr(url) { | ||
if (url.startsWith(baseUrl.href)) return './' + url.slice(baseUrl.href.length); | ||
if (url.startsWith(baseUrl.href)) | ||
return './' + url.slice(baseUrl.href.length); | ||
} | ||
//# sourceMappingURL=url.js.map |
@@ -0,39 +1,43 @@ | ||
export interface ConditionalTarget { | ||
[env: string]: ConditionalTarget | string | null; | ||
} | ||
export interface IImportMap { | ||
imports?: Record<string, string>; | ||
imports?: Record<string, string | null>; | ||
scopes?: { | ||
[scope: string]: Record<string, string>; | ||
[scope: string]: Record<string, string | null>; | ||
}; | ||
} | ||
export interface IConditionalImportMap { | ||
imports?: Record<string, string | ConditionalTarget | null>; | ||
scopes?: { | ||
[scope: string]: Record<string, string | ConditionalTarget | null>; | ||
}; | ||
} | ||
export declare class ImportMap { | ||
imports: Record<string, string>; | ||
scopes: Record<string, Record<string, string>>; | ||
mapUrl: URL; | ||
rootUrl: URL | null; | ||
constructor(mapUrl: string | URL, rootUrl?: string | URL | null); | ||
imports: Record<string, string | ConditionalTarget | null>; | ||
scopes: Record<string, Record<string, string | ConditionalTarget | null>>; | ||
baseUrl: URL; | ||
constructor(mapBaseUrl: string | URL, initialMap?: IConditionalImportMap); | ||
clone(): ImportMap; | ||
extend(map: IImportMap | IImportMap, overrideScopes?: boolean): this; | ||
extend(map: IImportMap | IConditionalImportMap, overrideScopes?: boolean): this; | ||
sort(): void; | ||
set(name: string, target: string, parent?: string): void; | ||
set(name: string, target: string | null | ConditionalTarget, parent?: string): void; | ||
replace(url: string, newUrl: string): this; | ||
combineSubpaths(): void; | ||
flatten(): this; | ||
rebase(newBaseUrl?: string, abs?: boolean): this; | ||
/** | ||
* Groups the import map scopes to shared URLs to reduce duplicate mappings. | ||
* | ||
* @param baseScope {String | URL} | ||
* | ||
* 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. | ||
* | ||
* In the case where the scope is on the same origin as the mapUrl, the grouped | ||
* root will never backtrack below the mapUrl, unless specifying the baseScope | ||
* option to permit a custom backtracking base. | ||
* | ||
* Narrow all mappings to the given conditional environment constraints | ||
*/ | ||
flatten(baseScope?: URL | string): this; | ||
rebase(newmapUrl?: string, absRoot?: string | boolean): this; | ||
resolve(specifier: string, parentUrl?: URL | string): string; | ||
setEnv(env: string[] | EnvConstraints): this; | ||
resolve(specifier: string, parentUrl?: URL | string, env?: string[] | EnvConstraints): string | ConditionalTarget | null; | ||
toJSON(): any; | ||
} | ||
export declare function getScopeMatches(parentUrl: URL, scopes: Record<string, Record<string, string>>, mapUrl: URL): [string, string][]; | ||
export declare function getScopeMatches(parentUrl: URL, scopes: Record<string, Record<string, string | ConditionalTarget | null>>, baseUrl: URL): [string, string][]; | ||
export declare function getMapMatch<T = any>(specifier: string, map: Record<string, T>): string | undefined; | ||
export declare type EnvConstraints = { | ||
include?: string[]; | ||
exclude?: string[]; | ||
covers?: string[][]; | ||
}; | ||
export declare function resolveConditional(target: string | ConditionalTarget | null, env?: string[] | EnvConstraints): string | ConditionalTarget; |
@@ -70,3 +70,3 @@ import { baseUrl, rebase, isPlain, isURL, getCommonBase, resolve, sameOrigin } from './url.js'; | ||
const scopeImports = this.scopes[scope]; | ||
const scopeUrl = new URL(scope, this.mapUrl).href; | ||
const scopeUrl = resolve(scope, this.mapUrl, this.rootUrl); | ||
if (replaceSubpaths && scopeUrl.startsWith(url) || scopeUrl === url) { | ||
@@ -77,5 +77,5 @@ const newScope = newRelPkgUrl + scopeUrl.slice(url.length); | ||
} | ||
for (const impt1 of Object.keys(scopeImports)){ | ||
const target1 = scopeImports[impt1]; | ||
if (replaceSubpaths && target1.startsWith(url) || target1 === url) scopeImports[impt1] = newRelPkgUrl + target1.slice(url.length); | ||
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); | ||
} | ||
@@ -126,13 +126,13 @@ } | ||
// And if we dont dedupe against anything then dont perform this basing | ||
for (let impt1 of Object.keys(outMappings)){ | ||
const target1 = outMappings[impt1]; | ||
for (let impt of Object.keys(outMappings)){ | ||
const target = outMappings[impt]; | ||
let matches = false; | ||
if (isPlain(impt1)) { | ||
matches = impt1.startsWith(base); | ||
if (isPlain(impt)) { | ||
matches = impt.startsWith(base); | ||
} else { | ||
matches = (impt1 = rebase(impt1, this.mapUrl, this.rootUrl)).startsWith(base) || (impt1 = rebase(impt1, this.mapUrl, this.rootUrl)).startsWith(base); | ||
matches = (impt = rebase(impt, this.mapUrl, this.rootUrl)).startsWith(base) || (impt = rebase(impt, this.mapUrl, this.rootUrl)).startsWith(base); | ||
} | ||
if (matches && impt1.slice(base.length) === resolve(target1, this.mapUrl, this.rootUrl).slice(resolve(baseTarget, this.mapUrl, this.rootUrl).length)) { | ||
if (matches && impt.slice(base.length) === resolve(target, this.mapUrl, this.rootUrl).slice(resolve(baseTarget, this.mapUrl, this.rootUrl).length)) { | ||
newbase = true; | ||
delete outMappings[impt1]; | ||
delete outMappings[impt]; | ||
} | ||
@@ -171,3 +171,3 @@ } | ||
for (const scope of Object.keys(this.scopes)){ | ||
const scopeUrl = new URL(scope, this.mapUrl); | ||
const scopeUrl = new URL(resolve(scope, this.mapUrl, this.rootUrl)); | ||
if (scopeUrl.protocol === this.mapUrl.protocol && scopeUrl.hostname === this.mapUrl.hostname && scopeUrl.port === this.mapUrl.port) { | ||
@@ -180,10 +180,10 @@ if (!localScopemapUrl) localScopemapUrl = scopeUrl.href; | ||
// for each scope, update its mappings to be in the shared base where possible | ||
for (const scope1 of Object.keys(this.scopes)){ | ||
const scopeImports = this.scopes[scope1]; | ||
const scopeUrl1 = new URL(scope1, this.mapUrl); | ||
for (const scope of Object.keys(this.scopes)){ | ||
const scopeImports = this.scopes[scope]; | ||
const scopeUrl = new URL(resolve(scope, this.mapUrl, this.rootUrl)); | ||
let scopemapUrl; | ||
if (sameOrigin(scopeUrl1, this.mapUrl)) { | ||
if (sameOrigin(scopeUrl, this.mapUrl)) { | ||
scopemapUrl = relativeLocalScopemapUrl; | ||
} else { | ||
scopemapUrl = scopeUrl1.protocol + '//' + scopeUrl1.hostname + (scopeUrl1.port ? ':' + scopeUrl1.port : '') + '/'; | ||
scopemapUrl = scopeUrl.protocol + '//' + scopeUrl.hostname + (scopeUrl.port ? ':' + scopeUrl.port : '') + '/'; | ||
} | ||
@@ -195,5 +195,5 @@ let scopeBase = this.scopes[scopemapUrl] || Object.create(null); | ||
const target = scopeImports[name]; | ||
if (this.imports[name] && new URL(this.imports[name], this.mapUrl).href === new URL(target, this.mapUrl).href) { | ||
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] || new URL(scopeBase[name], this.mapUrl).href === new URL(target, this.mapUrl).href)) { | ||
} 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); | ||
@@ -206,3 +206,3 @@ delete scopeImports[name]; | ||
} | ||
if (flattenedAll) delete this.scopes[scope1]; | ||
if (flattenedAll) delete this.scopes[scope]; | ||
} | ||
@@ -249,11 +249,11 @@ return this; | ||
let changedScopeImportProps = false; | ||
for (const impt1 of Object.keys(scopeImports)){ | ||
const target1 = scopeImports[impt1]; | ||
scopeImports[impt1] = rebase(resolve(target1, this.mapUrl, this.rootUrl), mapUrl, rootUrl); | ||
if (!isPlain(impt1)) { | ||
const newName = rebase(resolve(impt1, this.mapUrl, this.rootUrl), mapUrl, rootUrl); | ||
if (newName !== impt1) { | ||
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[impt1]; | ||
delete scopeImports[impt1]; | ||
scopeImports[newName] = scopeImports[impt]; | ||
delete scopeImports[impt]; | ||
} | ||
@@ -289,3 +289,3 @@ } | ||
} | ||
const scopeMatches = getScopeMatches(parentUrl, this.scopes, this.mapUrl); | ||
const scopeMatches = getScopeMatches(parentUrl, this.scopes, this.mapUrl, this.rootUrl); | ||
for (const [scope] of scopeMatches){ | ||
@@ -301,9 +301,9 @@ let mapMatch = getMapMatch(specifier, this.scopes[scope]); | ||
} | ||
let mapMatch1 = getMapMatch(specifier, this.imports); | ||
if (!mapMatch1 && specifierUrl) { | ||
mapMatch1 = getMapMatch(specifier = rebase(specifier, this.mapUrl, this.rootUrl), this.imports) || this.rootUrl && getMapMatch(specifier = rebase(specifier, this.mapUrl, null), this.imports) || undefined; | ||
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 (mapMatch1) { | ||
const target1 = this.imports[mapMatch1]; | ||
return resolve(target1 + specifier.slice(mapMatch1.length), this.mapUrl, this.rootUrl); | ||
if (mapMatch) { | ||
const target = this.imports[mapMatch]; | ||
return resolve(target + specifier.slice(mapMatch.length), this.mapUrl, this.rootUrl); | ||
} | ||
@@ -330,3 +330,3 @@ if (specifierUrl) return specifierUrl.href; | ||
this.scopes = Object.create(null); | ||
let { map , mapUrl =baseUrl , rootUrl } = opts instanceof URL || typeof opts === 'string' || opts === undefined ? { | ||
let { map , mapUrl =baseUrl , rootUrl } = opts instanceof URL || typeof opts === 'string' || typeof opts === 'undefined' ? { | ||
mapUrl: opts, | ||
@@ -344,6 +344,6 @@ map: undefined, | ||
} | ||
export function getScopeMatches(parentUrl, scopes, mapUrl) { | ||
export function getScopeMatches(parentUrl, scopes, mapUrl, rootUrl) { | ||
let scopeCandidates = Object.keys(scopes).map((scope)=>[ | ||
scope, | ||
new URL(scope, mapUrl).href | ||
resolve(scope, mapUrl, rootUrl) | ||
]); | ||
@@ -350,0 +350,0 @@ scopeCandidates = scopeCandidates.sort(([, matchA], [, matchB])=>matchA.length < matchB.length ? 1 : -1); |
@@ -1,2 +0,2 @@ | ||
var ref; | ||
var _process_versions; | ||
export let baseUrl; | ||
@@ -7,3 +7,3 @@ // @ts-ignore | ||
baseUrl = new URL('file://' + Deno.cwd() + '/'); | ||
} else if (typeof process !== 'undefined' && ((ref = process.versions) === null || ref === void 0 ? void 0 : ref.node)) { | ||
} else if (typeof process !== 'undefined' && ((_process_versions = process.versions) === null || _process_versions === void 0 ? void 0 : _process_versions.node)) { | ||
baseUrl = new URL('file://' + process.cwd() + '/'); | ||
@@ -40,2 +40,5 @@ } else if (typeof document !== 'undefined') { | ||
if (rootUrl && resolved.href.startsWith(rootUrl.href)) return resolved.href.slice(rootUrl.href.length - 1); | ||
if (rootUrl && rootUrl.href.startsWith(resolved.href)) { | ||
return '/' + relative(resolved, rootUrl); | ||
} | ||
if (sameOrigin(resolved, baseUrl)) return relative(resolved, baseUrl); | ||
@@ -42,0 +45,0 @@ return resolved.href; |
@@ -5,3 +5,3 @@ { | ||
"license": "MIT", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"types": "src/map.ts", | ||
@@ -35,6 +35,7 @@ "type": "module", | ||
"devDependencies": { | ||
"@swc/cli": "^0.1.57", | ||
"@swc/core": "^1.2.215", | ||
"rollup": "^2.53.3" | ||
"@swc/cli": "^0.1.62", | ||
"@swc/core": "^1.3.36", | ||
"rollup": "^2.53.3", | ||
"typedoc": "^0.23.24" | ||
} | ||
} |
@@ -0,0 +0,0 @@ # Import Map Utility |
@@ -0,0 +0,0 @@ export function alphabetize<T> (obj: Record<string, T>): Record<string, T> { |
@@ -35,3 +35,3 @@ import { baseUrl, rebase, isPlain, isURL, getCommonBase, resolve, sameOrigin } from './url.js'; | ||
constructor (opts: { map?: IImportMap, mapUrl?: string | URL, rootUrl?: string | URL | null } | string | URL) { | ||
let { map, mapUrl = baseUrl, rootUrl } = opts instanceof URL || typeof opts === 'string' || opts === undefined | ||
let { map, mapUrl = baseUrl, rootUrl } = (opts instanceof URL || typeof opts === 'string' || typeof opts === 'undefined') | ||
? { mapUrl: opts, map: undefined, rootUrl: undefined } | ||
@@ -129,3 +129,3 @@ : opts; | ||
const scopeImports = this.scopes[scope]; | ||
const scopeUrl = new URL(scope, this.mapUrl).href; | ||
const scopeUrl = resolve(scope, this.mapUrl, this.rootUrl); | ||
if (replaceSubpaths && scopeUrl.startsWith(url) || scopeUrl === url) { | ||
@@ -246,3 +246,3 @@ const newScope = newRelPkgUrl + scopeUrl.slice(url.length); | ||
for(const scope of Object.keys(this.scopes)) { | ||
const scopeUrl = new URL(scope, this.mapUrl); | ||
const scopeUrl = new URL(resolve(scope, this.mapUrl, this.rootUrl)); | ||
if (scopeUrl.protocol === this.mapUrl.protocol && scopeUrl.hostname === this.mapUrl.hostname && scopeUrl.port === this.mapUrl.port) { | ||
@@ -262,3 +262,3 @@ if (!localScopemapUrl) | ||
const scopeUrl = new URL(scope, this.mapUrl); | ||
const scopeUrl = new URL(resolve(scope, this.mapUrl, this.rootUrl)); | ||
@@ -279,6 +279,6 @@ let scopemapUrl: string; | ||
const target = scopeImports[name]; | ||
if (this.imports[name] && new URL(this.imports[name], this.mapUrl).href === new URL(target, this.mapUrl).href) { | ||
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] || new URL(scopeBase[name], this.mapUrl).href === new URL(target, this.mapUrl).href)) { | ||
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); | ||
@@ -387,3 +387,3 @@ delete scopeImports[name]; | ||
} | ||
const scopeMatches = getScopeMatches(parentUrl, this.scopes, this.mapUrl); | ||
const scopeMatches = getScopeMatches(parentUrl, this.scopes, this.mapUrl, this.rootUrl); | ||
for (const [scope] of scopeMatches) { | ||
@@ -427,4 +427,4 @@ let mapMatch = getMapMatch(specifier, this.scopes[scope]); | ||
export function getScopeMatches (parentUrl: string, scopes: Record<string, Record<string, string>>, mapUrl: URL): [string, string][] { | ||
let scopeCandidates = Object.keys(scopes).map(scope => [scope, new URL(scope, mapUrl).href]); | ||
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); | ||
@@ -431,0 +431,0 @@ |
@@ -62,2 +62,5 @@ declare global { | ||
return resolved.href.slice(rootUrl.href.length - 1); | ||
if (rootUrl && rootUrl.href.startsWith(resolved.href)) {// edge-case | ||
return '/' + relative(resolved, rootUrl); | ||
} | ||
if (sameOrigin(resolved, baseUrl)) | ||
@@ -64,0 +67,0 @@ return relative(resolved, baseUrl); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
22
1532
152313
4