Skip to content

Commit ed5e1d7

Browse files
committed
docs: document finder functions
1 parent 6221c66 commit ed5e1d7

File tree

8 files changed

+147
-2
lines changed

8 files changed

+147
-2
lines changed

blog/releases/10.16.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Every matched package will also print out the license from its `package.json`:
9999
license: MIT
100100
```
101101

102-
### Patch Changes
102+
## Patch Changes
103103

104104
- Fix deprecation warning printed when executing pnpm with Node.js 24 [#9529](https://github.com/pnpm/pnpm/issues/9529).
105105
- Throw an error if `nodeVersion` is not set to an exact semver version [#9934](https://github.com/pnpm/pnpm/issues/9934).

docs/cli/list.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,7 @@ Exclude peer dependencies from the results (but dependencies of peer dependencie
6868
### --filter <package_selector\>
6969

7070
[Read more about filtering.](../filtering.md)
71+
72+
import FindBy from '../settings/_findBy.mdx'
73+
74+
<FindBy />

docs/cli/why.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,7 @@ Exclude peer dependencies from the results (but dependencies of peer dependencie
5959
### --filter &lt;package_selector\>
6060

6161
[Read more about filtering.](../filtering.md)
62+
63+
import FindBy from '../settings/_findBy.mdx'
64+
65+
<FindBy />

docs/finders.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
id: finders
3+
title: Finders
4+
---
5+
6+
Added in: v10.16.0
7+
8+
Finder functions let you **search your dependency graph** by any property of a package, not just its name.
9+
They can be declared in [.pnpmfile.cjs] and used with [pnpm list] and [pnpm why].
10+
11+
[.pnpmfile.cjs]: ./pnpmfile.md
12+
[pnpm list]: ./cli/list.md
13+
[pnpm why]: ./cli/why.md
14+
15+
## Defining finder functions
16+
17+
Finder functions are declared in your project’s [.pnpmfile.cjs] file under the finders export.
18+
Each function receives a context object and must return either:
19+
20+
* `true` → include this dependency in the results,
21+
* `false` → skip it,
22+
* or a `string` → include this dependency and print the string as additional info.
23+
24+
Example: a finder that matches any dependency with **React 17** in `peerDependencies`:
25+
26+
```js title=".pnpmfile.cjs"
27+
module.exports = {
28+
finders: {
29+
react17: (ctx) => {
30+
return ctx.readManifest().peerDependencies?.react === "^17.0.0"
31+
}
32+
}
33+
}
34+
```
35+
36+
### Finder context (ctx)
37+
38+
Each finder function receives a context object that describes the dependency node being visited.
39+
40+
|Field|Type/Example|Description|
41+
|--|--|--|
42+
|`name`|`"minimist"`|Package name.|
43+
|`version`|`"1.2.8"`|Package version.|
44+
|`readManifest()`|returns the `package.json` object|Load the package manifest (use this for fields like `peerDependencies`, `license`, `engines`, etc.).|
45+
46+
## Using finders
47+
48+
You can invoke a finder with the `--find-by=<functionName>` flag:
49+
50+
```
51+
pnpm why --find-by=react17
52+
```
53+
54+
Output:
55+
56+
```
57+
@apollo/client 4.0.4
58+
├── @graphql-typed-document-node/core 3.2.0
59+
└── graphql-tag 2.12.6
60+
```
61+
62+
## Returning extra metadata
63+
64+
A finder can also return a string. That string will be shown alongside the matched package in the output.
65+
66+
Example: print the package license:
67+
68+
```js
69+
module.exports = {
70+
finders: {
71+
react17: (ctx) => {
72+
const manifest = ctx.readManifest()
73+
if (manifest.peerDependencies?.react === "^17.0.0") {
74+
return `license: ${manifest.license}`
75+
}
76+
return false
77+
}
78+
}
79+
}
80+
```
81+
82+
Output:
83+
84+
```
85+
@apollo/client 4.0.4
86+
├── @graphql-typed-document-node/core 3.2.0
87+
│ license: MIT
88+
└── graphql-tag 2.12.6
89+
license: MIT
90+
```
91+
92+
Othere example use cases:
93+
* Find all packages with a specific license.
94+
* Detect packages requiring a minimum Node.js version.
95+
* List all dependencies that expose binaries.
96+
* Print funding URLs for all packages.
97+

docs/pnpmfile.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,34 @@ This hook allows to override the fetchers that are used for different types of d
174174
* `directory`
175175
* `git`
176176

177+
## Finders
178+
179+
Added in: v10.16.0
180+
181+
Finder functions are used with `pnpm list` and `pnpm why` via the `--find-by` flag.
182+
183+
Example:
184+
185+
```js title=".pnpmfile.cjs"
186+
module.exports = {
187+
finders: {
188+
react17: (ctx) => {
189+
return ctx.readManifest().peerDependencies?.react === "^17.0.0"
190+
}
191+
}
192+
}
193+
```
194+
195+
Usage:
196+
197+
```
198+
pnpm why --find-by=react17
199+
```
200+
201+
See [Finders] for more details.
202+
203+
[Finders]: ./finders.md
204+
177205
## Related Configuration
178206
179207
import IgnorePnpmfile from './settings/_ignorePnpmfile.mdx'

docs/settings/_findBy.mdx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### --find-by &lt;finder_name\>
2+
3+
Added in: v10.16.0
4+
5+
Use a [finder function] defined in `.pnpmfile.cjs` to match dependencies by properties other than name.
6+
7+
[finder function]: ../finders.md

sidebars.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@
113113
"config-dependencies",
114114
"aliases",
115115
"completion",
116-
"git_branch_lockfiles"
116+
"git_branch_lockfiles",
117+
"finders"
117118
],
118119
"Recipes": [
119120
"using-changesets",

versioned_sidebars/version-10.x-sidebars.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,10 @@
347347
{
348348
"type": "doc",
349349
"id": "git_branch_lockfiles"
350+
},
351+
{
352+
"type": "doc",
353+
"id": "finders"
350354
}
351355
]
352356
},

0 commit comments

Comments
 (0)