Gathering detailed insights and metrics for resolve-pkg-maps
Gathering detailed insights and metrics for resolve-pkg-maps
Gathering detailed insights and metrics for resolve-pkg-maps
Gathering detailed insights and metrics for resolve-pkg-maps
resolve-pkg
Resolve the path of a package regardless of it having an entry point
import-from
Import a module like with `require()` but from a given path
import-meta-resolve
Resolve things like Node.js — ponyfill for `import.meta.resolve`
@dual-bundle/import-meta-resolve
A fork of `import-meta-resolve` with commonjs + ESM support at the same time, AKA dual package.
npm install resolve-pkg-maps
99.4
Supply Chain
96.2
Quality
75
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
58 Stars
7 Commits
1 Forks
4 Watching
2 Branches
1 Contributors
Updated on 30 Oct 2024
TypeScript (99.22%)
JavaScript (0.78%)
Cumulative downloads
Total Downloads
Last day
-5.7%
2,111,545
Compared to previous day
Last week
2.9%
12,076,246
Compared to previous week
Last month
9.3%
50,312,057
Compared to previous month
Last year
462.9%
423,191,970
Compared to previous year
No dependencies detected.
Utils to resolve package.json
subpath & conditional exports
/imports
maps in resolvers.
Implements the ESM resolution algorithm. Tested against Node.js for accuracy.
→ Try it out online on StackBlitz
Support this project by ⭐️ starring and sharing it. Follow me to see what other cool projects I'm working on! ❤️
1npm i resolve-pkg-maps
exports
Let's say you want to resolve the subpath export reverse
in a package called utils
.
utils/package.json
1{ 2 // ... 3 "exports": { 4 "./reverse": { 5 "require": "./file.cjs", 6 "default": "./file.mjs" 7 } 8 }, 9 // ... 10}
1import { resolveExports } from 'resolve-pkg-maps' 2 3const [packageName, packageSubpath] = parseRequest('utils/reverse') 4 5const resolvedPaths: string[] = resolveExports( 6 getPackageJson(packageName).exports, 7 packageSubpath, 8 ['import', ...otherConditions] 9) 10// => ['./file.mjs']
imports
Let's say you want to resolve the subpath import #supports-color
in the current package.
package.json
1{ 2 // ... 3 "imports": { 4 "#supports-color": { 5 "node": "./index.js", 6 "default": "./browser.js" 7 } 8 }, 9 // ... 10}
1import { resolveImports } from 'resolve-pkg-maps' 2 3const resolvedPaths: string[] = resolveImports( 4 getPackageJson('.').imports, 5 '#supports-color', 6 ['node', ...otherConditions] 7) 8// => ['./index.js']
Returns: string[]
Resolves the request
based on exports
and conditions
. Returns an array of paths (e.g. in case a fallback array is matched).
Type:
1type Exports = PathOrMap | readonly PathOrMap[] 2 3type PathOrMap = string | PathConditionsMap 4 5type PathConditionsMap = { 6 [condition: string]: PathConditions | null 7}
The exports
property value in package.json
.
Type: string
The package subpath to resolve. Assumes a normalized path is passed in (eg. repeating slashes //
).
It should not start with /
or ./
.
Example: if the full import path is some-package/subpath/file
, the request is subpath/file
.
Type: readonly string[]
An array of conditions to use when resolving the request. For reference, Node.js's default conditions are ['node', 'import']
.
The order of this array does not matter; the order of condition keys in the export map is what matters instead.
Not all conditions in the array need to be met to resolve the request. It just needs enough to resolve to a path.
Returns: string[]
Resolves the request
based on imports
and conditions
. Returns an array of paths (e.g. in case a fallback array is matched).
Type:
1type Imports = { 2 [condition: string]: PathOrMap | readonly PathOrMap[] | null 3} 4 5type PathOrMap = string | Imports
The imports
property value in package.json
.
Type: string
The request resolve. Assumes a normalized path is passed in (eg. repeating slashes //
).
Note: In Node.js, imports resolutions are limited to requests prefixed with
#
. However, this package does not enforce that requirement in case you want to add custom support for non-prefixed entries.
Type: readonly string[]
An array of conditions to use when resolving the request. For reference, Node.js's default conditions are ['node', 'import']
.
The order of this array does not matter; the order of condition keys in the import map is what matters instead.
Not all conditions in the array need to be met to resolve the request. It just needs enough to resolve to a path.
ERR_PACKAGE_PATH_NOT_EXPORTED
ERR_PACKAGE_IMPORT_NOT_DEFINED
ERR_INVALID_PACKAGE_CONFIG
.
)ERR_INVALID_PACKAGE_TARGET
..
or node_modules
exports
/imports
supports passing in a fallback array to provide fallback paths if the previous one is invalid:
1{ 2 "exports": { 3 "./feature": [ 4 "./file.js", 5 "./fallback.js" 6 ] 7 } 8}
Node.js's implementation picks the first valid path (without attempting to resolve it) and throws an error if it can't be resolved. Node.js's fallback array is designed for forward compatibility with features (e.g. protocols) that can be syntactically validated:
1{ 2 "exports": { 3 "./core-polyfill": ["std:core-module", "./core-polyfill.js"] 4 } 5}
However, Webpack and TypeScript have deviated from this behavior and attempts to resolve the next path if a path cannot be resolved.
By returning an array of matched paths instead of just the first one, the user can decide which behavior to adopt.
Even though it returns an array of results, the conditions inside the array are resolved and non-matching conditions are filtered out.
For example, given the following exports:
1{ 2 "exports": { 3 "./path": [{ "node": "./node.js" }, "./browser.js"] 4 } 5}
Resolving with condition node
:
1["./node.js", "./browser.js"]
Resolving with condition browser
:
1["./browser.js"]
resolve.exports
?resolve.exports
only resolves exports
, whereas this package resolves both exports
& imports
. This comparison will only cover resolving exports
.
Despite it's name, resolve.exports
handles more than just exports
. It takes in the entire package.json
object to handle resolving .
and self-references. This package only accepts exports
/imports
maps from package.json
and is scoped to only resolving what's defined in the maps.
resolve.exports
accepts the full request (e.g. foo/bar
), whereas this package only accepts the requested subpath (e.g. bar
).
resolve.exports
only returns the first result in a fallback array. This package returns an array of results for the user to decide how to handle it.
resolve.exports
supports subpath folder mapping (deprecated in Node.js v16 & removed in v17) but seems to have a bug. This package does not support subpath folder mapping because Node.js has removed it in favor of using subpath patterns.
Neither resolvers rely on a file-system
This package also addresses many of the bugs in resolve.exports
, demonstrated in this test.
Get all entry-points for an npm package. Supports the exports field to expand subpaths and condition combinations.
No vulnerabilities found.
No security vulnerabilities found.