Initial commit
This commit is contained in:
21
node_modules/resolve-pkg-maps/LICENSE
generated
vendored
Normal file
21
node_modules/resolve-pkg-maps/LICENSE
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) Hiroki Osame <hiroki.osame@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
216
node_modules/resolve-pkg-maps/README.md
generated
vendored
Normal file
216
node_modules/resolve-pkg-maps/README.md
generated
vendored
Normal file
@ -0,0 +1,216 @@
|
||||
# resolve-pkg-maps
|
||||
|
||||
Utils to resolve `package.json` subpath & conditional [`exports`](https://nodejs.org/api/packages.html#exports)/[`imports`](https://nodejs.org/api/packages.html#imports) in resolvers.
|
||||
|
||||
Implements the [ESM resolution algorithm](https://nodejs.org/api/esm.html#resolver-algorithm-specification). Tested [against Node.js](/tests/) for accuracy.
|
||||
|
||||
<sub>Support this project by ⭐️ starring and sharing it. [Follow me](https://github.com/privatenumber) to see what other cool projects I'm working on! ❤️</sub>
|
||||
|
||||
## Usage
|
||||
|
||||
### Resolving `exports`
|
||||
|
||||
_utils/package.json_
|
||||
```json5
|
||||
{
|
||||
// ...
|
||||
"exports": {
|
||||
"./reverse": {
|
||||
"require": "./file.cjs",
|
||||
"default": "./file.mjs"
|
||||
}
|
||||
},
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
```ts
|
||||
import { resolveExports } from 'resolve-pkg-maps'
|
||||
|
||||
const [packageName, packageSubpath] = parseRequest('utils/reverse')
|
||||
|
||||
const resolvedPaths: string[] = resolveExports(
|
||||
getPackageJson(packageName).exports,
|
||||
packageSubpath,
|
||||
['import', ...otherConditions]
|
||||
)
|
||||
// => ['./file.mjs']
|
||||
```
|
||||
|
||||
### Resolving `imports`
|
||||
|
||||
_package.json_
|
||||
```json5
|
||||
{
|
||||
// ...
|
||||
"imports": {
|
||||
"#supports-color": {
|
||||
"node": "./index.js",
|
||||
"default": "./browser.js"
|
||||
}
|
||||
},
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
```ts
|
||||
import { resolveImports } from 'resolve-pkg-maps'
|
||||
|
||||
const resolvedPaths: string[] = resolveImports(
|
||||
getPackageJson('.').imports,
|
||||
'#supports-color',
|
||||
['node', ...otherConditions]
|
||||
)
|
||||
// => ['./index.js']
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### resolveExports(exports, request, conditions)
|
||||
|
||||
Returns: `string[]`
|
||||
|
||||
Resolves the `request` based on `exports` and `conditions`. Returns an array of paths (e.g. in case a fallback array is matched).
|
||||
|
||||
#### exports
|
||||
|
||||
Type:
|
||||
```ts
|
||||
type Exports = PathOrMap | readonly PathOrMap[]
|
||||
|
||||
type PathOrMap = string | PathConditionsMap
|
||||
|
||||
type PathConditionsMap = {
|
||||
[condition: string]: PathConditions | null
|
||||
}
|
||||
```
|
||||
|
||||
The [`exports` property](https://nodejs.org/api/packages.html#exports) value in `package.json`.
|
||||
|
||||
#### request
|
||||
|
||||
Type: `string`
|
||||
|
||||
The package subpath to resolve. Assumes a normalized path is passed in (eg. [repeating slashes `//`](https://github.com/nodejs/node/issues/44316)).
|
||||
|
||||
It _should not_ start with `/` or `./`.
|
||||
|
||||
Example: if the full import path is `some-package/subpath/file`, the request is `subpath/file`.
|
||||
|
||||
|
||||
#### conditions
|
||||
|
||||
Type: `readonly string[]`
|
||||
|
||||
An array of conditions to use when resolving the request. For reference, Node.js's default conditions are [`['node', 'import']`](https://nodejs.org/api/esm.html#:~:text=defaultConditions%20is%20the%20conditional%20environment%20name%20array%2C%20%5B%22node%22%2C%20%22import%22%5D.).
|
||||
|
||||
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.
|
||||
|
||||
---
|
||||
|
||||
### resolveImports(imports, request, conditions)
|
||||
|
||||
Returns: `string[]`
|
||||
|
||||
Resolves the `request` based on `imports` and `conditions`. Returns an array of paths (e.g. in case a fallback array is matched).
|
||||
|
||||
#### imports
|
||||
|
||||
Type:
|
||||
```ts
|
||||
type Imports = {
|
||||
[condition: string]: PathOrMap | readonly PathOrMap[] | null
|
||||
}
|
||||
|
||||
type PathOrMap = string | Imports
|
||||
```
|
||||
|
||||
The [`imports` property](https://nodejs.org/api/packages.html#imports) value in `package.json`.
|
||||
|
||||
|
||||
#### request
|
||||
|
||||
Type: `string`
|
||||
|
||||
The request resolve. Assumes a normalized path is passed in (eg. [repeating slashes `//`](https://github.com/nodejs/node/issues/44316)).
|
||||
|
||||
> **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.
|
||||
|
||||
#### conditions
|
||||
|
||||
Type: `readonly string[]`
|
||||
|
||||
An array of conditions to use when resolving the request. For reference, Node.js's default conditions are [`['node', 'import']`](https://nodejs.org/api/esm.html#:~:text=defaultConditions%20is%20the%20conditional%20environment%20name%20array%2C%20%5B%22node%22%2C%20%22import%22%5D.).
|
||||
|
||||
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.
|
||||
|
||||
---
|
||||
|
||||
### Errors
|
||||
|
||||
#### `ERR_PACKAGE_PATH_NOT_EXPORTED`
|
||||
- If the request is not exported by the export map
|
||||
|
||||
#### `ERR_PACKAGE_IMPORT_NOT_DEFINED`
|
||||
- If the request is not defined by the import map
|
||||
|
||||
#### `ERR_INVALID_PACKAGE_CONFIG`
|
||||
|
||||
- If an object contains properties that are both paths and conditions (e.g. start with and without `.`)
|
||||
- If an object contains numeric properties
|
||||
|
||||
#### `ERR_INVALID_PACKAGE_TARGET`
|
||||
- If a resolved exports path is not a valid path (e.g. not relative or has protocol)
|
||||
- If a resolved path includes `..` or `node_modules`
|
||||
- If a resolved path is a type that cannot be parsed
|
||||
|
||||
## FAQ
|
||||
|
||||
### Why do the APIs return an array of paths?
|
||||
|
||||
`exports`/`imports` supports passing in a [fallback array](https://github.com/jkrems/proposal-pkg-exports/#:~:text=Whenever%20there%20is,to%20new%20cases.) to provide fallback paths if the previous one is invalid:
|
||||
|
||||
```json5
|
||||
{
|
||||
"exports": {
|
||||
"./feature": [
|
||||
"./file.js",
|
||||
"./fallback.js"
|
||||
]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Node.js's implementation [picks the first valid path (without attempting to resolve it)](https://github.com/nodejs/node/issues/44282#issuecomment-1220151715) and throws an error if it can't be resolved. Node.js's fallback array is designed for [forward compatibility with features](https://github.com/jkrems/proposal-pkg-exports/#:~:text=providing%20forwards%20compatiblitiy%20for%20new%20features) (e.g. protocols) that can be immediately/inexpensively validated:
|
||||
|
||||
```json5
|
||||
{
|
||||
"exports": {
|
||||
"./core-polyfill": ["std:core-module", "./core-polyfill.js"]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
However, [Webpack](https://webpack.js.org/guides/package-exports/#alternatives) and [TypeScript](https://github.com/microsoft/TypeScript/blob/71e852922888337ef51a0e48416034a94a6c34d9/src/compiler/moduleSpecifiers.ts#L695) 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.
|
||||
|
||||
### How is it different from [`resolve.exports`](https://github.com/lukeed/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](https://nodejs.org/api/packages.html#self-referencing-a-package-using-its-name). 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](https://nodejs.org/docs/latest-v16.x/api/packages.html#subpath-folder-mappings) (deprecated in Node.js v16 & removed in v17) but seems to [have a bug](https://github.com/lukeed/resolve.exports/issues/7). 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](/tests/exports/compare-resolve.exports.ts).
|
||||
1
node_modules/resolve-pkg-maps/dist/index.cjs
generated
vendored
Executable file
1
node_modules/resolve-pkg-maps/dist/index.cjs
generated
vendored
Executable file
@ -0,0 +1 @@
|
||||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const d=r=>r!==null&&typeof r=="object",s=(r,t)=>Object.assign(new Error(`[${r}]: ${t}`),{code:r}),g="ERR_INVALID_PACKAGE_CONFIG",E="ERR_INVALID_PACKAGE_TARGET",I="ERR_PACKAGE_PATH_NOT_EXPORTED",P="ERR_PACKAGE_IMPORT_NOT_DEFINED",R=/^\d+$/,O=/^(\.{1,2}|node_modules)$/i,u=/\/|\\/;var h=(r=>(r.Export="exports",r.Import="imports",r))(h||{});const f=(r,t,n,o,c)=>{if(t==null)return[];if(typeof t=="string"){const[e,...i]=t.split(u);if(e===".."||i.some(l=>O.test(l)))throw s(E,`Invalid "${r}" target "${t}" defined in the package config`);return[c?t.replace(/\*/g,c):t]}if(Array.isArray(t))return t.flatMap(e=>f(r,e,n,o,c));if(d(t)){for(const e of Object.keys(t)){if(R.test(e))throw s(g,"Cannot contain numeric property keys");if(e==="default"||o.includes(e))return f(r,t[e],n,o,c)}return[]}throw s(E,`Invalid "${r}" target "${t}"`)},a="*",v=(r,t)=>{const n=r.indexOf(a),o=t.indexOf(a);return n===o?t.length>r.length:o>n};function A(r,t){if(!t.includes(a)&&r.hasOwnProperty(t))return[t];let n,o;for(const c of Object.keys(r))if(c.includes(a)){const[e,i,l]=c.split(a);if(l===void 0&&t.startsWith(e)&&t.endsWith(i)){const _=t.slice(e.length,-i.length||void 0);_&&(!n||v(n,c))&&(n=c,o=_)}}return[n,o]}const p=r=>Object.keys(r).reduce((t,n)=>{const o=n===""||n[0]!==".";if(t===void 0||t===o)return o;throw s(g,'"exports" cannot contain some keys starting with "." and some not')},void 0),w=/^\w+:/,m=(r,t,n)=>{if(!r)throw new Error('"exports" is required');t=t===""?".":`./${t}`,(typeof r=="string"||Array.isArray(r)||d(r)&&p(r))&&(r={".":r});const[o,c]=A(r,t),e=f(h.Export,r[o],t,n,c);if(e.length===0)throw s(I,t==="."?'No "exports" main defined':`Package subpath '${t}' is not defined by "exports"`);for(const i of e)if(!i.startsWith("./")&&!w.test(i))throw s(E,`Invalid "exports" target "${i}" defined in the package config`);return e},T=(r,t,n)=>{if(!r)throw new Error('"imports" is required');const[o,c]=A(r,t),e=f(h.Import,r[o],t,n,c);if(e.length===0)throw s(P,`Package import specifier "${t}" is not defined in package`);return e};exports.resolveExports=m,exports.resolveImports=T;
|
||||
11
node_modules/resolve-pkg-maps/dist/index.d.cts
generated
vendored
Normal file
11
node_modules/resolve-pkg-maps/dist/index.d.cts
generated
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
type PathConditionsMap = {
|
||||
[condition: string]: PathConditions | null;
|
||||
};
|
||||
type PathOrMap = string | PathConditionsMap;
|
||||
type PathConditions = PathOrMap | readonly PathOrMap[];
|
||||
|
||||
declare const resolveExports: (exports: PathConditions, request: string, conditions: readonly string[]) => string[];
|
||||
|
||||
declare const resolveImports: (imports: PathConditionsMap, request: string, conditions: readonly string[]) => string[];
|
||||
|
||||
export { PathConditions, PathConditionsMap, resolveExports, resolveImports };
|
||||
11
node_modules/resolve-pkg-maps/dist/index.d.mts
generated
vendored
Normal file
11
node_modules/resolve-pkg-maps/dist/index.d.mts
generated
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
type PathConditionsMap = {
|
||||
[condition: string]: PathConditions | null;
|
||||
};
|
||||
type PathOrMap = string | PathConditionsMap;
|
||||
type PathConditions = PathOrMap | readonly PathOrMap[];
|
||||
|
||||
declare const resolveExports: (exports: PathConditions, request: string, conditions: readonly string[]) => string[];
|
||||
|
||||
declare const resolveImports: (imports: PathConditionsMap, request: string, conditions: readonly string[]) => string[];
|
||||
|
||||
export { PathConditions, PathConditionsMap, resolveExports, resolveImports };
|
||||
1
node_modules/resolve-pkg-maps/dist/index.mjs
generated
vendored
Executable file
1
node_modules/resolve-pkg-maps/dist/index.mjs
generated
vendored
Executable file
@ -0,0 +1 @@
|
||||
const A=r=>r!==null&&typeof r=="object",a=(r,t)=>Object.assign(new Error(`[${r}]: ${t}`),{code:r}),_="ERR_INVALID_PACKAGE_CONFIG",E="ERR_INVALID_PACKAGE_TARGET",I="ERR_PACKAGE_PATH_NOT_EXPORTED",P="ERR_PACKAGE_IMPORT_NOT_DEFINED",R=/^\d+$/,O=/^(\.{1,2}|node_modules)$/i,w=/\/|\\/;var h=(r=>(r.Export="exports",r.Import="imports",r))(h||{});const f=(r,t,e,o,c)=>{if(t==null)return[];if(typeof t=="string"){const[n,...i]=t.split(w);if(n===".."||i.some(l=>O.test(l)))throw a(E,`Invalid "${r}" target "${t}" defined in the package config`);return[c?t.replace(/\*/g,c):t]}if(Array.isArray(t))return t.flatMap(n=>f(r,n,e,o,c));if(A(t)){for(const n of Object.keys(t)){if(R.test(n))throw a(_,"Cannot contain numeric property keys");if(n==="default"||o.includes(n))return f(r,t[n],e,o,c)}return[]}throw a(E,`Invalid "${r}" target "${t}"`)},s="*",m=(r,t)=>{const e=r.indexOf(s),o=t.indexOf(s);return e===o?t.length>r.length:o>e};function d(r,t){if(!t.includes(s)&&r.hasOwnProperty(t))return[t];let e,o;for(const c of Object.keys(r))if(c.includes(s)){const[n,i,l]=c.split(s);if(l===void 0&&t.startsWith(n)&&t.endsWith(i)){const g=t.slice(n.length,-i.length||void 0);g&&(!e||m(e,c))&&(e=c,o=g)}}return[e,o]}const p=r=>Object.keys(r).reduce((t,e)=>{const o=e===""||e[0]!==".";if(t===void 0||t===o)return o;throw a(_,'"exports" cannot contain some keys starting with "." and some not')},void 0),u=/^\w+:/,v=(r,t,e)=>{if(!r)throw new Error('"exports" is required');t=t===""?".":`./${t}`,(typeof r=="string"||Array.isArray(r)||A(r)&&p(r))&&(r={".":r});const[o,c]=d(r,t),n=f(h.Export,r[o],t,e,c);if(n.length===0)throw a(I,t==="."?'No "exports" main defined':`Package subpath '${t}' is not defined by "exports"`);for(const i of n)if(!i.startsWith("./")&&!u.test(i))throw a(E,`Invalid "exports" target "${i}" defined in the package config`);return n},T=(r,t,e)=>{if(!r)throw new Error('"imports" is required');const[o,c]=d(r,t),n=f(h.Import,r[o],t,e,c);if(n.length===0)throw a(P,`Package import specifier "${t}" is not defined in package`);return n};export{v as resolveExports,T as resolveImports};
|
||||
42
node_modules/resolve-pkg-maps/package.json
generated
vendored
Normal file
42
node_modules/resolve-pkg-maps/package.json
generated
vendored
Normal file
@ -0,0 +1,42 @@
|
||||
{
|
||||
"name": "resolve-pkg-maps",
|
||||
"version": "1.0.0",
|
||||
"description": "Resolve package.json exports & imports maps",
|
||||
"keywords": [
|
||||
"node.js",
|
||||
"package.json",
|
||||
"exports",
|
||||
"imports"
|
||||
],
|
||||
"license": "MIT",
|
||||
"repository": "privatenumber/resolve-pkg-maps",
|
||||
"funding": "https://github.com/privatenumber/resolve-pkg-maps?sponsor=1",
|
||||
"author": {
|
||||
"name": "Hiroki Osame",
|
||||
"email": "hiroki.osame@gmail.com"
|
||||
},
|
||||
"type": "module",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.mjs",
|
||||
"types": "./dist/index.d.cts",
|
||||
"exports": {
|
||||
"require": {
|
||||
"types": "./dist/index.d.cts",
|
||||
"default": "./dist/index.cjs"
|
||||
},
|
||||
"import": {
|
||||
"types": "./dist/index.d.mts",
|
||||
"default": "./dist/index.mjs"
|
||||
}
|
||||
},
|
||||
"imports": {
|
||||
"#resolve-pkg-maps": {
|
||||
"types": "./src/index.ts",
|
||||
"development": "./src/index.ts",
|
||||
"default": "./dist/index.mjs"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user