Socket
Socket
Sign inDemoInstall

@jsenv/import-map

Package Overview
Dependencies
Maintainers
2
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jsenv/import-map - npm Package Compare versions

Comparing version 6.8.1 to 6.9.0

src/internal/getCommonPathname.js

218

dist/esmodule/main.js

@@ -80,6 +80,10 @@ var nativeTypeOf = function nativeTypeOf(obj) {

var pathnameToDirectoryPathname = function pathnameToDirectoryPathname(pathname) {
var pathnameToParentPathname = function pathnameToParentPathname(pathname) {
var slashLastIndex = pathname.lastIndexOf("/");
if (slashLastIndex === -1) return "";
return pathname.slice(0, slashLastIndex);
if (slashLastIndex === -1) {
return "/";
}
return pathname.slice(0, slashLastIndex + 1);
};

@@ -130,4 +134,4 @@

if (specifier === ".") {
var baseDirectoryPathname = pathnameToDirectoryPathname(basePathname);
return "".concat(baseOrigin).concat(baseDirectoryPathname, "/");
var baseDirectoryPathname = pathnameToParentPathname(basePathname);
return "".concat(baseOrigin).concat(baseDirectoryPathname);
} // pathname relative inside

@@ -137,5 +141,5 @@

if (specifier.slice(0, 2) === "./") {
var _baseDirectoryPathname = pathnameToDirectoryPathname(basePathname);
var _baseDirectoryPathname = pathnameToParentPathname(basePathname);
return "".concat(baseOrigin).concat(_baseDirectoryPathname, "/").concat(specifier.slice(2));
return "".concat(baseOrigin).concat(_baseDirectoryPathname).concat(specifier.slice(2));
} // pathname relative outside

@@ -171,3 +175,3 @@

return "".concat(baseOrigin).concat(pathnameToDirectoryPathname(basePathname), "/").concat(specifier);
return "".concat(baseOrigin).concat(pathnameToParentPathname(basePathname)).concat(specifier);
};

@@ -199,3 +203,3 @@

var resolveSpecifier = function resolveSpecifier(specifier, importer) {
if (specifier[0] === "/" || specifier.startsWith("./") || specifier.startsWith("../")) {
if (specifier === "." || specifier[0] === "/" || specifier.startsWith("./") || specifier.startsWith("../")) {
return resolveUrl(specifier, importer);

@@ -440,10 +444,123 @@ }

var makeImportMapRelativeTo = function makeImportMapRelativeTo(importMap, url) {
assertImportMap(importMap);
var slashCount = urlToPathname(url).slice(1).split("/").length - 1;
var getCommonPathname = function getCommonPathname(pathname, otherPathname) {
var firstDifferentCharacterIndex = findFirstDifferentCharacterIndex(pathname, otherPathname); // pathname and otherpathname are exactly the same
if (slashCount === 0) {
return importMap;
if (firstDifferentCharacterIndex === -1) {
return pathname;
}
var commonString = pathname.slice(0, firstDifferentCharacterIndex + 1); // the first different char is at firstDifferentCharacterIndex
if (pathname.charAt(firstDifferentCharacterIndex) === "/") {
return commonString;
}
if (otherPathname.charAt(firstDifferentCharacterIndex) === "/") {
return commonString;
}
var firstDifferentSlashIndex = commonString.lastIndexOf("/");
return pathname.slice(0, firstDifferentSlashIndex + 1);
};
var findFirstDifferentCharacterIndex = function findFirstDifferentCharacterIndex(string, otherString) {
var maxCommonLength = Math.min(string.length, otherString.length);
var i = 0;
while (i < maxCommonLength) {
var char = string.charAt(i);
var otherChar = otherString.charAt(i);
if (char !== otherChar) {
return i;
}
i++;
}
if (string.length === otherString.length) {
return -1;
} // they differ at maxCommonLength
return maxCommonLength;
};
var urlToRelativeUrl = function urlToRelativeUrl(urlArg, baseUrlArg) {
var url = new URL(urlArg);
var baseUrl = new URL(baseUrlArg);
if (url.protocol !== baseUrl.protocol) {
return urlArg;
}
if (url.username !== baseUrl.username || url.password !== baseUrl.password) {
return urlArg.slice(url.protocol.length);
}
if (url.host !== baseUrl.host) {
return urlArg.slice(url.protocol.length);
}
var pathname = url.pathname,
hash = url.hash,
search = url.search;
if (pathname === "/") {
return baseUrl.pathname.slice(1);
}
var basePathname = baseUrl.pathname;
var commonPathname = getCommonPathname(pathname, basePathname);
if (!commonPathname) {
return urlArg;
}
var specificPathname = pathname.slice(commonPathname.length);
var baseSpecificPathname = basePathname.slice(commonPathname.length);
if (baseSpecificPathname.includes("/")) {
var baseSpecificParentPathname = pathnameToParentPathname(baseSpecificPathname);
var relativeDirectoriesNotation = baseSpecificParentPathname.replace(/.*?\//g, "../");
return "".concat(relativeDirectoriesNotation).concat(specificPathname).concat(search).concat(hash);
}
return "".concat(specificPathname).concat(search).concat(hash);
};
var moveImportMap = function moveImportMap(importMap, fromUrl, toUrl) {
assertImportMap(importMap);
var makeRelativeTo = function makeRelativeTo(value, type) {
var url;
if (type === "specifier") {
url = resolveSpecifier(value, fromUrl);
if (!url) {
// bare specifier
return value;
}
} else {
url = resolveUrl(value, fromUrl);
}
var relativeUrl = urlToRelativeUrl(url, toUrl);
if (relativeUrl.startsWith("../")) {
return relativeUrl;
}
if (relativeUrl.startsWith("./")) {
return relativeUrl;
}
if (hasScheme(relativeUrl)) {
return relativeUrl;
}
return "./".concat(relativeUrl);
};
var importMapRelative = {};

@@ -453,3 +570,3 @@ var imports = importMap.imports;

if (imports) {
importMapRelative.imports = makeImportsRelativeTo(imports, url) || imports;
importMapRelative.imports = makeImportsRelativeTo(imports, makeRelativeTo) || imports;
}

@@ -460,3 +577,8 @@

if (scopes) {
importMapRelative.scopes = makeScopedRemappingRelativeTo(scopes, url) || scopes;
importMapRelative.scopes = makeScopedRemappingRelativeTo(scopes, makeRelativeTo) || scopes;
} // nothing changed
if (importMapRelative.imports === imports && importMapRelative.scopes === scopes) {
return importMap;
}

@@ -467,3 +589,3 @@

var makeScopedRemappingRelativeTo = function makeScopedRemappingRelativeTo(scopes, url) {
var makeScopedRemappingRelativeTo = function makeScopedRemappingRelativeTo(scopes, makeRelativeTo) {
var scopesTransformed = {};

@@ -474,4 +596,4 @@ var scopesRemaining = {};

var scopeValue = scopes[scopeKey];
var scopeKeyRelative = makeAddressRelativeTo(scopeKey, url);
var scopeValueRelative = makeImportsRelativeTo(scopeValue, url);
var scopeKeyRelative = makeRelativeTo(scopeKey, "address");
var scopeValueRelative = makeImportsRelativeTo(scopeValue, makeRelativeTo);

@@ -491,3 +613,3 @@ if (scopeKeyRelative) {

var makeImportsRelativeTo = function makeImportsRelativeTo(imports, url) {
var makeImportsRelativeTo = function makeImportsRelativeTo(imports, makeRelativeTo) {
var importsTransformed = {};

@@ -498,4 +620,4 @@ var importsRemaining = {};

var importValue = imports[importKey];
var importKeyRelative = makeSpecifierRelativeTo(importKey, url);
var importValueRelative = makeAddressRelativeTo(importValue, url);
var importKeyRelative = makeRelativeTo(importKey, "specifier");
var importValueRelative = makeRelativeTo(importValue, "address");

@@ -515,52 +637,2 @@ if (importKeyRelative) {

var makeSpecifierRelativeTo = function makeSpecifierRelativeTo(specifier, url) {
if (specifier.startsWith("//")) {
return null;
}
if (specifier[0] === "/") {
return null;
}
if (specifier.startsWith("./")) {
return makeRelativeTo(specifier.slice(2), url);
}
if (specifier.startsWith("../")) {
return makeRelativeTo(specifier, url);
}
return null;
};
var makeAddressRelativeTo = function makeAddressRelativeTo(address, url) {
if (address.startsWith("//")) {
return null;
}
if (address[0] === "/") {
return null;
}
if (address.startsWith("./")) {
return makeRelativeTo(address.slice(2), url);
}
if (address.startsWith("../")) {
return makeRelativeTo(address, url);
}
if (hasScheme(address)) {
return null;
} // bare
return makeRelativeTo(address, url);
};
var makeRelativeTo = function makeRelativeTo(string, url) {
var slashCount = urlToPathname(url).slice(1).split("/").length - 1;
return "".concat("../".repeat(slashCount)).concat(string);
};
var sortImportMap = function sortImportMap(importMap) {

@@ -763,3 +835,3 @@ assertImportMap(importMap);

export { applyImportMap, composeTwoImportMaps, makeImportMapRelativeTo, normalizeImportMap, resolveImport, resolveSpecifier, resolveUrl, sortImportMap };
export { applyImportMap, composeTwoImportMaps, moveImportMap, normalizeImportMap, resolveImport, resolveSpecifier, resolveUrl, sortImportMap };
//# sourceMappingURL=main.js.map
export { applyImportMap } from "./src/applyImportMap.js"
export { composeTwoImportMaps } from "./src/composeTwoImportMaps.js"
export { makeImportMapRelativeTo } from "./src/makeImportMapRelativeTo.js"
export { moveImportMap } from "./src/moveImportMap.js"
export { normalizeImportMap } from "./src/normalizeImportMap.js"

@@ -5,0 +5,0 @@ export { resolveImport } from "./src/resolveImport.js"

{
"name": "@jsenv/import-map",
"version": "6.8.1",
"version": "6.9.0",
"description": "Helpers to implement importmaps.",

@@ -5,0 +5,0 @@ "license": "MIT",

@@ -17,2 +17,3 @@ # import-map

- [resolveImport](#resolveimport)
- [moveImportMap](#moveImportMap)

@@ -120,1 +121,32 @@ # Presentation

— source code at [src/resolveImport.js](./src/resolveImport.js).
# moveImportMap
`moveImportMap` receives `importMap`, `fromUrl`, `toUrl` and return an importmap where all relative urls and specifiers becomes relative to `toUrl` instead of `fromUrl`.
This function exists in case you need to move an importmap file somewhere else in the filesystem. This is not a common use case but might happen.
```js
import { moveImportMap } from "@jsenv/import-map"
const importMapMoved = moveImportMap(
{
imports: {
foo: "./foo.js",
},
},
"file:///project/project.importmap",
"file:///project/dir/project.importmap",
)
console.log(JSON.stringify(importMap, null, ' ')
```
```console
{
"imports": {
"foo": "../foo.js",
}
}
```
— source code at [src/moveImportMap.js](./src/moveImportMap.js).

@@ -5,3 +5,8 @@ import { hasScheme } from "./internal/hasScheme.js"

export const resolveSpecifier = (specifier, importer) => {
if (specifier[0] === "/" || specifier.startsWith("./") || specifier.startsWith("../")) {
if (
specifier === "." ||
specifier[0] === "/" ||
specifier.startsWith("./") ||
specifier.startsWith("../")
) {
return resolveUrl(specifier, importer)

@@ -8,0 +13,0 @@ }

@@ -6,3 +6,3 @@ // could be useful: https://url.spec.whatwg.org/#url-miscellaneous

import { urlToOrigin } from "./internal/urlToOrigin.js"
import { pathnameToDirectoryPathname } from "./internal/pathnameToDirectoryPathname.js"
import { pathnameToParentPathname } from "./internal/pathnameToParentPathname.js"
import { hasScheme } from "./internal/hasScheme.js"

@@ -42,4 +42,4 @@

if (specifier === ".") {
const baseDirectoryPathname = pathnameToDirectoryPathname(basePathname)
return `${baseOrigin}${baseDirectoryPathname}/`
const baseDirectoryPathname = pathnameToParentPathname(basePathname)
return `${baseOrigin}${baseDirectoryPathname}`
}

@@ -49,4 +49,4 @@

if (specifier.slice(0, 2) === "./") {
const baseDirectoryPathname = pathnameToDirectoryPathname(basePathname)
return `${baseOrigin}${baseDirectoryPathname}/${specifier.slice(2)}`
const baseDirectoryPathname = pathnameToParentPathname(basePathname)
return `${baseOrigin}${baseDirectoryPathname}${specifier.slice(2)}`
}

@@ -80,3 +80,3 @@

}
return `${baseOrigin}${pathnameToDirectoryPathname(basePathname)}/${specifier}`
return `${baseOrigin}${pathnameToParentPathname(basePathname)}${specifier}`
}

@@ -83,0 +83,0 @@

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc