Skip to content

Commit 16d03f7

Browse files
bytedoewebpro
andauthored
feat: support configuring preprocessors in knip.json (#1920)
Co-authored-by: Lars Kappert <lars@webpro.nl>
1 parent 90b384d commit 16d03f7

44 files changed

Lines changed: 476 additions & 59 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎packages/docs/src/content/docs/features/reporters.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,14 @@ Example usage:
377377
knip --preprocessor ./preprocess.ts
378378
```
379379

380+
Preprocessors can also be configured in the Knip configuration file:
381+
382+
```json title="knip.json"
383+
{
384+
"preprocessor": "./preprocess.ts"
385+
}
386+
```
387+
380388
[1]: #codeclimate
381389
[2]: #codeowners
382390
[3]: #cycles

‎packages/docs/src/content/docs/reference/cli.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ knip --preprocessor ./my-preprocessor.ts
322322
knip --preprocessor preprocessor-package
323323
```
324324

325+
Preprocessors can also be configured in the Knip configuration file.
326+
325327
Also see [Reporters & Preprocessors][15].
326328

327329
### `--preprocessor-options [json]`

‎packages/docs/src/content/docs/reference/configuration.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,29 @@ letters and avoid snake-case.
213213

214214
Also see [JSDoc & TSDoc Tags][8].
215215

216+
### `preprocessor`
217+
218+
Preprocess the results before providing them to the reporter(s). Can be a single preprocessor or an array of preprocessors. Each value is a path to a local file or an npm package name.
219+
220+
```json title="knip.json"
221+
{
222+
"preprocessor": ["./first.ts", "./second.ts"]
223+
}
224+
```
225+
226+
Also see [Preprocessors](../features/reporters.md#preprocessors).
227+
228+
### `preprocessorOptions`
229+
230+
Extra options to pass to the preprocessor.
231+
232+
```json title="knip.json"
233+
{
234+
"preprocessor": "./my-preprocessor.ts",
235+
"preprocessorOptions": { "key": "value" }
236+
}
237+
```
238+
216239
### `treatConfigHintsAsErrors`
217240

218241
Exit with non-zero code (1) if there are any configuration hints.

‎packages/knip/fixtures/cli-preprocessor/node_modules/knip-preprocessor/package.json‎

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{
22
"name": "@fixtures/cli-preprocessor",
3+
"devDependencies": {
4+
"knip-preprocessor": "1.0.0"
5+
},
36
"knip": {
7+
"preprocessor": "knip-preprocessor",
48
"entry": ["index.{js,ts}", "chain-{async,sync}.js"]
59
}
610
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function (options) {
2+
return { ...options, chain: 'first' };
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "@fixtures/config-preprocessor-chain",
3+
"knip": {
4+
"preprocessor": ["./first.js", "./second.js"]
5+
}
6+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default function (options) {
2+
console.log(`hi from ${options.chain} then second preprocessor`);
3+
return options;
4+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "@fixtures/config-preprocessor-options",
3+
"knip": {
4+
"preprocessor": "./preprocessor.js",
5+
"preprocessorOptions": { "food": "cupcake" }
6+
}
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default function (options) {
2+
console.log(`hi from config preprocessor, you gave me: ${JSON.parse(options.preprocessorOptions).food}`);
3+
return options;
4+
}

0 commit comments

Comments
 (0)