@jsenv/module-resolution
Advanced tools
Comparing version 3.7.0 to 4.0.0
@@ -41,2 +41,187 @@ 'use strict'; | ||
var hrefToScheme = function hrefToScheme(href) { | ||
var colonIndex = href.indexOf(":"); | ||
if (colonIndex === -1) return ""; | ||
return href.slice(0, colonIndex); | ||
}; | ||
var hrefToPathname = function hrefToPathname(href) { | ||
return ressourceToPathname(hrefToRessource(href)); | ||
}; | ||
var hrefToRessource = function hrefToRessource(href) { | ||
var scheme = hrefToScheme(href); | ||
if (scheme === "file") { | ||
return href.slice("file://".length); | ||
} | ||
if (scheme === "https" || scheme === "http") { | ||
// remove origin | ||
var afterProtocol = href.slice(scheme.length + "://".length); | ||
var pathnameSlashIndex = afterProtocol.indexOf("/", "://".length); | ||
return afterProtocol.slice(pathnameSlashIndex); | ||
} | ||
return href.slice(scheme.length + 1); | ||
}; | ||
var ressourceToPathname = function ressourceToPathname(ressource) { | ||
var searchSeparatorIndex = ressource.indexOf("?"); | ||
return searchSeparatorIndex === -1 ? ressource : ressource.slice(0, searchSeparatorIndex); | ||
}; | ||
var hrefToOrigin = function hrefToOrigin(href) { | ||
var scheme = hrefToScheme(href); | ||
if (scheme === "file") { | ||
return "file://"; | ||
} | ||
if (scheme === "http" || scheme === "https") { | ||
var secondProtocolSlashIndex = scheme.length + "://".length; | ||
var pathnameSlashIndex = href.indexOf("/", secondProtocolSlashIndex); | ||
if (pathnameSlashIndex === -1) return href; | ||
return href.slice(0, pathnameSlashIndex); | ||
} | ||
return href.slice(0, scheme.length + 1); | ||
}; | ||
var assertImportMap = function assertImportMap(value) { | ||
if (value === null) { | ||
throw new TypeError("an importMap must be an object, got null"); | ||
} | ||
var type = _typeof(value); | ||
if (type !== "object") { | ||
throw new TypeError("an importMap must be an object, received ".concat(value)); | ||
} | ||
if (Array.isArray(value)) { | ||
throw new TypeError("an importMap must be an object, received array ".concat(value)); | ||
} | ||
}; | ||
var applyImportMap = function applyImportMap(_ref) { | ||
var importMap = _ref.importMap, | ||
href = _ref.href, | ||
importerHref = _ref.importerHref; | ||
assertImportMap(importMap); | ||
if (typeof href !== "string") { | ||
throw new TypeError("href must be a string, got ".concat(href)); | ||
} | ||
var _importMap$imports = importMap.imports, | ||
imports = _importMap$imports === void 0 ? {} : _importMap$imports, | ||
_importMap$scopes = importMap.scopes, | ||
scopes = _importMap$scopes === void 0 ? {} : _importMap$scopes; | ||
if (importerHref) { | ||
if (typeof importerHref !== "string") { | ||
throw new TypeError("importerHref must be a string, got ".concat(importerHref)); | ||
} | ||
var importerPathname = hrefToPathname(importerHref); // here instead or taking the first match | ||
// take the best match instead | ||
// check the importMap spec repo to find how | ||
// (bsically most char matching is a win) | ||
// (or just the longest scope) | ||
var matchingPathnamePattern = Object.keys(scopes).find(function (pathnameMatchPattern) { | ||
return match(importerPathname, pathnameMatchPattern); | ||
}); | ||
if (matchingPathnamePattern) { | ||
var scopeImports = scopes[matchingPathnamePattern]; | ||
return applyImports({ | ||
href: href, | ||
imports: scopeImports // scopePattern: matchingPathnamePattern, | ||
}); | ||
} | ||
} | ||
return applyImports({ | ||
href: href, | ||
imports: imports | ||
}); | ||
}; | ||
var applyImports = function applyImports(_ref2) { | ||
var href = _ref2.href, | ||
imports = _ref2.imports; | ||
var modulePathname = hrefToPathname(href); | ||
var pathnamePatternArray = Object.keys(imports); | ||
var i = 0; | ||
while (i < pathnamePatternArray.length) { | ||
var pathnamePattern = pathnamePatternArray[i]; | ||
i++; | ||
var matchResult = match(modulePathname, pathnamePattern); | ||
if (!matchResult) continue; | ||
var before = matchResult.before, | ||
after = matchResult.after; | ||
var moduleOrigin = hrefToOrigin(href); | ||
var replacement = imports[pathnamePattern]; | ||
return "".concat(moduleOrigin).concat(before).concat(replacement).concat(after); | ||
} | ||
return href; | ||
}; | ||
var match = function match(pathname, pattern) { | ||
var patternHasLeadingSlash = pattern[0] === "/"; | ||
var patternHasTrailingSlash = pattern[pattern.length - 1] === "/"; // pattern : '/foo/' | ||
if (patternHasLeadingSlash && patternHasTrailingSlash) { | ||
var index = pathname.indexOf(pattern); // pathname: '/fo/bar' | ||
// pathname: '/foobar' | ||
if (index === -1) return null; // pathname: '/foo/bar' | ||
return { | ||
before: pathname.slice(0, index), | ||
after: pathname.slice(index + pattern.length) | ||
}; | ||
} // pattern: '/foo' | ||
if (patternHasLeadingSlash && !patternHasTrailingSlash) { | ||
// pathname: '/fo', /foobar' | ||
if (pathname !== pattern) return null; // pathname: '/foo' | ||
return { | ||
before: "", | ||
after: "" | ||
}; | ||
} // pattern: 'foo/' | ||
if (!patternHasLeadingSlash && patternHasTrailingSlash) { | ||
var _index = pathname.indexOf("/".concat(pattern)); // pathname: '/fo/bar' | ||
if (_index === -1) return null; // pathname: '/bar/foo/file.js' | ||
if (_index !== 0) return null; // pathname: '/foo', '/foo/bar' | ||
return { | ||
before: "", | ||
after: pathname.slice(pattern.length + 1) | ||
}; | ||
} // pattern 'foo' | ||
// pathname: '/fo', /foobar' | ||
if (pathname.slice(1) !== pattern) return null; // pathname: '/foo' | ||
return { | ||
before: "", | ||
after: "" | ||
}; | ||
}; | ||
// https://url.spec.whatwg.org/#example-start-with-a-widows-drive-letter | ||
@@ -67,3 +252,3 @@ var isWindowsDriveLetter = function isWindowsDriveLetter(specifier) { | ||
var hrefToScheme = function hrefToScheme(href) { | ||
var hrefToScheme$1 = function hrefToScheme(href) { | ||
var colonIndex = href.indexOf(":"); | ||
@@ -80,7 +265,7 @@ if (colonIndex === -1) return ""; | ||
specifier = _ref.specifier; | ||
return "".concat(hrefToScheme(importer), ":").concat(specifier); | ||
return "".concat(hrefToScheme$1(importer), ":").concat(specifier); | ||
}; | ||
var hrefToOrigin = function hrefToOrigin(href) { | ||
var scheme = hrefToScheme(href); | ||
var hrefToOrigin$1 = function hrefToOrigin(href) { | ||
var scheme = hrefToScheme$1(href); | ||
@@ -111,12 +296,12 @@ if (scheme === "file") { | ||
specifier = _ref.specifier; | ||
var importerOrigin = hrefToOrigin(importer); | ||
var importerOrigin = hrefToOrigin$1(importer); | ||
return "".concat(importerOrigin, "/").concat(specifier.slice(1)); | ||
}; | ||
var hrefToPathname = function hrefToPathname(href) { | ||
return ressourceToPathname(hrefToRessource(href)); | ||
var hrefToPathname$1 = function hrefToPathname(href) { | ||
return ressourceToPathname$1(hrefToRessource$1(href)); | ||
}; | ||
var hrefToRessource = function hrefToRessource(href) { | ||
var scheme = hrefToScheme(href); | ||
var hrefToRessource$1 = function hrefToRessource(href) { | ||
var scheme = hrefToScheme$1(href); | ||
@@ -137,3 +322,3 @@ if (scheme === "file") { | ||
var ressourceToPathname = function ressourceToPathname(ressource) { | ||
var ressourceToPathname$1 = function ressourceToPathname(ressource) { | ||
var searchSeparatorIndex = ressource.indexOf("?"); | ||
@@ -159,3 +344,3 @@ return searchSeparatorIndex === -1 ? ressource : ressource.slice(0, searchSeparatorIndex); | ||
specifier = _ref.specifier; | ||
var importerPathname = hrefToPathname(importer); // ./foo.js on /folder/file.js -> /folder/foo.js | ||
var importerPathname = hrefToPathname$1(importer); // ./foo.js on /folder/file.js -> /folder/foo.js | ||
// ./foo/bar.js on /folder/file.js -> /folder/foo/bar.js | ||
@@ -165,3 +350,3 @@ // ./foo.js on /folder/subfolder/file.js -> /folder/subfolder/foo.js | ||
if (specifier.startsWith("./")) { | ||
var _importerOrigin = hrefToOrigin(importer); | ||
var _importerOrigin = hrefToOrigin$1(importer); | ||
@@ -190,3 +375,3 @@ var importerDirname = pathnameToDirname(importerPathname); | ||
var importerOrigin = hrefToOrigin(importer); | ||
var importerOrigin = hrefToOrigin$1(importer); | ||
var resolvedPathname = "".concat(importerFolders.join("/"), "/").concat(unresolvedPathname); | ||
@@ -199,3 +384,3 @@ return "".concat(importerOrigin).concat(resolvedPathname); | ||
specifier = _ref.specifier; | ||
var importerOrigin = hrefToOrigin(importer); | ||
var importerOrigin = hrefToOrigin$1(importer); | ||
return "".concat(importerOrigin, "/").concat(specifier); | ||
@@ -246,112 +431,2 @@ }; | ||
var remapResolvedImport = function remapResolvedImport(_ref) { | ||
var importMap = _ref.importMap, | ||
importerHref = _ref.importerHref, | ||
resolvedImport = _ref.resolvedImport; | ||
if (_typeof(importMap) !== "object") throw new TypeError("importMap must be an object, got ".concat(importMap)); | ||
var _importMap$imports = importMap.imports, | ||
imports = _importMap$imports === void 0 ? {} : _importMap$imports, | ||
_importMap$scopes = importMap.scopes, | ||
scopes = _importMap$scopes === void 0 ? {} : _importMap$scopes; | ||
if (_typeof(imports) !== "object") throw new TypeError("importMap.imports must be an object, got ".concat(imports)); | ||
if (_typeof(scopes) !== "object") throw new TypeError("importMap.scopes must be an object, got ".concat(scopes)); | ||
if (typeof resolvedImport !== "string") throw new TypeError("resolvedImport must be a string, got ".concat(resolvedImport)); | ||
if (importerHref) { | ||
if (typeof importerHref !== "string") throw new TypeError("importerHref must be a string, got ".concat(importerHref)); | ||
var importerPathname = hrefToPathname(importerHref); | ||
var matchingPathnamePattern = Object.keys(scopes).find(function (pathnameMatchPattern) { | ||
return match(importerPathname, pathnameMatchPattern); | ||
}); | ||
if (matchingPathnamePattern) { | ||
var scopeImports = scopes[matchingPathnamePattern]; | ||
return remapUsingImports({ | ||
moduleHref: resolvedImport, | ||
imports: scopeImports, | ||
scopePattern: matchingPathnamePattern | ||
}); | ||
} | ||
} | ||
return remapUsingImports({ | ||
moduleHref: resolvedImport, | ||
imports: imports | ||
}); | ||
}; | ||
var remapUsingImports = function remapUsingImports(_ref2) { | ||
var moduleHref = _ref2.moduleHref, | ||
imports = _ref2.imports; | ||
var modulePathname = hrefToPathname(moduleHref); | ||
var pathnamePatternArray = Object.keys(imports); | ||
var i = 0; | ||
while (i < pathnamePatternArray.length) { | ||
var pathnamePattern = pathnamePatternArray[i]; | ||
i++; | ||
var matchResult = match(modulePathname, pathnamePattern); | ||
if (!matchResult) continue; | ||
var before = matchResult.before, | ||
after = matchResult.after; | ||
var moduleOrigin = hrefToOrigin(moduleHref); | ||
var replacement = imports[pathnamePattern]; | ||
return "".concat(moduleOrigin).concat(before).concat(replacement).concat(after); | ||
} | ||
return moduleHref; | ||
}; | ||
var match = function match(pathname, pattern) { | ||
var patternHasLeadingSlash = pattern[0] === "/"; | ||
var patternHasTrailingSlash = pattern[pattern.length - 1] === "/"; // pattern : '/foo/' | ||
if (patternHasLeadingSlash && patternHasTrailingSlash) { | ||
var index = pathname.indexOf(pattern); // pathname: '/fo/bar' | ||
// pathname: '/foobar' | ||
if (index === -1) return null; // pathname: '/foo/bar' | ||
return { | ||
before: pathname.slice(0, index), | ||
after: pathname.slice(index + pattern.length) | ||
}; | ||
} // pattern: '/foo' | ||
if (patternHasLeadingSlash && !patternHasTrailingSlash) { | ||
// pathname: '/fo', /foobar' | ||
if (pathname !== pattern) return null; // pathname: '/foo' | ||
return { | ||
before: "", | ||
after: "" | ||
}; | ||
} // pattern: 'foo/' | ||
if (!patternHasLeadingSlash && patternHasTrailingSlash) { | ||
var _index = pathname.indexOf("/".concat(pattern)); // pathname: '/fo/bar' | ||
if (_index === -1) return null; // pathname: '/bar/foo/file.js' | ||
if (_index !== 0) return null; // pathname: '/foo', '/foo/bar' | ||
return { | ||
before: "", | ||
after: pathname.slice(pattern.length + 1) | ||
}; | ||
} // pattern 'foo' | ||
// pathname: '/fo', /foobar' | ||
if (pathname.slice(1) !== pattern) return null; // pathname: '/foo' | ||
return { | ||
before: "", | ||
after: "" | ||
}; | ||
}; | ||
var pathnameToExtension = function pathnameToExtension(pathname) { | ||
@@ -380,6 +455,6 @@ var slashLastIndex = pathname.lastIndexOf("/"); | ||
}); | ||
var remappedImport = importMap ? remapResolvedImport({ | ||
resolvedImport: resolvedImport, | ||
importerHref: importer, | ||
importMap: importMap | ||
var remappedImport = importMap ? applyImportMap({ | ||
importMap: importMap, | ||
href: resolvedImport, | ||
importerHref: importer | ||
}) : resolvedImport; | ||
@@ -400,3 +475,3 @@ | ||
if (importer) { | ||
var importerPathname = hrefToPathname(importer); | ||
var importerPathname = hrefToPathname$1(importer); | ||
var importerExtension = pathnameToExtension(importerPathname); | ||
@@ -411,21 +486,9 @@ return "".concat(remappedImport).concat(importerExtension); | ||
var normalizePathname = function normalizePathname(pathname) { | ||
pathname = pathname.replace(/\\/g, "/"); // we could also just ensure leading slash on pathname | ||
if (isWindowsDriveLetter(pathname.slice(0, 3))) { | ||
return "/".concat(pathname); | ||
} | ||
return pathname; | ||
}; | ||
exports.hrefToOrigin = hrefToOrigin; | ||
exports.hrefToPathname = hrefToPathname; | ||
exports.hrefToScheme = hrefToScheme; | ||
exports.normalizePathname = normalizePathname; | ||
exports.hrefToOrigin = hrefToOrigin$1; | ||
exports.hrefToPathname = hrefToPathname$1; | ||
exports.hrefToScheme = hrefToScheme$1; | ||
exports.pathnameToDirname = pathnameToDirname; | ||
exports.pathnameToExtension = pathnameToExtension; | ||
exports.remapResolvedImport = remapResolvedImport; | ||
exports.resolveImport = resolveImport; | ||
exports.resolvePath = resolvePath; | ||
//# sourceMappingURL=main.js.map |
@@ -38,5 +38,190 @@ var __dmail_assert__ = function (exports) { | ||
globalObject.globalThis = globalObject; | ||
} // https://url.spec.whatwg.org/#example-start-with-a-widows-drive-letter | ||
} | ||
var hrefToScheme = function hrefToScheme(href) { | ||
var colonIndex = href.indexOf(":"); | ||
if (colonIndex === -1) return ""; | ||
return href.slice(0, colonIndex); | ||
}; | ||
var hrefToPathname = function hrefToPathname(href) { | ||
return ressourceToPathname(hrefToRessource(href)); | ||
}; | ||
var hrefToRessource = function hrefToRessource(href) { | ||
var scheme = hrefToScheme(href); | ||
if (scheme === "file") { | ||
return href.slice("file://".length); | ||
} | ||
if (scheme === "https" || scheme === "http") { | ||
// remove origin | ||
var afterProtocol = href.slice(scheme.length + "://".length); | ||
var pathnameSlashIndex = afterProtocol.indexOf("/", "://".length); | ||
return afterProtocol.slice(pathnameSlashIndex); | ||
} | ||
return href.slice(scheme.length + 1); | ||
}; | ||
var ressourceToPathname = function ressourceToPathname(ressource) { | ||
var searchSeparatorIndex = ressource.indexOf("?"); | ||
return searchSeparatorIndex === -1 ? ressource : ressource.slice(0, searchSeparatorIndex); | ||
}; | ||
var hrefToOrigin = function hrefToOrigin(href) { | ||
var scheme = hrefToScheme(href); | ||
if (scheme === "file") { | ||
return "file://"; | ||
} | ||
if (scheme === "http" || scheme === "https") { | ||
var secondProtocolSlashIndex = scheme.length + "://".length; | ||
var pathnameSlashIndex = href.indexOf("/", secondProtocolSlashIndex); | ||
if (pathnameSlashIndex === -1) return href; | ||
return href.slice(0, pathnameSlashIndex); | ||
} | ||
return href.slice(0, scheme.length + 1); | ||
}; | ||
var assertImportMap = function assertImportMap(value) { | ||
if (value === null) { | ||
throw new TypeError("an importMap must be an object, got null"); | ||
} | ||
var type = _typeof(value); | ||
if (type !== "object") { | ||
throw new TypeError("an importMap must be an object, received ".concat(value)); | ||
} | ||
if (Array.isArray(value)) { | ||
throw new TypeError("an importMap must be an object, received array ".concat(value)); | ||
} | ||
}; | ||
var applyImportMap = function applyImportMap(_ref) { | ||
var importMap = _ref.importMap, | ||
href = _ref.href, | ||
importerHref = _ref.importerHref; | ||
assertImportMap(importMap); | ||
if (typeof href !== "string") { | ||
throw new TypeError("href must be a string, got ".concat(href)); | ||
} | ||
var _importMap$imports = importMap.imports, | ||
imports = _importMap$imports === void 0 ? {} : _importMap$imports, | ||
_importMap$scopes = importMap.scopes, | ||
scopes = _importMap$scopes === void 0 ? {} : _importMap$scopes; | ||
if (importerHref) { | ||
if (typeof importerHref !== "string") { | ||
throw new TypeError("importerHref must be a string, got ".concat(importerHref)); | ||
} | ||
var importerPathname = hrefToPathname(importerHref); // here instead or taking the first match | ||
// take the best match instead | ||
// check the importMap spec repo to find how | ||
// (bsically most char matching is a win) | ||
// (or just the longest scope) | ||
var matchingPathnamePattern = Object.keys(scopes).find(function (pathnameMatchPattern) { | ||
return match(importerPathname, pathnameMatchPattern); | ||
}); | ||
if (matchingPathnamePattern) { | ||
var scopeImports = scopes[matchingPathnamePattern]; | ||
return applyImports({ | ||
href: href, | ||
imports: scopeImports // scopePattern: matchingPathnamePattern, | ||
}); | ||
} | ||
} | ||
return applyImports({ | ||
href: href, | ||
imports: imports | ||
}); | ||
}; | ||
var applyImports = function applyImports(_ref2) { | ||
var href = _ref2.href, | ||
imports = _ref2.imports; | ||
var modulePathname = hrefToPathname(href); | ||
var pathnamePatternArray = Object.keys(imports); | ||
var i = 0; | ||
while (i < pathnamePatternArray.length) { | ||
var pathnamePattern = pathnamePatternArray[i]; | ||
i++; | ||
var matchResult = match(modulePathname, pathnamePattern); | ||
if (!matchResult) continue; | ||
var before = matchResult.before, | ||
after = matchResult.after; | ||
var moduleOrigin = hrefToOrigin(href); | ||
var replacement = imports[pathnamePattern]; | ||
return "".concat(moduleOrigin).concat(before).concat(replacement).concat(after); | ||
} | ||
return href; | ||
}; | ||
var match = function match(pathname, pattern) { | ||
var patternHasLeadingSlash = pattern[0] === "/"; | ||
var patternHasTrailingSlash = pattern[pattern.length - 1] === "/"; // pattern : '/foo/' | ||
if (patternHasLeadingSlash && patternHasTrailingSlash) { | ||
var index = pathname.indexOf(pattern); // pathname: '/fo/bar' | ||
// pathname: '/foobar' | ||
if (index === -1) return null; // pathname: '/foo/bar' | ||
return { | ||
before: pathname.slice(0, index), | ||
after: pathname.slice(index + pattern.length) | ||
}; | ||
} // pattern: '/foo' | ||
if (patternHasLeadingSlash && !patternHasTrailingSlash) { | ||
// pathname: '/fo', /foobar' | ||
if (pathname !== pattern) return null; // pathname: '/foo' | ||
return { | ||
before: "", | ||
after: "" | ||
}; | ||
} // pattern: 'foo/' | ||
if (!patternHasLeadingSlash && patternHasTrailingSlash) { | ||
var _index = pathname.indexOf("/".concat(pattern)); // pathname: '/fo/bar' | ||
if (_index === -1) return null; // pathname: '/bar/foo/file.js' | ||
if (_index !== 0) return null; // pathname: '/foo', '/foo/bar' | ||
return { | ||
before: "", | ||
after: pathname.slice(pattern.length + 1) | ||
}; | ||
} // pattern 'foo' | ||
// pathname: '/fo', /foobar' | ||
if (pathname.slice(1) !== pattern) return null; // pathname: '/foo' | ||
return { | ||
before: "", | ||
after: "" | ||
}; | ||
}; // https://url.spec.whatwg.org/#example-start-with-a-widows-drive-letter | ||
var isWindowsDriveLetter = function isWindowsDriveLetter(specifier) { | ||
@@ -66,3 +251,3 @@ var firstChar = specifier[0]; | ||
var hrefToScheme = function hrefToScheme(href) { | ||
var hrefToScheme$1 = function hrefToScheme(href) { | ||
var colonIndex = href.indexOf(":"); | ||
@@ -80,7 +265,7 @@ if (colonIndex === -1) return ""; | ||
specifier = _ref.specifier; | ||
return "".concat(hrefToScheme(importer), ":").concat(specifier); | ||
return "".concat(hrefToScheme$1(importer), ":").concat(specifier); | ||
}; | ||
var hrefToOrigin = function hrefToOrigin(href) { | ||
var scheme = hrefToScheme(href); | ||
var hrefToOrigin$1 = function hrefToOrigin(href) { | ||
var scheme = hrefToScheme$1(href); | ||
@@ -112,12 +297,12 @@ if (scheme === "file") { | ||
specifier = _ref.specifier; | ||
var importerOrigin = hrefToOrigin(importer); | ||
var importerOrigin = hrefToOrigin$1(importer); | ||
return "".concat(importerOrigin, "/").concat(specifier.slice(1)); | ||
}; | ||
var hrefToPathname = function hrefToPathname(href) { | ||
return ressourceToPathname(hrefToRessource(href)); | ||
var hrefToPathname$1 = function hrefToPathname(href) { | ||
return ressourceToPathname$1(hrefToRessource$1(href)); | ||
}; | ||
var hrefToRessource = function hrefToRessource(href) { | ||
var scheme = hrefToScheme(href); | ||
var hrefToRessource$1 = function hrefToRessource(href) { | ||
var scheme = hrefToScheme$1(href); | ||
@@ -138,3 +323,3 @@ if (scheme === "file") { | ||
var ressourceToPathname = function ressourceToPathname(ressource) { | ||
var ressourceToPathname$1 = function ressourceToPathname(ressource) { | ||
var searchSeparatorIndex = ressource.indexOf("?"); | ||
@@ -160,3 +345,3 @@ return searchSeparatorIndex === -1 ? ressource : ressource.slice(0, searchSeparatorIndex); | ||
specifier = _ref.specifier; | ||
var importerPathname = hrefToPathname(importer); // ./foo.js on /folder/file.js -> /folder/foo.js | ||
var importerPathname = hrefToPathname$1(importer); // ./foo.js on /folder/file.js -> /folder/foo.js | ||
// ./foo/bar.js on /folder/file.js -> /folder/foo/bar.js | ||
@@ -166,3 +351,3 @@ // ./foo.js on /folder/subfolder/file.js -> /folder/subfolder/foo.js | ||
if (specifier.startsWith("./")) { | ||
var _importerOrigin = hrefToOrigin(importer); | ||
var _importerOrigin = hrefToOrigin$1(importer); | ||
@@ -191,3 +376,3 @@ var importerDirname = pathnameToDirname(importerPathname); | ||
var importerOrigin = hrefToOrigin(importer); | ||
var importerOrigin = hrefToOrigin$1(importer); | ||
var resolvedPathname = "".concat(importerFolders.join("/"), "/").concat(unresolvedPathname); | ||
@@ -200,3 +385,3 @@ return "".concat(importerOrigin).concat(resolvedPathname); | ||
specifier = _ref.specifier; | ||
var importerOrigin = hrefToOrigin(importer); | ||
var importerOrigin = hrefToOrigin$1(importer); | ||
return "".concat(importerOrigin, "/").concat(specifier); | ||
@@ -247,112 +432,2 @@ }; | ||
var remapResolvedImport = function remapResolvedImport(_ref) { | ||
var importMap = _ref.importMap, | ||
importerHref = _ref.importerHref, | ||
resolvedImport = _ref.resolvedImport; | ||
if (_typeof(importMap) !== "object") throw new TypeError("importMap must be an object, got ".concat(importMap)); | ||
var _importMap$imports = importMap.imports, | ||
imports = _importMap$imports === void 0 ? {} : _importMap$imports, | ||
_importMap$scopes = importMap.scopes, | ||
scopes = _importMap$scopes === void 0 ? {} : _importMap$scopes; | ||
if (_typeof(imports) !== "object") throw new TypeError("importMap.imports must be an object, got ".concat(imports)); | ||
if (_typeof(scopes) !== "object") throw new TypeError("importMap.scopes must be an object, got ".concat(scopes)); | ||
if (typeof resolvedImport !== "string") throw new TypeError("resolvedImport must be a string, got ".concat(resolvedImport)); | ||
if (importerHref) { | ||
if (typeof importerHref !== "string") throw new TypeError("importerHref must be a string, got ".concat(importerHref)); | ||
var importerPathname = hrefToPathname(importerHref); | ||
var matchingPathnamePattern = Object.keys(scopes).find(function (pathnameMatchPattern) { | ||
return match(importerPathname, pathnameMatchPattern); | ||
}); | ||
if (matchingPathnamePattern) { | ||
var scopeImports = scopes[matchingPathnamePattern]; | ||
return remapUsingImports({ | ||
moduleHref: resolvedImport, | ||
imports: scopeImports, | ||
scopePattern: matchingPathnamePattern | ||
}); | ||
} | ||
} | ||
return remapUsingImports({ | ||
moduleHref: resolvedImport, | ||
imports: imports | ||
}); | ||
}; | ||
var remapUsingImports = function remapUsingImports(_ref2) { | ||
var moduleHref = _ref2.moduleHref, | ||
imports = _ref2.imports; | ||
var modulePathname = hrefToPathname(moduleHref); | ||
var pathnamePatternArray = Object.keys(imports); | ||
var i = 0; | ||
while (i < pathnamePatternArray.length) { | ||
var pathnamePattern = pathnamePatternArray[i]; | ||
i++; | ||
var matchResult = match(modulePathname, pathnamePattern); | ||
if (!matchResult) continue; | ||
var before = matchResult.before, | ||
after = matchResult.after; | ||
var moduleOrigin = hrefToOrigin(moduleHref); | ||
var replacement = imports[pathnamePattern]; | ||
return "".concat(moduleOrigin).concat(before).concat(replacement).concat(after); | ||
} | ||
return moduleHref; | ||
}; | ||
var match = function match(pathname, pattern) { | ||
var patternHasLeadingSlash = pattern[0] === "/"; | ||
var patternHasTrailingSlash = pattern[pattern.length - 1] === "/"; // pattern : '/foo/' | ||
if (patternHasLeadingSlash && patternHasTrailingSlash) { | ||
var index = pathname.indexOf(pattern); // pathname: '/fo/bar' | ||
// pathname: '/foobar' | ||
if (index === -1) return null; // pathname: '/foo/bar' | ||
return { | ||
before: pathname.slice(0, index), | ||
after: pathname.slice(index + pattern.length) | ||
}; | ||
} // pattern: '/foo' | ||
if (patternHasLeadingSlash && !patternHasTrailingSlash) { | ||
// pathname: '/fo', /foobar' | ||
if (pathname !== pattern) return null; // pathname: '/foo' | ||
return { | ||
before: "", | ||
after: "" | ||
}; | ||
} // pattern: 'foo/' | ||
if (!patternHasLeadingSlash && patternHasTrailingSlash) { | ||
var _index = pathname.indexOf("/".concat(pattern)); // pathname: '/fo/bar' | ||
if (_index === -1) return null; // pathname: '/bar/foo/file.js' | ||
if (_index !== 0) return null; // pathname: '/foo', '/foo/bar' | ||
return { | ||
before: "", | ||
after: pathname.slice(pattern.length + 1) | ||
}; | ||
} // pattern 'foo' | ||
// pathname: '/fo', /foobar' | ||
if (pathname.slice(1) !== pattern) return null; // pathname: '/foo' | ||
return { | ||
before: "", | ||
after: "" | ||
}; | ||
}; | ||
var pathnameToExtension = function pathnameToExtension(pathname) { | ||
@@ -381,6 +456,6 @@ var slashLastIndex = pathname.lastIndexOf("/"); | ||
}); | ||
var remappedImport = importMap ? remapResolvedImport({ | ||
resolvedImport: resolvedImport, | ||
importerHref: importer, | ||
importMap: importMap | ||
var remappedImport = importMap ? applyImportMap({ | ||
importMap: importMap, | ||
href: resolvedImport, | ||
importerHref: importer | ||
}) : resolvedImport; | ||
@@ -401,3 +476,3 @@ | ||
if (importer) { | ||
var importerPathname = hrefToPathname(importer); | ||
var importerPathname = hrefToPathname$1(importer); | ||
var importerExtension = pathnameToExtension(importerPathname); | ||
@@ -412,19 +487,7 @@ return "".concat(remappedImport).concat(importerExtension); | ||
var normalizePathname = function normalizePathname(pathname) { | ||
pathname = pathname.replace(/\\/g, "/"); // we could also just ensure leading slash on pathname | ||
if (isWindowsDriveLetter(pathname.slice(0, 3))) { | ||
return "/".concat(pathname); | ||
} | ||
return pathname; | ||
}; | ||
exports.hrefToOrigin = hrefToOrigin; | ||
exports.hrefToPathname = hrefToPathname; | ||
exports.hrefToScheme = hrefToScheme; | ||
exports.normalizePathname = normalizePathname; | ||
exports.hrefToOrigin = hrefToOrigin$1; | ||
exports.hrefToPathname = hrefToPathname$1; | ||
exports.hrefToScheme = hrefToScheme$1; | ||
exports.pathnameToDirname = pathnameToDirname; | ||
exports.pathnameToExtension = pathnameToExtension; | ||
exports.remapResolvedImport = remapResolvedImport; | ||
exports.resolveImport = resolveImport; | ||
@@ -431,0 +494,0 @@ exports.resolvePath = resolvePath; |
22
index.js
@@ -1,13 +0,11 @@ | ||
// public export | ||
export { resolvePath } from "./src/resolvePath.js" | ||
// documented export | ||
export { resolvePath } from "./src/resolvePath/resolvePath.js" | ||
// some generic helpers some other project uses | ||
export { resolveImport } from "./src/resolveImport.js" | ||
export { remapResolvedImport } from "./src/remapResolvedImport.js" | ||
export { hrefToScheme } from "./src/hrefToScheme.js" | ||
export { hrefToOrigin } from "./src/hrefToOrigin.js" | ||
export { hrefToPathname } from "./src/hrefToPathname.js" | ||
export { normalizePathname } from "./src/normalizePathname.js" | ||
export { pathnameToDirname } from "./src/pathnameToDirname.js" | ||
export { pathnameToExtension } from "./src/pathnameToExtension.js" | ||
// undocumented exports | ||
// that's why they are not at src/${exportName}/${exportName}.js | ||
export { resolveImport } from "./src/resolvePath/resolveImport.js" | ||
export { hrefToOrigin } from "./src/resolvePath/hrefToOrigin.js" | ||
export { hrefToPathname } from "./src/resolvePath/hrefToPathname.js" | ||
export { hrefToScheme } from "./src/resolvePath/hrefToScheme.js" | ||
export { pathnameToDirname } from "./src/resolvePath/pathnameToDirname.js" | ||
export { pathnameToExtension } from "./src/resolvePath/pathnameToExtension.js" |
{ | ||
"name": "@jsenv/module-resolution", | ||
"version": "3.7.0", | ||
"version": "4.0.0", | ||
"license": "MIT", | ||
@@ -16,3 +16,4 @@ "repository": { | ||
"exports": { | ||
"/": "/" | ||
"/": "/", | ||
"/isNativeNodeModuleBareSpecifier.js": "/src/isNativeNodeModuleBareSpecifier.js" | ||
}, | ||
@@ -40,3 +41,5 @@ "files": [ | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"@jsenv/import-map": "1.0.0" | ||
}, | ||
"devDependencies": { | ||
@@ -43,0 +46,0 @@ "@dmail/assert": "3.14.0", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
193366
1
22
1084
1
+ Added@jsenv/import-map@1.0.0
+ Added@jsenv/import-map@1.0.0(transitive)
+ Added@jsenv/module-resolution@3.6.0(transitive)