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 package.json `exports` & `imports` maps
npm install resolve-pkg-maps
Typescript
Module System
Node Version
NPM Version
TypeScript (99.22%)
JavaScript (0.78%)
Total Downloads
593,444,585
Last Day
2,294,040
Last Week
10,676,447
Last Month
46,596,485
Last Year
474,534,018
58 Stars
7 Commits
2 Forks
4 Watching
2 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
1.0.0
Package Id
resolve-pkg-maps@1.0.0
Unpacked Size
14.68 kB
Size
5.07 kB
File Count
7
NPM Version
8.11.0
Node Version
16.15.1
Cumulative downloads
Total Downloads
Last day
-4%
2,294,040
Compared to previous day
Last week
-13.3%
10,676,447
Compared to previous week
Last month
4.5%
46,596,485
Compared to previous month
Last year
299.1%
474,534,018
Compared to previous year
No dependencies detected.
Utils to resolve package.json
subpath & conditional exports
/imports
in resolvers.
Implements the ESM resolution algorithm. Tested against Node.js for accuracy.
Support this project by ⭐️ starring and sharing it. Follow me to see what other cool projects I'm working on! ❤️
exports
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
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 immediately/inexpensively 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.
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.
No vulnerabilities found.
No security vulnerabilities found.