@jsenv/module-resolution
Advanced tools
Comparing version 2.5.0 to 3.0.0
@@ -68,2 +68,3 @@ 'use strict'; | ||
var pathnameSlashIndex = href.indexOf("/", secondProtocolSlashIndex); | ||
if (pathnameSlashIndex === -1) return href; | ||
return href.slice(0, pathnameSlashIndex); | ||
@@ -329,2 +330,38 @@ } | ||
var pathnameToExtension = function pathnameToExtension(pathname) { | ||
var slashLastIndex = pathname.lastIndexOf("/"); | ||
if (slashLastIndex !== -1) { | ||
pathname = pathname.slice(slashLastIndex + 1); | ||
} | ||
var dotLastIndex = pathname.lastIndexOf("."); | ||
if (dotLastIndex === -1) return ""; // if (dotLastIndex === pathname.length - 1) return "" | ||
return pathname.slice(dotLastIndex); | ||
}; | ||
var resolvePath = function resolvePath(_ref) { | ||
var specifier = _ref.specifier, | ||
importer = _ref.importer, | ||
importMap = _ref.importMap, | ||
defaultExtension = _ref.defaultExtension; | ||
var resolvedImport = resolveImport({ | ||
specifier: specifier, | ||
importer: importer | ||
}); | ||
var remappedImport = importMap ? remapResolvedImport({ | ||
resolvedImport: resolvedImport, | ||
importerHref: importer, | ||
importMap: importMap | ||
}) : resolvedImport; | ||
if (typeof defaultExtension === "string") { | ||
var extension = pathnameToExtension(remappedImport); | ||
if (extension === "") return "".concat(remappedImport).concat(defaultExtension); | ||
} | ||
return remappedImport; | ||
}; | ||
var normalizePathname = function normalizePathname(pathname) { | ||
@@ -340,2 +377,3 @@ pathname = pathname.replace(/\\/g, "/"); // we could also just ensure leading slash on pathname | ||
exports.resolvePath = resolvePath; | ||
exports.resolveImport = resolveImport; | ||
@@ -348,2 +386,3 @@ exports.remapResolvedImport = remapResolvedImport; | ||
exports.pathnameToDirname = pathnameToDirname; | ||
exports.pathnameToExtension = pathnameToExtension; | ||
//# sourceMappingURL=main.js.map |
@@ -66,2 +66,3 @@ var __dmail_assert__ = function (exports) { | ||
var pathnameSlashIndex = href.indexOf("/", secondProtocolSlashIndex); | ||
if (pathnameSlashIndex === -1) return href; | ||
return href.slice(0, pathnameSlashIndex); | ||
@@ -328,2 +329,38 @@ } | ||
var pathnameToExtension = function pathnameToExtension(pathname) { | ||
var slashLastIndex = pathname.lastIndexOf("/"); | ||
if (slashLastIndex !== -1) { | ||
pathname = pathname.slice(slashLastIndex + 1); | ||
} | ||
var dotLastIndex = pathname.lastIndexOf("."); | ||
if (dotLastIndex === -1) return ""; // if (dotLastIndex === pathname.length - 1) return "" | ||
return pathname.slice(dotLastIndex); | ||
}; | ||
var resolvePath = function resolvePath(_ref) { | ||
var specifier = _ref.specifier, | ||
importer = _ref.importer, | ||
importMap = _ref.importMap, | ||
defaultExtension = _ref.defaultExtension; | ||
var resolvedImport = resolveImport({ | ||
specifier: specifier, | ||
importer: importer | ||
}); | ||
var remappedImport = importMap ? remapResolvedImport({ | ||
resolvedImport: resolvedImport, | ||
importerHref: importer, | ||
importMap: importMap | ||
}) : resolvedImport; | ||
if (typeof defaultExtension === "string") { | ||
var extension = pathnameToExtension(remappedImport); | ||
if (extension === "") return "".concat(remappedImport).concat(defaultExtension); | ||
} | ||
return remappedImport; | ||
}; | ||
var normalizePathname = function normalizePathname(pathname) { | ||
@@ -339,2 +376,3 @@ pathname = pathname.replace(/\\/g, "/"); // we could also just ensure leading slash on pathname | ||
exports.resolvePath = resolvePath; | ||
exports.resolveImport = resolveImport; | ||
@@ -347,4 +385,5 @@ exports.remapResolvedImport = remapResolvedImport; | ||
exports.pathnameToDirname = pathnameToDirname; | ||
exports.pathnameToExtension = pathnameToExtension; | ||
return exports; | ||
}({}); | ||
//# sourceMappingURL=./main.js.map |
// public export | ||
export { resolvePath } from "./src/resolvePath.js" | ||
// some generic helpers some other project uses | ||
export { resolveImport } from "./src/resolveImport.js" | ||
export { remapResolvedImport } from "./src/import-map/remapResolvedImport.js" | ||
export { remapResolvedImport } from "./src/remapResolvedImport.js" | ||
// some generic helpers some other project uses | ||
export { hrefToScheme } from "./src/hrefToScheme.js" | ||
@@ -11,1 +13,2 @@ export { hrefToOrigin } from "./src/hrefToOrigin.js" | ||
export { pathnameToDirname } from "./src/pathnameToDirname.js" | ||
export { pathnameToExtension } from "./src/pathnameToExtension.js" |
{ | ||
"name": "@jsenv/module-resolution", | ||
"version": "2.5.0", | ||
"version": "3.0.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -1,2 +0,2 @@ | ||
# jsenv-module-resolution | ||
# jsenv module resolution | ||
@@ -7,42 +7,77 @@ [![npm package](https://img.shields.io/npm/v/@jsenv/module-resolution.svg)](https://www.npmjs.com/package/@jsenv/module-resolution) | ||
The following code illustrates what import this project aims to resolve. | ||
> import resolution and remapping. | ||
## Introduction | ||
jsenv use its own import resolution mostly because it uses some features not fully available in systemjs and rollup.<br /> | ||
It uses `resolvePath` function exported here. | ||
## Installation | ||
```console | ||
npm install @jsenv/module-resolution | ||
``` | ||
## `resolvePath` | ||
> takes { specifier, importer, importMap, defaultExtension } and returns a resolved path. | ||
##### basic example | ||
```js | ||
// absolute | ||
import "https://domai.com:80/file.js" | ||
// protocol relative | ||
import "//file.js" | ||
// origin relative | ||
import "/root.js" | ||
// pathname relative beside | ||
import "./beside.js" | ||
// pathname relative below | ||
import "./folder/below.js" | ||
// pathname relative above | ||
import "../above.js" | ||
// bare | ||
import "fs" | ||
import { resolvePath } from "@jsenv/module-resolution" | ||
const resolvePath = resolveImport({ | ||
importer: "http://domain.com/folder/file.js", | ||
specifier: "../index.js", | ||
}) | ||
console.log(resolvePath) | ||
``` | ||
This project exports a `resolveImport` function capable to resolve these imports. | ||
The code above logs `http://domain.com/index.js`. | ||
## resolveImport | ||
##### remapping example | ||
Check [test](./test) folder to understand how this function works. | ||
```js | ||
import { resolvePath } from "@jsenv/module-resolution" | ||
## Bare import | ||
const resolvedPath = resolvePath({ | ||
importer: "http://domain.com/folder/file.js", | ||
specifier: "../index.js", | ||
importMap: { | ||
imports: { | ||
"/index.js": "/foo.js", | ||
}, | ||
}, | ||
}) | ||
Here are some examples of bar imports: `import "fs"`, `import "lodash"` | ||
or `import "@babel/core"`. | ||
console.log(resolvedPath) | ||
``` | ||
Inside a browser bare specifier will be [explicitely reserved](https://html.spec.whatwg.org/multipage/webappapis.html#resolve-a-module-specifier). | ||
This way third party module will not clash with future browser native modules. | ||
code above logs `http://domain.com/foo.js`. | ||
Inside node, [node module resolution algorithm](https://nodejs.org/api/modules.html#modules_all_together) find the corresponding file for you. | ||
It means node native module and third party module look the same. | ||
#### scoped remapping example | ||
# Installing | ||
```js | ||
import { resolvedPath } from "@jsenv/module-resolution" | ||
```console | ||
npm install --save-dev @jsenv/module-resolution | ||
const resolvedPath = resolvePath({ | ||
importer: "http://domain.com/folder/file.js", | ||
specifier: "../index.js", | ||
importMap: { | ||
imports: { | ||
"/index.js": "/foo.js", | ||
}, | ||
scopes: { | ||
"/folder/": { | ||
"/index.js": "/bar.js", | ||
}, | ||
}, | ||
}, | ||
}) | ||
console.log(remappedImport) | ||
``` | ||
code above logs `http://domain.com/bar.js`. |
@@ -14,2 +14,3 @@ import { hrefToScheme } from "./hrefToScheme.js" | ||
if (pathnameSlashIndex === -1) return href | ||
return href.slice(0, pathnameSlashIndex) | ||
@@ -16,0 +17,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
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
67000
24
1011
83
0