@knip/mcp 0.0.4 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/docs/blog/brief-history.md +30 -0
- package/docs/docs/blog/for-editors-and-agents.md +127 -0
- package/docs/docs/blog/knip-v3.mdx +88 -0
- package/docs/docs/blog/knip-v4.mdx +149 -0
- package/docs/docs/blog/knip-v5.mdx +190 -0
- package/docs/docs/blog/migration-to-v1.md +65 -0
- package/docs/docs/blog/release-notes-v2.md +46 -0
- package/docs/docs/blog/slim-down-to-speed-up.md +269 -0
- package/docs/docs/blog/state-of-knip.md +191 -0
- package/docs/docs/blog/two-years.mdx +107 -0
- package/docs/docs/explanations/comparison-and-migration.md +129 -0
- package/docs/docs/explanations/entry-files.md +70 -0
- package/docs/docs/explanations/plugins.md +319 -0
- package/docs/docs/explanations/why-use-knip.md +128 -0
- package/docs/docs/features/auto-fix.mdx +333 -0
- package/docs/docs/features/compilers.md +172 -0
- package/docs/docs/features/integrated-monorepos.md +61 -0
- package/docs/docs/features/monorepos-and-workspaces.md +134 -0
- package/docs/docs/features/production-mode.md +95 -0
- package/docs/docs/features/reporters.md +302 -0
- package/docs/docs/features/rules-and-filters.md +102 -0
- package/docs/docs/features/script-parser.md +156 -0
- package/docs/docs/features/source-mapping.md +100 -0
- package/docs/docs/guides/configuring-project-files.md +205 -0
- package/docs/docs/guides/contributing.md +24 -0
- package/docs/docs/guides/handling-issues.mdx +646 -0
- package/docs/docs/guides/issue-reproduction.md +94 -0
- package/docs/docs/guides/namespace-imports.md +125 -0
- package/docs/docs/guides/performance.md +97 -0
- package/docs/docs/guides/troubleshooting.md +136 -0
- package/docs/docs/guides/using-knip-in-ci.md +54 -0
- package/docs/docs/guides/working-with-commonjs.md +72 -0
- package/docs/docs/index.mdx +160 -0
- package/docs/docs/overview/configuration.md +104 -0
- package/docs/docs/overview/features.md +66 -0
- package/docs/docs/overview/getting-started.mdx +195 -0
- package/docs/docs/overview/screenshots-videos.md +42 -0
- package/docs/docs/playground.mdx +38 -0
- package/docs/docs/reference/cli.md +485 -0
- package/docs/docs/reference/configuration.md +413 -0
- package/docs/docs/reference/dynamic-configuration.mdx +72 -0
- package/docs/docs/reference/faq.md +441 -0
- package/docs/docs/reference/issue-types.md +43 -0
- package/docs/docs/reference/jsdoc-tsdoc-tags.md +122 -0
- package/docs/docs/reference/known-issues.md +64 -0
- package/docs/docs/reference/plugins/.gitkeep +0 -0
- package/docs/docs/reference/plugins.md +238 -0
- package/docs/docs/reference/related-tooling.md +46 -0
- package/docs/docs/sponsors.mdx +65 -0
- package/docs/docs/typescript/unused-dependencies.md +86 -0
- package/docs/docs/typescript/unused-exports.md +87 -0
- package/docs/docs/writing-a-plugin/argument-parsing.md +202 -0
- package/docs/docs/writing-a-plugin/index.md +376 -0
- package/docs/docs/writing-a-plugin/inputs.md +162 -0
- package/package.json +5 -3
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Issue Reproduction
|
|
3
|
+
sidebar:
|
|
4
|
+
order: 4
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
If you encounter an issue or false positives when using Knip, you can [open an
|
|
8
|
+
issue on GitHub][1]. This will help you in your project, and will also improve
|
|
9
|
+
Knip for everyone else!
|
|
10
|
+
|
|
11
|
+
Think of Knip as a kitchen sink, it handles a large amount of projects and
|
|
12
|
+
configurations, and your project is different from all others. Many factors may
|
|
13
|
+
influence the issue at hand, such as:
|
|
14
|
+
|
|
15
|
+
- Code syntax, import and export structure in source files
|
|
16
|
+
- Dependencies, scripts and entry files in `package.json`
|
|
17
|
+
- TypeScript configuration in `tsconfig.json`
|
|
18
|
+
- Enabled plugins and related configuration files
|
|
19
|
+
- Dependent or depending workspaces in a monorepo
|
|
20
|
+
- Knip configuration in `knip.json`
|
|
21
|
+
|
|
22
|
+
Create the minimum of source code and configuration with a few files to
|
|
23
|
+
reproduce and demonstrate the issue. Having this as a basis has many benefits:
|
|
24
|
+
|
|
25
|
+
- Minimize barriers and unrelated contextual overhead
|
|
26
|
+
- Optimize shared understanding of the situation
|
|
27
|
+
- Serves as a "contract" to fix the actual issue
|
|
28
|
+
- Files serve as a fixture to the project
|
|
29
|
+
|
|
30
|
+
Providing this with an issue description will help us help you and improve the
|
|
31
|
+
chances the issue can be looked into efficiently and in a timely manner.
|
|
32
|
+
|
|
33
|
+
## Before opening an issue
|
|
34
|
+
|
|
35
|
+
Before opening an issue, please make sure you:
|
|
36
|
+
|
|
37
|
+
- are using the latest version
|
|
38
|
+
- have read the relevant documentation
|
|
39
|
+
- have searched [existing issues][1]
|
|
40
|
+
- have checked the list of [known issues][2]
|
|
41
|
+
|
|
42
|
+
Please file only a single issue at a time, so each of them can be labeled and
|
|
43
|
+
tracked separately.
|
|
44
|
+
|
|
45
|
+
## Templates
|
|
46
|
+
|
|
47
|
+
A convenient way to create a minimal reproduction is by starting with one of
|
|
48
|
+
these templates in CodeSandbox or StackBlitz:
|
|
49
|
+
|
|
50
|
+
| Template | | |
|
|
51
|
+
| :------- | ---------------- | --------------- |
|
|
52
|
+
| Basic | [CodeSandbox][3] | [StackBlitz][4] |
|
|
53
|
+
| Monorepo | [CodeSandbox][5] | [StackBlitz][6] |
|
|
54
|
+
|
|
55
|
+
Shoutout to [CodeSandbox][7] and [StackBlitz][8] for generously providing these
|
|
56
|
+
free dev containers!
|
|
57
|
+
|
|
58
|
+
## Alternatives
|
|
59
|
+
|
|
60
|
+
Other solutions to share a minimal and reproducible case may work well too,
|
|
61
|
+
including:
|
|
62
|
+
|
|
63
|
+
- A public repository on e.g. GitHub or GitLab.
|
|
64
|
+
- A new [fixtures folder in the Knip repository][9].
|
|
65
|
+
|
|
66
|
+
The goal is to have an easy and common understanding and reproduction. A link to
|
|
67
|
+
your existing project repository will likely not be considered "minimal". Issues
|
|
68
|
+
containing just a screenshot, or snippets of output or source code don't provide
|
|
69
|
+
the full picture and aren't complete nor actionable.
|
|
70
|
+
|
|
71
|
+
If you're unable to create a reproduction using one of the methods described
|
|
72
|
+
then please clearly explain this in the issue or [contact me][10].
|
|
73
|
+
|
|
74
|
+
## Pull Request
|
|
75
|
+
|
|
76
|
+
The optimal way is to add fixtures and failing tests to the Knip repository, and
|
|
77
|
+
open a pull request to discuss the issue! Also see [instructions for
|
|
78
|
+
development][11].
|
|
79
|
+
|
|
80
|
+
[1]: https://github.com/webpro-nl/knip/issues?q=is%3Aissue
|
|
81
|
+
[2]: https://knip.dev/reference/known-issues
|
|
82
|
+
[3]:
|
|
83
|
+
https://codesandbox.io/p/devbox/github/webpro-nl/knip/main/templates/issue-reproduction/basic
|
|
84
|
+
[4]:
|
|
85
|
+
https://stackblitz.com/github/webpro-nl/knip/tree/main/templates/issue-reproduction/basic
|
|
86
|
+
[5]:
|
|
87
|
+
https://codesandbox.io/p/devbox/github/webpro-nl/knip/main/templates/issue-reproduction/monorepo
|
|
88
|
+
[6]:
|
|
89
|
+
https://stackblitz.com/github/webpro-nl/knip/tree/main/templates/issue-reproduction/monorepo
|
|
90
|
+
[7]: https://codesandbox.io
|
|
91
|
+
[8]: https://stackblitz.com
|
|
92
|
+
[9]: https://github.com/webpro-nl/knip/tree/main/packages/knip/fixtures
|
|
93
|
+
[10]: https://github.com/webpro
|
|
94
|
+
[11]: https://github.com/webpro-nl/knip/blob/main/.github/DEVELOPMENT.md
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Namespace Imports
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
The intention of exports used through namespace imports may not always be clear
|
|
6
|
+
to Knip. Here's a guide to better understand how Knip handles such exports.
|
|
7
|
+
|
|
8
|
+
## Example
|
|
9
|
+
|
|
10
|
+
We start off by having two exports:
|
|
11
|
+
|
|
12
|
+
```ts title="my-namespace.js"
|
|
13
|
+
export const version = 'v5';
|
|
14
|
+
export const getRocket = () => '🚀';
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
The next snippet shows how to import all the exports above on a namespace. All
|
|
18
|
+
exports of the `my-namespace.js` module will be members on the `NS` object:
|
|
19
|
+
|
|
20
|
+
```ts title="my-module.ts"
|
|
21
|
+
import * as NS from './my-namespace.js';
|
|
22
|
+
import send from 'stats';
|
|
23
|
+
send(NS);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
The intention of export usage is not always clear. In the example above is
|
|
27
|
+
`version` or `getRocket` used? We're not sure, but we _probably_ don't want them
|
|
28
|
+
to be reported as unused. The same goes for the next example:
|
|
29
|
+
|
|
30
|
+
```ts title="my-module.ts"
|
|
31
|
+
import * as NS from './my-namespace.js';
|
|
32
|
+
|
|
33
|
+
export { NS };
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
If this all usage of the `NS` namespace object, we also don't know whether
|
|
37
|
+
individual exports like `version` or `getRocket` will be used. However, if at
|
|
38
|
+
least one reference to a property such as `NS.version` is found, then the
|
|
39
|
+
individual exports are considered separately again and `getRocket` will be
|
|
40
|
+
marked as unused:
|
|
41
|
+
|
|
42
|
+
```ts title="index.ts"
|
|
43
|
+
import { NS } from './my-module.js';
|
|
44
|
+
|
|
45
|
+
const version = NS.version;
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## The default heuristic
|
|
49
|
+
|
|
50
|
+
Knip uses the following heuristic to determine which of the individual exports
|
|
51
|
+
are used:
|
|
52
|
+
|
|
53
|
+
- If there's one or more references to the import namespace object, but without
|
|
54
|
+
any property access, all exports on that namespace are considered used.
|
|
55
|
+
- Otherwise, exports are considered separately.
|
|
56
|
+
|
|
57
|
+
Below are a few more examples, and a way to disable this default behavior.
|
|
58
|
+
|
|
59
|
+
## Examples
|
|
60
|
+
|
|
61
|
+
Let's take a look at more examples:
|
|
62
|
+
|
|
63
|
+
```ts title="my-namespace.ts"
|
|
64
|
+
export const start = 1;
|
|
65
|
+
|
|
66
|
+
export const end = 1;
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
In the following cases all exports of `my-namespace.ts` are considered used:
|
|
70
|
+
|
|
71
|
+
```ts title="index.ts"
|
|
72
|
+
import * as NS from './my-namespace.js';
|
|
73
|
+
import send from 'stats';
|
|
74
|
+
|
|
75
|
+
send(NS);
|
|
76
|
+
|
|
77
|
+
const spread = { ...NS };
|
|
78
|
+
|
|
79
|
+
const shorthand = { NS };
|
|
80
|
+
|
|
81
|
+
const assignment = NS;
|
|
82
|
+
|
|
83
|
+
const item = [NS];
|
|
84
|
+
|
|
85
|
+
type TypeOf = typeof NS;
|
|
86
|
+
|
|
87
|
+
Object.values(NS);
|
|
88
|
+
|
|
89
|
+
for (const fruit in Fruits) {
|
|
90
|
+
//
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export { NS };
|
|
94
|
+
|
|
95
|
+
export { NS as AliasedNS };
|
|
96
|
+
|
|
97
|
+
export = NS;
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
However, this is no longer the case when one of the properties is accessed:
|
|
101
|
+
|
|
102
|
+
```ts title="index.js"
|
|
103
|
+
import * as NS from './namespace.js';
|
|
104
|
+
|
|
105
|
+
const begin = NS.start;
|
|
106
|
+
|
|
107
|
+
send(NS);
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
In this case, the `end` export will be reported as unused, even though the `NS`
|
|
111
|
+
object itself is referenced on its own as well.
|
|
112
|
+
|
|
113
|
+
## Include `nsExports` and `nsTypes`
|
|
114
|
+
|
|
115
|
+
To disable the heuristic as explained above, and enforce Knip to consider each
|
|
116
|
+
export on a namespace individually, include the `nsExports` issue type:
|
|
117
|
+
|
|
118
|
+
```json
|
|
119
|
+
{
|
|
120
|
+
"include": ["nsExports"]
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Or use the `--include nsExports` argument from the CLI. The `nsTypes` can be
|
|
125
|
+
added as well to do the same for exported types.
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Performance
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
This page describes a few topics around Knip's performance, and how you might
|
|
6
|
+
improve it.
|
|
7
|
+
|
|
8
|
+
Knip does not want to tell you how to structure files or how to write your code,
|
|
9
|
+
but it might still be good to understand inefficient patterns for Knip.
|
|
10
|
+
|
|
11
|
+
Use the `--debug` and `--performance` flags to find potential bottlenecks.
|
|
12
|
+
|
|
13
|
+
## Cache
|
|
14
|
+
|
|
15
|
+
Use `--cache` to speed up consecutive runs.
|
|
16
|
+
|
|
17
|
+
## Ignoring files
|
|
18
|
+
|
|
19
|
+
Files matching the `ignore` patterns are not excluded from the analysis. They're
|
|
20
|
+
just not printed in the report. Use negated `entry` and `project` patterns to
|
|
21
|
+
exclude files from the analysis.
|
|
22
|
+
|
|
23
|
+
Read [configuring project files][1] for details and examples. Improving
|
|
24
|
+
configuration may have a significant impact on performance.
|
|
25
|
+
|
|
26
|
+
## Workspace sharing
|
|
27
|
+
|
|
28
|
+
Knip shares files from separate workspaces if the configuration in
|
|
29
|
+
`tsconfig.json` allows this. This aims to reduce memory consumption and run
|
|
30
|
+
duration. Relevant compiler options include `baseUrl`, `paths` and
|
|
31
|
+
`moduleResolution`.
|
|
32
|
+
|
|
33
|
+
With the `--debug` flag you can see how many programs Knip uses. Look for
|
|
34
|
+
messages like this:
|
|
35
|
+
|
|
36
|
+
```sh
|
|
37
|
+
...
|
|
38
|
+
[*] Installed 2 programs for 29 workspaces
|
|
39
|
+
...
|
|
40
|
+
[*] Analyzing used resolved files [P1/1] (123)
|
|
41
|
+
...
|
|
42
|
+
[*] Analyzing used resolved files [P1/2] (8)
|
|
43
|
+
...
|
|
44
|
+
[*] Analyzing used resolved files [P2/1] (41)
|
|
45
|
+
...
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
The first number in `P1/1` is the number of the programs, the second number
|
|
49
|
+
indicates additional entry files were found so it does another round of analysis
|
|
50
|
+
on those files.
|
|
51
|
+
|
|
52
|
+
Use [--isolate-workspaces][2] to disable this behavior. This is usually not
|
|
53
|
+
necessary, but more of an escape hatch in cases with memory usage issues or
|
|
54
|
+
incompatible `compilerOptions` across workspaces. Workspaces are analyzed
|
|
55
|
+
sequentially to spread out memory usage more evenly, which may prevent crashes
|
|
56
|
+
on large monorepos.
|
|
57
|
+
|
|
58
|
+
## Language Service
|
|
59
|
+
|
|
60
|
+
Knip does not install the TypeScript Language Service (LS) by default. This is
|
|
61
|
+
expensive, as TypeScript needs to set up symbols and caching for the rather slow
|
|
62
|
+
`findReferences` function.
|
|
63
|
+
|
|
64
|
+
There are two cases that enforce Knip to install the LS.
|
|
65
|
+
|
|
66
|
+
### 1. Class members
|
|
67
|
+
|
|
68
|
+
The `findReferences` function is used to find unused members of imported classes
|
|
69
|
+
(i.e. when the issue type `classMembers` is included).
|
|
70
|
+
|
|
71
|
+
### 2. Include external type definitions
|
|
72
|
+
|
|
73
|
+
When [`--include-libs`][3] is enabled, Knip enables loading type definitions of
|
|
74
|
+
external dependencies. This will also install the LS to access its
|
|
75
|
+
`findReferences` function. It acts as an extra line of defense: only exports
|
|
76
|
+
that weren't referenced to during default procedure go through this.
|
|
77
|
+
|
|
78
|
+
## Metrics
|
|
79
|
+
|
|
80
|
+
Use [the `--performance` flag][4] to see how many times potentially expensive
|
|
81
|
+
functions (e.g. `findReferences`) are invoked and how much time is spent in
|
|
82
|
+
those functions. Example usage:
|
|
83
|
+
|
|
84
|
+
```sh
|
|
85
|
+
knip --include classMembers --performance
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## A last resort
|
|
89
|
+
|
|
90
|
+
In case Knip is unbearably slow (or even crashes), you could resort to [lint
|
|
91
|
+
individual workspaces][5].
|
|
92
|
+
|
|
93
|
+
[1]: ./configuring-project-files.md
|
|
94
|
+
[2]: ../reference/cli.md#--isolate-workspaces
|
|
95
|
+
[3]: ../guides/handling-issues.mdx#external-libraries
|
|
96
|
+
[4]: ../reference/cli.md#--performance
|
|
97
|
+
[5]: ../features/monorepos-and-workspaces.md#lint-a-single-workspace
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Troubleshooting
|
|
3
|
+
sidebar:
|
|
4
|
+
order: 2
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
We can distinguish two types of issues:
|
|
8
|
+
|
|
9
|
+
- [Lint issues reported by Knip][1]
|
|
10
|
+
- [Exceptions thrown by Knip][2]
|
|
11
|
+
|
|
12
|
+
Also see the [debug][3] and [trace][4] options below that can help to
|
|
13
|
+
troubleshoot issues.
|
|
14
|
+
|
|
15
|
+
:::note[Rationale]
|
|
16
|
+
|
|
17
|
+
The JavaScript/TypeScript ecosystem has a vast amount of frameworks and tools.
|
|
18
|
+
Additionally, file locations, configuration semantics, command-line arguments
|
|
19
|
+
and so on vary wildly. Files and dependencies are referenced in many ways. Knip
|
|
20
|
+
tries harder than you think to cover it all.
|
|
21
|
+
|
|
22
|
+
Knip is intentionally strict to maximize its potential. It may initially report
|
|
23
|
+
many unused files. However, getting this right will result in great reports and
|
|
24
|
+
tidy codebases.
|
|
25
|
+
|
|
26
|
+
If it doesn't come your way at the first try, remember that often only a small
|
|
27
|
+
change go a long way towards success.
|
|
28
|
+
|
|
29
|
+
:::
|
|
30
|
+
|
|
31
|
+
## Lint issues reported by Knip
|
|
32
|
+
|
|
33
|
+
Knip reports lint issues in your codebase. See [handling issues][5] to deal with
|
|
34
|
+
the reported issues.
|
|
35
|
+
|
|
36
|
+
If Knip reports false positives and you're considering filing a GitHub issue,
|
|
37
|
+
please do! It'll make Knip better for everyone. Please read [issue
|
|
38
|
+
reproduction][6] first.
|
|
39
|
+
|
|
40
|
+
Exit code 1 indicates a successful run, but lint issues were found.
|
|
41
|
+
|
|
42
|
+
## Exceptions thrown by Knip
|
|
43
|
+
|
|
44
|
+
Knip may throw an exception, resulting in an unsuccessful run.
|
|
45
|
+
|
|
46
|
+
See [known issues][7] as it may be listed there and a workaround may be
|
|
47
|
+
available. If it isn't clear what's throwing the exception, try another run with
|
|
48
|
+
`--debug` to locate the cause of the issue with more details.
|
|
49
|
+
|
|
50
|
+
If Knip throws an exception and you're considering filing a GitHub issue, please
|
|
51
|
+
do! It'll make Knip better for everyone. Please read [issue reproduction][6]
|
|
52
|
+
first.
|
|
53
|
+
|
|
54
|
+
Exit code 2 indicates an exception was thrown by Knip.
|
|
55
|
+
|
|
56
|
+
## Debug
|
|
57
|
+
|
|
58
|
+
To better understand why Knip reports what it does, run it in debug mode by
|
|
59
|
+
adding `--debug` to the command:
|
|
60
|
+
|
|
61
|
+
```sh
|
|
62
|
+
knip --debug
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
This will give a lengthy output, including:
|
|
66
|
+
|
|
67
|
+
- Included workspaces
|
|
68
|
+
- Used configuration per workspace
|
|
69
|
+
- Enabled plugins per workspace
|
|
70
|
+
- Glob patterns and options followed by matching file paths
|
|
71
|
+
- Plugin config file paths and found dependencies per plugin
|
|
72
|
+
- Compiled non-standard source files
|
|
73
|
+
|
|
74
|
+
## Trace
|
|
75
|
+
|
|
76
|
+
Use `--trace` to see where exports or dependencies are used:
|
|
77
|
+
|
|
78
|
+
- Use `--trace-file [path]` to output this only for the given file.
|
|
79
|
+
- Use `--trace-export [name]` to output this only for the given export name.
|
|
80
|
+
- Use `--trace-dependency [name]` to find where a dependency is imported
|
|
81
|
+
- Use both to trace a specific named or default export of a certain file.
|
|
82
|
+
|
|
83
|
+
This works across re-exports, barrel files and workspaces. Here's an example
|
|
84
|
+
screenshot:
|
|
85
|
+
|
|
86
|
+
<img src="/screenshots/trace-export.png" alt="trace export" class="mw500" />
|
|
87
|
+
|
|
88
|
+
It's like a reversed module graph. Instead of traversing imports it goes in the
|
|
89
|
+
opposite direction and shows where exports are imported.
|
|
90
|
+
|
|
91
|
+
The `--trace-dependency` accepts strings for exact matches, but if it looks like
|
|
92
|
+
a regex that works too:
|
|
93
|
+
|
|
94
|
+
<img src="/screenshots/trace-dependency.png" alt="trace dependency" class="mw500" />
|
|
95
|
+
|
|
96
|
+
Use [--workspace \[dir\]][8] to filter accordingly.
|
|
97
|
+
|
|
98
|
+
#### Legend
|
|
99
|
+
|
|
100
|
+
| | Description |
|
|
101
|
+
| --- | :------------------------------------------ |
|
|
102
|
+
| `✓` | Contains import and reference to the export |
|
|
103
|
+
| `x` | Is not imported |
|
|
104
|
+
| `◯` | Entry file |
|
|
105
|
+
|
|
106
|
+
## Opening an issue
|
|
107
|
+
|
|
108
|
+
If you want to open an issue, please see [issue reproduction][6].
|
|
109
|
+
|
|
110
|
+
## Understanding Knip
|
|
111
|
+
|
|
112
|
+
Looking to better understand how Knip works? The [entry files][9] and
|
|
113
|
+
[plugins][10] explanations cover two core concepts. After this you might want to
|
|
114
|
+
check out features like [production mode][11] and [monorepos & workspaces][12].
|
|
115
|
+
|
|
116
|
+
In a more general sense, [Why use Knip?][13] explains what Knip can do for you.
|
|
117
|
+
|
|
118
|
+
## Asking for help
|
|
119
|
+
|
|
120
|
+
If you can't find your answer in any of the aforementioned resources, feel free
|
|
121
|
+
to [open an issue on GitHub][14].
|
|
122
|
+
|
|
123
|
+
[1]: #lint-issues-reported-by-knip
|
|
124
|
+
[2]: #exceptions-thrown-by-knip
|
|
125
|
+
[3]: #debug
|
|
126
|
+
[4]: #trace
|
|
127
|
+
[5]: ../guides/handling-issues.md
|
|
128
|
+
[6]: ./issue-reproduction.md
|
|
129
|
+
[7]: ../reference/known-issues.md
|
|
130
|
+
[8]: ../reference/cli.md#--workspace-dir
|
|
131
|
+
[9]: ../explanations/entry-files.md
|
|
132
|
+
[10]: ../explanations/plugins.md
|
|
133
|
+
[11]: ../features/production-mode.md
|
|
134
|
+
[12]: ../features/monorepos-and-workspaces.md
|
|
135
|
+
[13]: ../explanations/why-use-knip.md
|
|
136
|
+
[14]: https://github.com/webpro-nl/knip/issues
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Using Knip in CI
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Knip is your companion during local development. But it is even more valuable in
|
|
6
|
+
a continuous integration (CI) environment to prevent regressions over time. Knip
|
|
7
|
+
will notify you of unused dependencies, exports and files if you forgot to
|
|
8
|
+
remove them.
|
|
9
|
+
|
|
10
|
+
Knip will exit the process with code `1` if there are one or more issues.
|
|
11
|
+
|
|
12
|
+
## GitHub Actions
|
|
13
|
+
|
|
14
|
+
Here's an example workflow configuration for GitHub Actions:
|
|
15
|
+
|
|
16
|
+
```yaml
|
|
17
|
+
name: Lint project
|
|
18
|
+
|
|
19
|
+
on: push
|
|
20
|
+
|
|
21
|
+
jobs:
|
|
22
|
+
lint:
|
|
23
|
+
runs-on: ubuntu-latest
|
|
24
|
+
name: Ubuntu/Node v20
|
|
25
|
+
steps:
|
|
26
|
+
- uses: actions/checkout@v4
|
|
27
|
+
- uses: actions/setup-node@v4
|
|
28
|
+
- name: Install dependencies
|
|
29
|
+
run: npm install --ignore-scripts
|
|
30
|
+
- name: Run knip
|
|
31
|
+
run: npm run knip
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Notes
|
|
35
|
+
|
|
36
|
+
In CI environments, the [--no-progress][1] flag is set automatically.
|
|
37
|
+
|
|
38
|
+
## Related features
|
|
39
|
+
|
|
40
|
+
- [--cache][2]
|
|
41
|
+
- [--max-issues][3]
|
|
42
|
+
- [--no-exit-code][4]
|
|
43
|
+
- [--reporter][5]
|
|
44
|
+
|
|
45
|
+
## Related reading
|
|
46
|
+
|
|
47
|
+
- [Why use Knip?][6]
|
|
48
|
+
|
|
49
|
+
[1]: ../reference/cli.md#--no-progress
|
|
50
|
+
[2]: ../reference/cli.md#--cache
|
|
51
|
+
[3]: ../reference/cli.md#--max-issues
|
|
52
|
+
[4]: ../reference/cli.md#--no-exit-code
|
|
53
|
+
[5]: ../reference/cli.md#--reporter-reporter
|
|
54
|
+
[6]: ../explanations/why-use-knip.md
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Working with CommonJS
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
CommonJS is the JavaScript module system using `require()` and `module.exports`
|
|
6
|
+
statements.
|
|
7
|
+
|
|
8
|
+
Knip works well with CommonJS. You don't need to use ES Modules or a
|
|
9
|
+
`tsconfig.json` to use Knip.
|
|
10
|
+
|
|
11
|
+
The dynamic nature of CommonJS leaves room for ambiguity: it's sometimes unclear
|
|
12
|
+
whether an export is a default or a named export (and thus how it should be
|
|
13
|
+
imported). So we'll have to agree on a few conventions to prevent false
|
|
14
|
+
positives. Those conventions are designed to minimize impact on existing
|
|
15
|
+
codebases, improve consistency, and ease migration to ES Modules or TypeScript.
|
|
16
|
+
|
|
17
|
+
For **named exports**, the recommendation is to assign keys to `module.exports`:
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
const B = function () {};
|
|
21
|
+
|
|
22
|
+
module.exports.A = { option: true };
|
|
23
|
+
module.exports.B = B;
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Alternatively, assign an object with ONLY shorthand property assignments to
|
|
27
|
+
`module.exports`:
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
const A = function () {};
|
|
31
|
+
const B = { option: true };
|
|
32
|
+
|
|
33
|
+
module.exports = { A, B };
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Anything else assigned to `module.exports` is considered a default export, and
|
|
37
|
+
should be imported as such.
|
|
38
|
+
|
|
39
|
+
The following **default import** of the **named exports** above will result in
|
|
40
|
+
all those exports reported as unused, even when referenced like below:
|
|
41
|
+
|
|
42
|
+
```js
|
|
43
|
+
const DefaultImport = require('./common.js');
|
|
44
|
+
const runtime = [DefaultImport.A, DefaultImport.B];
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Instead, do this:
|
|
48
|
+
|
|
49
|
+
```js
|
|
50
|
+
const { A, B } = require('./common.js');
|
|
51
|
+
const runtime = [A, B];
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Not recommended per se, but the following import syntax also results in the
|
|
55
|
+
named export `A` being used:
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
const runtime = [require('./common.js').A];
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Add a non-shorthand property to turn the named object notation into a single
|
|
62
|
+
**default export**:
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
const A = function () {};
|
|
66
|
+
const B = { option: true };
|
|
67
|
+
|
|
68
|
+
module.exports = { __esModule: true, A, B };
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
The `__esModule` key could be named differently (but makes sense given it's an
|
|
72
|
+
informal "CJS/ESM interop" standard amongst compilers and bundlers).
|