knip 0.0.0-graph.3 → 0.0.0-node-types.0
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/README.md +7 -915
- package/bin/knip.js +2 -0
- package/dist/ConfigurationChief.d.ts +14 -6
- package/dist/ConfigurationChief.js +104 -70
- package/dist/ConfigurationValidator.d.ts +553 -321
- package/dist/ConfigurationValidator.js +7 -1
- package/dist/DependencyDeputy.d.ts +4 -1
- package/dist/DependencyDeputy.js +13 -11
- package/dist/PrincipalFactory.d.ts +5 -2
- package/dist/PrincipalFactory.js +11 -7
- package/dist/ProjectPrincipal.d.ts +18 -15
- package/dist/ProjectPrincipal.js +43 -25
- package/dist/WorkspaceWorker.d.ts +9 -7
- package/dist/WorkspaceWorker.js +45 -63
- package/dist/binaries/bash-parser.js +1 -1
- package/dist/binaries/index.js +12 -3
- package/dist/binaries/resolvers/c8.js +2 -1
- package/dist/binaries/resolvers/dotenv.js +2 -1
- package/dist/binaries/resolvers/fallback.js +4 -2
- package/dist/binaries/resolvers/index.d.ts +1 -0
- package/dist/binaries/resolvers/index.js +1 -0
- package/dist/binaries/resolvers/node.js +1 -1
- package/dist/binaries/resolvers/nodemon.js +2 -1
- package/dist/binaries/resolvers/npx.js +2 -1
- package/dist/binaries/resolvers/nx.js +1 -1
- package/dist/binaries/resolvers/pnpm.js +1 -1
- package/dist/binaries/resolvers/rollup.js +2 -1
- package/dist/binaries/resolvers/tsx.d.ts +2 -0
- package/dist/binaries/resolvers/tsx.js +6 -0
- package/dist/binaries/resolvers/yarn.js +1 -1
- package/dist/binaries/util.d.ts +0 -3
- package/dist/binaries/util.js +1 -3
- package/dist/cli.d.ts +0 -1
- package/dist/cli.js +10 -9
- package/dist/constants.d.ts +0 -1
- package/dist/constants.js +5 -4
- package/dist/index.js +101 -65
- package/dist/manifest/index.js +8 -4
- package/dist/plugins/_template/index.js +15 -3
- package/dist/plugins/_template/types.d.ts +1 -0
- package/dist/plugins/angular/index.js +11 -11
- package/dist/plugins/astro/index.d.ts +7 -0
- package/dist/plugins/astro/index.js +19 -0
- package/dist/plugins/ava/index.d.ts +1 -1
- package/dist/plugins/ava/index.js +28 -7
- package/dist/plugins/ava/types.d.ts +5 -1
- package/dist/plugins/babel/helpers.d.ts +1 -2
- package/dist/plugins/babel/helpers.js +28 -20
- package/dist/plugins/babel/index.js +17 -14
- package/dist/plugins/capacitor/index.js +7 -3
- package/dist/plugins/changesets/index.js +10 -6
- package/dist/plugins/commitizen/index.d.ts +1 -0
- package/dist/plugins/commitizen/index.js +8 -4
- package/dist/plugins/commitizen/types.d.ts +1 -1
- package/dist/plugins/commitlint/index.js +7 -3
- package/dist/plugins/cspell/index.js +7 -3
- package/dist/plugins/cspell/types.d.ts +1 -1
- package/dist/plugins/cypress/index.d.ts +3 -1
- package/dist/plugins/cypress/index.js +17 -4
- package/dist/plugins/drizzle/index.js +5 -7
- package/dist/plugins/eslint/fallback.js +6 -1
- package/dist/plugins/eslint/helpers.d.ts +4 -11
- package/dist/plugins/eslint/helpers.js +24 -26
- package/dist/plugins/eslint/index.d.ts +1 -1
- package/dist/plugins/eslint/index.js +14 -4
- package/dist/plugins/gatsby/index.js +11 -5
- package/dist/plugins/github-actions/index.d.ts +1 -1
- package/dist/plugins/github-actions/index.js +7 -4
- package/dist/plugins/graphql-codegen/index.d.ts +7 -0
- package/dist/plugins/graphql-codegen/index.js +46 -0
- package/dist/plugins/graphql-codegen/types.d.ts +25 -0
- package/dist/plugins/graphql-codegen/types.js +3 -0
- package/dist/plugins/husky/index.js +9 -4
- package/dist/plugins/index.d.ts +4 -0
- package/dist/plugins/index.js +4 -0
- package/dist/plugins/jest/index.d.ts +1 -1
- package/dist/plugins/jest/index.js +21 -12
- package/dist/plugins/jest/types.d.ts +3 -0
- package/dist/plugins/lefthook/index.js +12 -8
- package/dist/plugins/lint-staged/index.d.ts +1 -0
- package/dist/plugins/lint-staged/index.js +10 -7
- package/dist/plugins/markdownlint/index.js +7 -4
- package/dist/plugins/mocha/index.d.ts +1 -0
- package/dist/plugins/mocha/index.js +12 -7
- package/dist/plugins/mocha/types.d.ts +4 -0
- package/dist/plugins/mocha/types.js +1 -0
- package/dist/plugins/next/index.d.ts +2 -1
- package/dist/plugins/next/index.js +7 -0
- package/dist/plugins/node-test-runner/index.d.ts +6 -0
- package/dist/plugins/node-test-runner/index.js +19 -0
- package/dist/plugins/npm-package-json-lint/index.d.ts +1 -0
- package/dist/plugins/npm-package-json-lint/index.js +8 -4
- package/dist/plugins/nx/index.js +13 -10
- package/dist/plugins/nyc/index.js +6 -3
- package/dist/plugins/nyc/types.d.ts +3 -0
- package/dist/plugins/nyc/types.js +1 -0
- package/dist/plugins/playwright/index.d.ts +5 -1
- package/dist/plugins/playwright/index.js +25 -2
- package/dist/plugins/playwright-ct/index.d.ts +3 -1
- package/dist/plugins/playwright-ct/index.js +18 -2
- package/dist/plugins/postcss/index.js +12 -9
- package/dist/plugins/prettier/index.js +11 -6
- package/dist/plugins/prettier/types.d.ts +8 -0
- package/dist/plugins/prettier/types.js +1 -0
- package/dist/plugins/release-it/index.d.ts +1 -0
- package/dist/plugins/release-it/index.js +14 -10
- package/dist/plugins/remark/index.d.ts +1 -0
- package/dist/plugins/remark/index.js +20 -4
- package/dist/plugins/remark/types.d.ts +3 -0
- package/dist/plugins/remark/types.js +1 -0
- package/dist/plugins/remix/index.d.ts +2 -1
- package/dist/plugins/remix/index.js +9 -0
- package/dist/plugins/rollup/index.d.ts +2 -1
- package/dist/plugins/rollup/index.js +8 -1
- package/dist/plugins/semantic-release/index.d.ts +1 -0
- package/dist/plugins/semantic-release/index.js +10 -4
- package/dist/plugins/semantic-release/types.d.ts +1 -1
- package/dist/plugins/sentry/index.d.ts +2 -1
- package/dist/plugins/sentry/index.js +7 -0
- package/dist/plugins/storybook/index.js +28 -10
- package/dist/plugins/storybook/types.d.ts +7 -0
- package/dist/plugins/stryker/index.js +13 -9
- package/dist/plugins/stylelint/index.js +8 -5
- package/dist/plugins/svelte/index.d.ts +2 -1
- package/dist/plugins/svelte/index.js +6 -0
- package/dist/plugins/tsup/index.d.ts +6 -0
- package/dist/plugins/tsup/index.js +23 -0
- package/dist/plugins/tsup/types.d.ts +7 -0
- package/dist/plugins/tsup/types.js +1 -0
- package/dist/plugins/typedoc/index.d.ts +1 -0
- package/dist/plugins/typedoc/index.js +8 -4
- package/dist/plugins/typedoc/types.d.ts +1 -1
- package/dist/plugins/typescript/index.js +16 -11
- package/dist/plugins/vite/index.js +7 -5
- package/dist/plugins/vitest/helpers.d.ts +2 -2
- package/dist/plugins/vitest/index.d.ts +4 -4
- package/dist/plugins/vitest/index.js +55 -26
- package/dist/plugins/vitest/types.d.ts +23 -2
- package/dist/plugins/webpack/index.js +31 -19
- package/dist/plugins/webpack/types.d.ts +2 -1
- package/dist/reporters/codeowners.js +2 -2
- package/dist/reporters/json.js +14 -9
- package/dist/reporters/symbols.js +3 -2
- package/dist/reporters/util.d.ts +8 -2
- package/dist/reporters/util.js +7 -1
- package/dist/types/cli.d.ts +1 -1
- package/dist/types/config.d.ts +9 -4
- package/dist/types/exports.d.ts +1 -0
- package/dist/types/imports.d.ts +6 -0
- package/dist/types/issues.d.ts +11 -1
- package/dist/types/package-json.d.ts +41 -0
- package/dist/types/package-json.js +1 -0
- package/dist/types/plugins.d.ts +5 -7
- package/dist/types/util.d.ts +16 -0
- package/dist/types/util.js +1 -0
- package/dist/types/workspace.d.ts +4 -1
- package/dist/typescript/SourceFile.d.ts +5 -0
- package/dist/typescript/SourceFileManager.js +3 -3
- package/dist/typescript/ast-helpers.d.ts +7 -14
- package/dist/typescript/ast-helpers.js +11 -9
- package/dist/typescript/createHosts.d.ts +1 -0
- package/dist/typescript/createHosts.js +1 -1
- package/dist/typescript/getImportsAndExports.d.ts +7 -5
- package/dist/typescript/getImportsAndExports.js +35 -18
- package/dist/typescript/resolveModuleNames.d.ts +1 -1
- package/dist/typescript/resolveModuleNames.js +38 -12
- package/dist/typescript/visitors/exports/exportDeclaration.js +14 -1
- package/dist/typescript/visitors/exports/exportKeyword.js +5 -2
- package/dist/typescript/visitors/exports/moduleExportsAccessExpression.js +29 -21
- package/dist/typescript/visitors/imports/importCall.js +1 -1
- package/dist/typescript/visitors/imports/importDeclaration.js +3 -3
- package/dist/typescript/visitors/imports/importEqualsDeclaration.js +1 -1
- package/dist/typescript/visitors/imports/jsDocType.js +37 -5
- package/dist/typescript/visitors/imports/propertyAccessCall.js +1 -1
- package/dist/typescript/visitors/imports/reExportDeclaration.js +8 -3
- package/dist/typescript/visitors/imports/requireCall.js +5 -5
- package/dist/util/array.d.ts +3 -1
- package/dist/util/cli-arguments.d.ts +3 -2
- package/dist/util/cli-arguments.js +6 -6
- package/dist/util/compilers.d.ts +122 -80
- package/dist/util/debug.d.ts +3 -3
- package/dist/util/debug.js +12 -18
- package/dist/util/fs.d.ts +2 -0
- package/dist/util/fs.js +22 -3
- package/dist/util/get-included-issue-types.js +14 -18
- package/dist/util/git.js +1 -1
- package/dist/util/glob.js +1 -1
- package/dist/util/loader.d.ts +2 -0
- package/dist/util/loader.js +23 -3
- package/dist/util/modules.d.ts +7 -1
- package/dist/util/modules.js +20 -10
- package/dist/util/object.d.ts +1 -0
- package/dist/util/object.js +3 -0
- package/dist/util/parse-args.d.ts +3 -0
- package/dist/util/parse-args.js +8 -0
- package/dist/util/path.d.ts +1 -0
- package/dist/util/path.js +5 -3
- package/dist/util/plugin.d.ts +4 -3
- package/dist/util/plugin.js +11 -4
- package/dist/util/protocols.d.ts +9 -0
- package/dist/util/protocols.js +9 -0
- package/dist/util/register.d.ts +2 -1
- package/dist/util/register.js +9 -13
- package/dist/util/require.js +7 -5
- package/dist/util/tsconfig-loader.js +2 -1
- package/dist/util/unwrap-function.d.ts +1 -0
- package/dist/util/unwrap-function.js +13 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +86 -85
- package/schema-jsonc.json +11 -0
- package/schema.json +27 -3
- package/dist/plugins/vite/types.d.ts +0 -4
- /package/dist/plugins/{vite → jest}/types.js +0 -0
package/README.md
CHANGED
|
@@ -3,925 +3,17 @@
|
|
|
3
3
|
Knip finds **unused files, dependencies and exports** in your JavaScript and TypeScript projects. Less code and
|
|
4
4
|
dependencies lead to improved performance, less maintenance and easier refactorings.
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
export const myVar = true;
|
|
8
|
-
```
|
|
6
|
+
The documentation is available at [knip.dev][1].
|
|
9
7
|
|
|
10
|
-
This is
|
|
11
|
-
This is where Knip starts: it lints the project as a whole and finds unused exports, files and dependencies.
|
|
12
|
-
|
|
13
|
-
It's only human to forget to remove things that you no longer use. But how do you find out? Where do you start finding
|
|
14
|
-
things that can be removed?
|
|
15
|
-
|
|
16
|
-
The dots don't connect themselves. This is where Knip comes in:
|
|
17
|
-
|
|
18
|
-
- [x] Finds **unused files, dependencies and exports**
|
|
19
|
-
- [x] Finds used dependencies not listed in `package.json`
|
|
20
|
-
- [x] Built-in support for [workspaces (monorepos)][1]
|
|
21
|
-
- [x] Growing list of [built-in plugins][2]
|
|
22
|
-
- [x] Use [compilers][3] to include other file types (e.g. `.mdx`, `.vue`, `.svelte`)
|
|
23
|
-
- [x] Finds binaries and dependencies in npm scripts, and a lot more locations
|
|
24
|
-
- [x] Finds unused members of classes and enums
|
|
25
|
-
- [x] Finds duplicate exports
|
|
26
|
-
- [x] Supports any combination of JavaScript and TypeScript
|
|
27
|
-
- [x] Multiple built-in [reporters][4] (or use [custom reporters][5] and [preprocessors][6])
|
|
28
|
-
- [x] Understands [JSDoc/TSDoc tags][7] (e.g. `@public` and `@internal`)
|
|
29
|
-
- [x] Run Knip as part of your CI environment to detect issues and prevent regressions
|
|
30
|
-
|
|
31
|
-
Knip shines in both small and large projects. It's a fresh take on keeping your projects clean & tidy!
|
|
32
|
-
|
|
33
|
-
[![An orange cow with scissors, Van Gogh style][9]][8] <sup>_“An orange cow with scissors, Van Gogh style” - generated
|
|
34
|
-
with OpenAI_</sup>
|
|
35
|
-
|
|
36
|
-
For updates or questions, come hang out in [The Knip Barn (Discord)][10], or follow [@webprolific (Twitter)][11] or
|
|
37
|
-
[@webpro (fosstodon.org)][12]. Please use GitHub to [report issues][13].
|
|
38
|
-
|
|
39
|
-
## Contents
|
|
40
|
-
|
|
41
|
-
- [Getting Started][14]
|
|
42
|
-
- [Installation][15]
|
|
43
|
-
- [Default Configuration][16]
|
|
44
|
-
- [Let's Go!][17]
|
|
45
|
-
- [Configuration][18]
|
|
46
|
-
- [Entry Files][19]
|
|
47
|
-
- [Workspaces][1]
|
|
48
|
-
- [Plugins][2]
|
|
49
|
-
- [Compilers][3]
|
|
50
|
-
- [Ignore files, binaries, dependencies and workspaces][20]
|
|
51
|
-
- [Public exports][21]
|
|
52
|
-
- [Ignore exports used in file][22]
|
|
53
|
-
- [Include exports in entry files][23]
|
|
54
|
-
- [Paths][24]
|
|
55
|
-
- [Production Mode][25]
|
|
56
|
-
- [Strict][26]
|
|
57
|
-
- [Ignore `@internal` exports][27]
|
|
58
|
-
- [Plugins][28]
|
|
59
|
-
- [Output][29]
|
|
60
|
-
- [Screenshots][30]
|
|
61
|
-
- [Reading the report][31]
|
|
62
|
-
- [Rules & Filters][32]
|
|
63
|
-
- [Reporters][4]
|
|
64
|
-
- [Fixing Issues][33]
|
|
65
|
-
- [JSDoc tags][7]
|
|
66
|
-
- [Command Line Options][34]
|
|
67
|
-
- [Potential boost with `--no-gitignore`][35]
|
|
68
|
-
- [Comparison & Migration][36]
|
|
69
|
-
- [depcheck][37]
|
|
70
|
-
- [unimported][38]
|
|
71
|
-
- [ts-unused-exports][39]
|
|
72
|
-
- [ts-prune][40]
|
|
73
|
-
- [Projects using Knip][41]
|
|
74
|
-
- [Articles, etc.][42]
|
|
75
|
-
- [Why "Knip"?][43]
|
|
76
|
-
- [Really, another unused file/dependency/export finder?][44]
|
|
77
|
-
- [Contributors][45]
|
|
78
|
-
|
|
79
|
-
## Getting Started
|
|
80
|
-
|
|
81
|
-
### Installation
|
|
82
|
-
|
|
83
|
-
npm install -D knip
|
|
84
|
-
|
|
85
|
-
Knip supports LTS versions of Node.js, and currently requires at least Node.js v16.17 or v18.6.
|
|
86
|
-
|
|
87
|
-
### Default Configuration
|
|
88
|
-
|
|
89
|
-
Knip has good defaults and you can run it without any configuration. The (simplified) default config:
|
|
90
|
-
|
|
91
|
-
```json
|
|
92
|
-
{
|
|
93
|
-
"entry": ["index.js", "src/index.js"],
|
|
94
|
-
"project": ["**/*.js"]
|
|
95
|
-
}
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
There's more, jump to [Entry Files][19] for details.
|
|
99
|
-
|
|
100
|
-
Places where Knip looks for configuration (ordered by priority):
|
|
101
|
-
|
|
102
|
-
- `knip.json`
|
|
103
|
-
- `knip.jsonc`
|
|
104
|
-
- `.knip.json`
|
|
105
|
-
- `.knip.jsonc`
|
|
106
|
-
- `knip.ts`
|
|
107
|
-
- `knip.js`
|
|
108
|
-
- `package.json#knip`
|
|
109
|
-
|
|
110
|
-
You can use a dynamic `knip.ts` with TypeScript if you prefer:
|
|
111
|
-
|
|
112
|
-
```ts
|
|
113
|
-
import type { KnipConfig } from 'knip';
|
|
114
|
-
|
|
115
|
-
const config: KnipConfig = {
|
|
116
|
-
entry: ['src/index.ts'],
|
|
117
|
-
project: ['src/**/*.ts'],
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
export default config;
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
Use `--config path/to/knip.json` to use a different location.
|
|
124
|
-
|
|
125
|
-
### Let's Go!
|
|
126
|
-
|
|
127
|
-
Run the checks with `npx knip`. Or first add this script to `package.json`:
|
|
128
|
-
|
|
129
|
-
```json
|
|
130
|
-
{
|
|
131
|
-
"scripts": {
|
|
132
|
-
"knip": "knip"
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
Then use `npm run knip` to analyze the project and output unused files, dependencies and exports. Knip works just fine
|
|
138
|
-
with `yarn` or `pnpm` as well.
|
|
139
|
-
|
|
140
|
-
See [Command Line Options][34] for an overview of available CLI options.
|
|
141
|
-
|
|
142
|
-
## Configuration
|
|
143
|
-
|
|
144
|
-
### Entry Files
|
|
145
|
-
|
|
146
|
-
In addition to `index.js`, the following file names and extensions are also considered entry files:
|
|
147
|
-
|
|
148
|
-
- `index`, `main` and `cli`
|
|
149
|
-
- `js`, `mjs`, `cjs`, `jsx`, `ts`, `mts`, `cts` and `tsx`
|
|
150
|
-
|
|
151
|
-
This means files like `main.cjs` and `src/cli.ts` are automatically added as entry files. Knip looks for entry files at
|
|
152
|
-
those default locations, but also in other places:
|
|
153
|
-
|
|
154
|
-
- The `main`, `bin` and `exports` fields of `package.json`.
|
|
155
|
-
- [Plugins][2] such as for Next.js, Remix, Gatsby or Svelte add entry files.
|
|
156
|
-
- The `scripts` in package.json or other scripts may provide entry files.
|
|
157
|
-
|
|
158
|
-
Knip does this for each [workspace][1] it finds, trying to minimize the configuration to suit your project. In a
|
|
159
|
-
perfectly boring world where everything is according to defaults you wouldn't even need a `knip.json` file at all.
|
|
160
|
-
|
|
161
|
-
Larger projects tend to have more things customized, and therefore probably get more out of Knip with a configuration
|
|
162
|
-
file. Let's say you are using `.ts` files exclusively and have all source files only in the `src` directory:
|
|
163
|
-
|
|
164
|
-
```json
|
|
165
|
-
{
|
|
166
|
-
"$schema": "https://unpkg.com/knip@2/schema.json",
|
|
167
|
-
"entry": ["src/index.ts"],
|
|
168
|
-
"project": ["src/**/*.ts"]
|
|
169
|
-
}
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
The `entry` files target the starting point(s) to resolve the rest of the imported code. The `project` files should
|
|
173
|
-
contain all files to match against the files resolved from the entry files, including potentially unused files.
|
|
174
|
-
|
|
175
|
-
### Workspaces
|
|
176
|
-
|
|
177
|
-
Workspaces are handled out-of-the-box by Knip. Every workspace is part of the analysis.
|
|
178
|
-
|
|
179
|
-
Workspaces are sometimes also referred to as package-based monorepos, or as packages in a monorepo. Knip uses the term
|
|
180
|
-
workspace exclusively to indicate a directory that has a `package.json`.
|
|
181
|
-
|
|
182
|
-
Here's an example `knip.json` configuration with some custom `entry` and `project` patterns:
|
|
183
|
-
|
|
184
|
-
```json
|
|
185
|
-
{
|
|
186
|
-
"workspaces": {
|
|
187
|
-
".": {
|
|
188
|
-
"entry": "scripts/*.js",
|
|
189
|
-
"project": "scripts/**/*.js"
|
|
190
|
-
},
|
|
191
|
-
"packages/*": {
|
|
192
|
-
"entry": "{index,cli}.ts",
|
|
193
|
-
"project": "**/*.ts"
|
|
194
|
-
},
|
|
195
|
-
"packages/my-lib": {
|
|
196
|
-
"entry": "main.js"
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
It might be useful to run Knip first with no or little configuration to see where it needs custom `entry` and/or
|
|
203
|
-
`project` files. Each workspace has the same [default configuration][18].
|
|
204
|
-
|
|
205
|
-
The root workspace is named `"."` under `workspaces` (like in the example).
|
|
206
|
-
|
|
207
|
-
Knip supports workspaces as defined in three possible locations:
|
|
208
|
-
|
|
209
|
-
- In the `workspaces` array in `package.json` (npm, Yarn, Lerna).
|
|
210
|
-
- In the `packages` array in `pnpm-workspace.yaml` (pnpm).
|
|
211
|
-
- In the `workspaces.packages` array in `package.json` (legacy).
|
|
212
|
-
- In the `workspaces` object in Knip configuration.
|
|
213
|
-
|
|
214
|
-
The `workspaces` in Knip configuration not already defined in the root `package.json` or `pnpm-workspace.yaml` are
|
|
215
|
-
added. Knip requires a `package.json` file in each workspace directory.
|
|
216
|
-
|
|
217
|
-
Here's some example output when running Knip in a workspace:
|
|
218
|
-
|
|
219
|
-
<img src="./assets/screenshot-workspaces.png" alt="example output in workspaces" width="578">
|
|
220
|
-
|
|
221
|
-
Use `--debug` to get more verbose output.
|
|
222
|
-
|
|
223
|
-
Use `--workspace [dir]` to analyze a single workspace (including its ancestors).
|
|
224
|
-
|
|
225
|
-
The `ignore`, `ignoreBinaries` and `ignoreDependencies` options are available inside workspace configurations.
|
|
226
|
-
|
|
227
|
-
### Plugins
|
|
228
|
-
|
|
229
|
-
Plugins tell Knip where to look for configuration and entry files, and if necessary have a custom dependency finder.
|
|
230
|
-
Knip plugins are automatically activated, you don't need to install or configure anything.
|
|
231
|
-
|
|
232
|
-
To explain what they do, here's a quick example `.eslintrc.json` configuration file (for ESLint):
|
|
233
|
-
|
|
234
|
-
```json
|
|
235
|
-
{
|
|
236
|
-
"extends": ["airbnb"],
|
|
237
|
-
"plugins": ["prettier"]
|
|
238
|
-
}
|
|
239
|
-
```
|
|
240
|
-
|
|
241
|
-
Knip's ESLint plugin reads `.eslintrc.json` and will return `eslint-config-airbnb` and `eslint-plugin-prettier` from
|
|
242
|
-
this example to Knip, so it can tell you whether `package.json` is out of sync. In a nutshell, this is how plugins work.
|
|
243
|
-
This is especially useful over time when such configuration files change (and they will)!
|
|
244
|
-
|
|
245
|
-
Knip contains a growing list of plugins:
|
|
246
|
-
|
|
247
|
-
- [Angular][plugin-angular]
|
|
248
|
-
- [Ava][plugin-ava]
|
|
249
|
-
- [Babel][plugin-babel]
|
|
250
|
-
- [Capacitor][plugin-capacitor]
|
|
251
|
-
- [Changesets][plugin-changesets]
|
|
252
|
-
- [Commitizen][plugin-commitizen]
|
|
253
|
-
- [commitlint][plugin-commitlint]
|
|
254
|
-
- [cspell][plugin-cspell]
|
|
255
|
-
- [Cypress][plugin-cypress]
|
|
256
|
-
- [Drizzle][plugin-drizzle]
|
|
257
|
-
- [ESLint][plugin-eslint]
|
|
258
|
-
- [Gatsby][plugin-gatsby]
|
|
259
|
-
- [GitHub Actions][plugin-github-actions]
|
|
260
|
-
- [husky][plugin-husky]
|
|
261
|
-
- [Jest][plugin-jest]
|
|
262
|
-
- [Lefthook][plugin-lefthook]
|
|
263
|
-
- [lint-staged][plugin-lint-staged]
|
|
264
|
-
- [markdownlint][plugin-markdownlint]
|
|
265
|
-
- [Mocha][plugin-mocha]
|
|
266
|
-
- [Next.js][plugin-next]
|
|
267
|
-
- [npm-package-json-lint][plugin-npm-package-json-lint]
|
|
268
|
-
- [Nx][plugin-nx]
|
|
269
|
-
- [nyc][plugin-nyc]
|
|
270
|
-
- [Playwright][plugin-playwright]
|
|
271
|
-
- [Playwright for components][plugin-playwright-ct]
|
|
272
|
-
- [PostCSS][plugin-postcss]
|
|
273
|
-
- [Prettier][plugin-prettier]
|
|
274
|
-
- [Release It][plugin-release-it]
|
|
275
|
-
- [Remark][plugin-remark]
|
|
276
|
-
- [Remix][plugin-remix]
|
|
277
|
-
- [Rollup][plugin-rollup]
|
|
278
|
-
- [Semantic Release][plugin-semantic-release]
|
|
279
|
-
- [Sentry][plugin-sentry]
|
|
280
|
-
- [Storybook][plugin-storybook]
|
|
281
|
-
- [Stryker][plugin-stryker]
|
|
282
|
-
- [Stylelint][plugin-stylelint]
|
|
283
|
-
- [Svelte][plugin-svelte]
|
|
284
|
-
- [Tailwind][plugin-tailwind]
|
|
285
|
-
- [TypeDoc][plugin-typedoc]
|
|
286
|
-
- [TypeScript][plugin-typescript]
|
|
287
|
-
- [Vite][plugin-vite]
|
|
288
|
-
- [Vitest][plugin-vitest]
|
|
289
|
-
- [Webpack][plugin-webpack]
|
|
290
|
-
|
|
291
|
-
Plugins are automatically activated. Each plugin is automatically enabled based on simple heuristics. Most of them check
|
|
292
|
-
whether one of a few dependencies is listed in `package.json`. Once enabled, they add a set of `config` files for
|
|
293
|
-
themselves and/or `entry` files for Knip to analyze.
|
|
294
|
-
|
|
295
|
-
- `config` files are given to the plugin's dependency finder
|
|
296
|
-
- `entry` files are given to Knip to include with the analysis of the rest of the source code
|
|
297
|
-
|
|
298
|
-
See each plugin's documentation for its default values.
|
|
299
|
-
|
|
300
|
-
#### `config`
|
|
301
|
-
|
|
302
|
-
Plugins usually include `config` files. They are handled by the plugin's custom dependency finder, which returns all
|
|
303
|
-
dependencies referenced in the files it is given. Knip handles the rest to determine which of those dependencies are
|
|
304
|
-
unused or missing.
|
|
305
|
-
|
|
306
|
-
#### `entry`
|
|
307
|
-
|
|
308
|
-
Other configuration files use `require` or `import` statements to use dependencies, so they don't need special handing
|
|
309
|
-
and can be analyzed like any other source file. That's why these configuration files are also used as `entry` files.
|
|
310
|
-
|
|
311
|
-
#### Override plugin configuration
|
|
312
|
-
|
|
313
|
-
Usually, no custom configuration is required for plugins, but if your project uses custom file locations then Knip
|
|
314
|
-
allows you to override any defaults. Let's take Cypress for example. By default it uses `cypress.config.js`, but your
|
|
315
|
-
project uses `config/cypress.js`. Also, the default pattern for test files is `cypress/e2e/**/*.cy.js`, but your project
|
|
316
|
-
has them at `e2e-tests/*.spec.ts`. Here's how to configure this:
|
|
317
|
-
|
|
318
|
-
```json
|
|
319
|
-
{
|
|
320
|
-
"cypress": {
|
|
321
|
-
"entry": ["config/cypress.js", "e2e-tests/*.spec.js"]
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
```
|
|
325
|
-
|
|
326
|
-
When overriding any plugin's configuration, the options object is fully replaced. Look at the plugin's page to find the
|
|
327
|
-
default configuration and use that as a basis. Here's another example to override the test file pattern for Vitest:
|
|
328
|
-
|
|
329
|
-
```json
|
|
330
|
-
{
|
|
331
|
-
"vitest": {
|
|
332
|
-
"config": ["vitest.config.ts"],
|
|
333
|
-
"entry": ["**/*.vitest.ts"]
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
```
|
|
337
|
-
|
|
338
|
-
This plugin configuration can be set on root and workspace level. If set on root level, it can be disabled on workspace
|
|
339
|
-
level by setting it to `false` there.
|
|
340
|
-
|
|
341
|
-
#### Multi-project repositories
|
|
342
|
-
|
|
343
|
-
Some repositories have a single `package.json`, but consist of multiple projects with configuration files across the
|
|
344
|
-
repository (such as the [Nx "intregrated repo" style][46]). Let's assume some of these projects are apps and have their
|
|
345
|
-
own Cypress configuration and test files. In that case, we could configure the Cypress plugin like this:
|
|
346
|
-
|
|
347
|
-
```json
|
|
348
|
-
{
|
|
349
|
-
"cypress": {
|
|
350
|
-
"entry": ["apps/**/cypress.config.ts", "apps/**/cypress/e2e/*.spec.ts"]
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
```
|
|
354
|
-
|
|
355
|
-
#### Disable a plugin
|
|
356
|
-
|
|
357
|
-
In case a plugin causes issues, it can be disabled by using `false` as its value (e.g. `"webpack": false`).
|
|
358
|
-
|
|
359
|
-
#### Create a new plugin
|
|
360
|
-
|
|
361
|
-
Getting false positives because a plugin is missing? Want to help out? Please read more at [writing a plugin][47]. This
|
|
362
|
-
guide also contains more details if you want to learn more about plugins and why they are useful.
|
|
363
|
-
|
|
364
|
-
### Compilers
|
|
365
|
-
|
|
366
|
-
Compilers allow to include files that are not JavaScript or TypeScript in the process of finding unused or missing
|
|
367
|
-
dependencies (e.g. `.mdx`, `.vue` and `.svelte` files).
|
|
368
|
-
|
|
369
|
-
This requires using a dynamic `knip.js` or `knip.ts` configuration file. Provide a `compilers` object in the
|
|
370
|
-
configuration where each key represents the extension and the value is a function that takes the contents of these files
|
|
371
|
-
as input and returns JavaScript or TypeScript as output. Here is an example that compiles `.mdx` files to JavaScript so
|
|
372
|
-
these files and their imports and exports become part of the analysis:
|
|
373
|
-
|
|
374
|
-
```js
|
|
375
|
-
import { compileSync } from 'mdx-js/mdx';
|
|
376
|
-
|
|
377
|
-
export default {
|
|
378
|
-
compilers: {
|
|
379
|
-
mdx: compileSync,
|
|
380
|
-
},
|
|
381
|
-
};
|
|
382
|
-
```
|
|
383
|
-
|
|
384
|
-
Read [Compilers][48] for more details and examples.
|
|
385
|
-
|
|
386
|
-
### Ignore files, binaries, dependencies and workspaces
|
|
387
|
-
|
|
388
|
-
There are a few ways to tell Knip to ignore certain files, binaries, dependencies and workspaces. Some examples:
|
|
389
|
-
|
|
390
|
-
```json
|
|
391
|
-
{
|
|
392
|
-
"ignore": ["**/*.d.ts", "**/fixtures"],
|
|
393
|
-
"ignoreBinaries": ["zip", "docker-compose"],
|
|
394
|
-
"ignoreDependencies": ["hidden-package"],
|
|
395
|
-
"ignoreWorkspaces": ["packages/ignore", "packages/examples/**"]
|
|
396
|
-
}
|
|
397
|
-
```
|
|
398
|
-
|
|
399
|
-
These can also be configured per workspace (except for `ignoreWorkspaces`).
|
|
400
|
-
|
|
401
|
-
### Public exports
|
|
402
|
-
|
|
403
|
-
Sometimes a file that's not an entry file has one or more exports that are public and should not be reported as unused.
|
|
404
|
-
Such variables and types can be marked with the JSDoc `@public` tag:
|
|
405
|
-
|
|
406
|
-
```js
|
|
407
|
-
/**
|
|
408
|
-
* Merge two objects.
|
|
409
|
-
*
|
|
410
|
-
* @public
|
|
411
|
-
*/
|
|
412
|
-
|
|
413
|
-
export const merge = function () {};
|
|
414
|
-
|
|
415
|
-
/** @public */
|
|
416
|
-
export const split = function () {};
|
|
417
|
-
```
|
|
418
|
-
|
|
419
|
-
Knip does not report public exports and types as unused.
|
|
420
|
-
|
|
421
|
-
### Ignore exports used in file
|
|
422
|
-
|
|
423
|
-
In files with multiple exports, some of them might be used only internally. If these exports should not be reported,
|
|
424
|
-
there is a `ignoreExportsUsedInFile` option available. With this option enabled, you don't need to mark everything
|
|
425
|
-
`@public` separately and when something is no longer used internally, it will still be reported.
|
|
426
|
-
|
|
427
|
-
```json
|
|
428
|
-
{
|
|
429
|
-
"ignoreExportsUsedInFile": true
|
|
430
|
-
}
|
|
431
|
-
```
|
|
432
|
-
|
|
433
|
-
In a more fine-grained manner, you can also ignore only specific issue types:
|
|
434
|
-
|
|
435
|
-
```json
|
|
436
|
-
{
|
|
437
|
-
"ignoreExportsUsedInFile": {
|
|
438
|
-
"interface": true,
|
|
439
|
-
"type": true
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
```
|
|
443
|
-
|
|
444
|
-
### Include exports in entry files
|
|
445
|
-
|
|
446
|
-
When a repository is self-contained or private, you may want to include entry files when reporting unused exports:
|
|
447
|
-
|
|
448
|
-
knip --include-entry-exports
|
|
449
|
-
|
|
450
|
-
Knip will also report unused exports in entry source files and scripts (such as those referenced in `package.json`). But
|
|
451
|
-
not in entry and configuration files from plugins, such as `next.config.js` or `src/routes/+page.svelte`.
|
|
452
|
-
|
|
453
|
-
### Paths
|
|
454
|
-
|
|
455
|
-
Tools like TypeScript, Webpack and Babel support import aliases in various ways. Knip automatically includes
|
|
456
|
-
`compilerOptions.paths` from the TypeScript configuration, but does not (yet) automatically find other types of import
|
|
457
|
-
aliases. They can be configured manually:
|
|
458
|
-
|
|
459
|
-
```json
|
|
460
|
-
{
|
|
461
|
-
"$schema": "https://unpkg.com/knip@2/schema.json",
|
|
462
|
-
"paths": {
|
|
463
|
-
"@lib": ["./lib/index.ts"],
|
|
464
|
-
"@lib/*": ["./lib/*"]
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
```
|
|
468
|
-
|
|
469
|
-
Each workspace can also have its own `paths` configured. Knip `paths` follow the TypeScript semantics:
|
|
470
|
-
|
|
471
|
-
- Path values are an array of relative paths.
|
|
472
|
-
- Paths without an `*` are exact matches.
|
|
473
|
-
|
|
474
|
-
## Production Mode
|
|
475
|
-
|
|
476
|
-
The default mode for Knip is holistic and targets all project code, including configuration files and tests. Test files
|
|
477
|
-
usually import production files. This prevents the production files or their exports from being reported as unused,
|
|
478
|
-
while sometimes both of them can be removed. This is why Knip has a "production mode".
|
|
479
|
-
|
|
480
|
-
To tell Knip what is production code, add an exclamation mark behind each `pattern!` that is meant for production and
|
|
481
|
-
use the `--production` flag. Here's an example:
|
|
482
|
-
|
|
483
|
-
```json
|
|
484
|
-
{
|
|
485
|
-
"entry": ["src/index.ts!", "build/script.js"],
|
|
486
|
-
"project": ["src/**/*.ts!", "build/*.js"]
|
|
487
|
-
}
|
|
488
|
-
```
|
|
489
|
-
|
|
490
|
-
Here's what's included in production mode analysis:
|
|
491
|
-
|
|
492
|
-
- Only `entry` and `project` patterns suffixed with `!`.
|
|
493
|
-
- Only production `entry` file patterns exported by plugins (such as Next.js and Gatsby).
|
|
494
|
-
- Only the `start` and `postinstall` scripts (e.g. not the `test` or other npm scripts in `package.json`).
|
|
495
|
-
- Only unused `exports`, `nsExports` and `classMembers` are reported (not `types`, `nsTypes`, `enumMembers`).
|
|
496
|
-
|
|
497
|
-
### Strict
|
|
498
|
-
|
|
499
|
-
Additionally, the `--strict` flag can be used to:
|
|
500
|
-
|
|
501
|
-
- Consider `dependencies` (not `devDependencies`) when finding unused or unlisted dependencies.
|
|
502
|
-
- Include `peerDependencies` when finding unused or unlisted dependencies.
|
|
503
|
-
- Ignore type-only imports (`import type {}`).
|
|
504
|
-
- Verify each workspace is self-contained: have their own `dependencies` (and not use packages of ancestor workspaces).
|
|
505
|
-
|
|
506
|
-
### Ignore `@internal` exports
|
|
507
|
-
|
|
508
|
-
In addition to the flags above, the `--ignore-internal` flag can be used to ignore exports tagged with `@internal`. This
|
|
509
|
-
can be useful in production mode, since `@internal` exports might be only imported from non-production code.
|
|
510
|
-
|
|
511
|
-
### Plugins
|
|
512
|
-
|
|
513
|
-
Plugins also have this distinction. For instance, Next.js entry files for pages (`pages/**/*.tsx`) and Remix routes
|
|
514
|
-
(`app/routes/**/*.tsx`) represent production code, while Jest and Storybook entry files (e.g. `*.spec.ts` or
|
|
515
|
-
`*.stories.js`) do not. All of this is handled automatically by Knip and its plugins.
|
|
516
|
-
|
|
517
|
-
## Output
|
|
518
|
-
|
|
519
|
-
### Screenshots
|
|
520
|
-
|
|
521
|
-
Here's an example run using the default reporter:
|
|
522
|
-
|
|
523
|
-
<img src="./assets/screenshot-exports.png" alt="example output of exported values and types" width="578">
|
|
524
|
-
|
|
525
|
-
This example shows more output related to unused and unlisted dependencies:
|
|
526
|
-
|
|
527
|
-
<img src="./assets/screenshot-dependencies.png" alt="example output of dependencies" width="578">
|
|
528
|
-
|
|
529
|
-
### Reading the report
|
|
530
|
-
|
|
531
|
-
The report contains the following types of issues:
|
|
532
|
-
|
|
533
|
-
| Title | Description | Key |
|
|
534
|
-
| :---------------------------------- | :---------------------------------------------- | :------------- |
|
|
535
|
-
| Unused files | unable to find references to this file | `files` |
|
|
536
|
-
| Unused dependencies | unable to find references to this dependency | `dependencies` |
|
|
537
|
-
| Unused devDependencies | unable to find references to this devDependency | `dependencies` |
|
|
538
|
-
| Unlisted dependencies | used dependencies not listed in package.json | `unlisted` |
|
|
539
|
-
| Unlisted binaries | binaries from deps not listed in package.json | `binaries` |
|
|
540
|
-
| Unresolved imports | unable to resolve this (import) specifier | `unresolved` |
|
|
541
|
-
| Unused exports | unable to find references to this export | `exports` |
|
|
542
|
-
| Unused exports in namespaces | unable to find direct references to this export | `nsExports` |
|
|
543
|
-
| Unused exported types | unable to find references to this exported type | `types` |
|
|
544
|
-
| Unused exported types in namespaces | unable to find direct references to this export | `nsTypes` |
|
|
545
|
-
| Unused exported enum members | unable to find references to this enum member | `enumMembers` |
|
|
546
|
-
| Unused exported class members | unable to find references to this class member | `classMembers` |
|
|
547
|
-
| Duplicate exports | the same thing is exported more than once | `duplicates` |
|
|
548
|
-
|
|
549
|
-
When an issue type has zero issues, it is not shown.
|
|
550
|
-
|
|
551
|
-
Getting too many reported issues and false positives? Read more about [handling issues][49].
|
|
552
|
-
|
|
553
|
-
### Rules & Filters
|
|
554
|
-
|
|
555
|
-
Use rules or filters if you don't (yet) want all issue types Knip reports.
|
|
556
|
-
|
|
557
|
-
#### Rules
|
|
558
|
-
|
|
559
|
-
Use `rules` in the configuration to customize the issue types that count towards the total error count, or to exclude
|
|
560
|
-
them altogether.
|
|
561
|
-
|
|
562
|
-
- `error` (default): printed, adds to total error count (similar to the `--include` filter)
|
|
563
|
-
- `warn`: printed in faded/grey color, does not add to error count (i.e. the exit code)
|
|
564
|
-
- `off`: not printed, does not add to error count (similar to the `--exclude` filter)
|
|
565
|
-
|
|
566
|
-
Example:
|
|
567
|
-
|
|
568
|
-
```json
|
|
569
|
-
{
|
|
570
|
-
"rules": {
|
|
571
|
-
"files": "warn",
|
|
572
|
-
"classMembers": "off",
|
|
573
|
-
"duplicates": "off"
|
|
574
|
-
}
|
|
575
|
-
}
|
|
576
|
-
```
|
|
577
|
-
|
|
578
|
-
See [reading the report][31] for the list of issue types.
|
|
579
|
-
|
|
580
|
-
The rules are modeled after the ESLint `rules` configuration, and could be extended in the future. For instance, to
|
|
581
|
-
apply filters or configurations only to a specific issue type.
|
|
582
|
-
|
|
583
|
-
#### Filters
|
|
584
|
-
|
|
585
|
-
You can `--include` or `--exclude` any of the reported issue types to slice & dice the report to your needs.
|
|
586
|
-
Alternatively, they can be added to the configuration (e.g. `"exclude": ["dependencies"]`).
|
|
587
|
-
|
|
588
|
-
Use `--include` to report only specific issue types (the following example commands do the same):
|
|
589
|
-
|
|
590
|
-
knip --include files --include dependencies
|
|
591
|
-
knip --include files,dependencies
|
|
592
|
-
|
|
593
|
-
Use `--exclude` to ignore reports you're not interested in:
|
|
594
|
-
|
|
595
|
-
knip --include files --exclude classMembers,enumMembers
|
|
596
|
-
|
|
597
|
-
Use `--dependencies` or `--exports` as shortcuts to combine groups of related types.
|
|
598
|
-
|
|
599
|
-
See [reading the report][31] for the list of issue types.
|
|
600
|
-
|
|
601
|
-
#### When to use rules or filters
|
|
602
|
-
|
|
603
|
-
Filters are meant to be used as command-line flags, rules allow for more fine-grained configuration.
|
|
604
|
-
|
|
605
|
-
- Rules are more fine-grained since they also have "warn".
|
|
606
|
-
- Rules could be extended in the future.
|
|
607
|
-
- Filters can be set in configuration and from CLI, rules only in configuration.
|
|
608
|
-
- Filters have two groups (`--dependencies` and `--exports`), rules don't have any grouping.
|
|
609
|
-
|
|
610
|
-
### Reporters
|
|
611
|
-
|
|
612
|
-
Knip provides the following built-in reporters:
|
|
613
|
-
|
|
614
|
-
- codeowners
|
|
615
|
-
- compact
|
|
616
|
-
- json
|
|
617
|
-
- symbol (default)
|
|
618
|
-
|
|
619
|
-
#### Custom Reporters
|
|
620
|
-
|
|
621
|
-
When the provided built-in reporters are not sufficient, a custom reporter can be implemented.
|
|
622
|
-
|
|
623
|
-
Find more details in [reporters and preprocessors][50].
|
|
624
|
-
|
|
625
|
-
#### Preprocessers
|
|
626
|
-
|
|
627
|
-
Use preprocessers to modify the results before they're passed to the reporter(s).
|
|
628
|
-
|
|
629
|
-
Find more details in [reporters and preprocessors][50].
|
|
630
|
-
|
|
631
|
-
## Fixing Issues
|
|
632
|
-
|
|
633
|
-
This is the fun part! Knip, knip, knip ✂️
|
|
634
|
-
|
|
635
|
-
Tip: back up files or use an VCS like Git before deleting files or making changes. Run tests to verify results.
|
|
636
|
-
|
|
637
|
-
- Unused files can be removed.
|
|
638
|
-
- Unused dependencies can be removed from `package.json`.
|
|
639
|
-
- Unlisted dependencies should be added to `package.json`.
|
|
640
|
-
- Unresolved imports should be reviewed.
|
|
641
|
-
- Unused exports and types: remove the `export` keyword in front of unused exports. Then you can see whether the
|
|
642
|
-
variable or type is used within the same file. If this is not the case, it can be removed.
|
|
643
|
-
- Duplicate exports can be removed so they're exported only once.
|
|
644
|
-
|
|
645
|
-
Repeat the process to reveal new unused files and exports. It's so liberating to remove unused things!
|
|
646
|
-
|
|
647
|
-
Getting too many reported issues and false positives? Read more about [handling issues][49] describing potential causes
|
|
648
|
-
for false positives, and how to handle them.
|
|
649
|
-
|
|
650
|
-
## JSDoc tags
|
|
651
|
-
|
|
652
|
-
Knip takes the following JSDoc/TSDoc tags into account:
|
|
653
|
-
|
|
654
|
-
| Tag | Description |
|
|
655
|
-
| :---------- | :------------------------------------------------------ |
|
|
656
|
-
| `@public` | Do not report this unused export, type or member |
|
|
657
|
-
| `@beta` | ^^ Idem |
|
|
658
|
-
| `@internal` | Do not report this unused export in `--production` mode |
|
|
659
|
-
| `@alias` | Do not report this duplicate export |
|
|
660
|
-
|
|
661
|
-
## Command Line Options
|
|
662
|
-
|
|
663
|
-
$ npx knip --help
|
|
664
|
-
✂️ Find unused files, dependencies and exports in your JavaScript and TypeScript projects
|
|
665
|
-
|
|
666
|
-
Usage: knip [options]
|
|
667
|
-
|
|
668
|
-
Options:
|
|
669
|
-
-c, --config [file] Configuration file path (default: [.]knip.json[c], knip.js, knip.ts or package.json#knip)
|
|
670
|
-
-t, --tsConfig [file] TypeScript configuration path (default: tsconfig.json)
|
|
671
|
-
--production Analyze only production source files (e.g. no tests, devDependencies, exported types)
|
|
672
|
-
--strict Consider only direct dependencies of workspace (not devDependencies, not other workspaces)
|
|
673
|
-
--ignore-internal Ignore exports with tag @internal (JSDoc/TSDoc)
|
|
674
|
-
-W, --workspace [dir] Analyze a single workspace (default: analyze all configured workspaces)
|
|
675
|
-
--no-gitignore Don't use .gitignore
|
|
676
|
-
--include Report only provided issue type(s), can be comma-separated or repeated (1)
|
|
677
|
-
--exclude Exclude provided issue type(s) from report, can be comma-separated or repeated (1)
|
|
678
|
-
--dependencies Shortcut for --include dependencies,unlisted,unresolved
|
|
679
|
-
--exports Shortcut for --include exports,nsExports,classMembers,types,nsTypes,enumMembers,duplicates
|
|
680
|
-
--include-entry-exports Include entry files when reporting unused exports
|
|
681
|
-
-n, --no-progress Don't show dynamic progress updates (automatically enabled in CI environments)
|
|
682
|
-
--preprocessor Preprocess the results before providing it to the reporter(s), can be repeated
|
|
683
|
-
--preprocessor-options Pass extra options to the preprocessor (as JSON string, see --reporter-options example)
|
|
684
|
-
--reporter Select reporter: symbols, compact, codeowners, json, can be repeated (default: symbols)
|
|
685
|
-
--reporter-options Pass extra options to the reporter (as JSON string, see example)
|
|
686
|
-
--no-config-hints Suppress configuration hints
|
|
687
|
-
--no-exit-code Always exit with code zero (0)
|
|
688
|
-
--max-issues Maximum number of issues before non-zero exit code (default: 0)
|
|
689
|
-
-d, --debug Show debug output
|
|
690
|
-
--debug-file-filter Filter for files in debug output (regex as string)
|
|
691
|
-
--performance Measure count and running time of expensive functions and display stats table
|
|
692
|
-
-h, --help Print this help text
|
|
693
|
-
-V, --version Print version
|
|
694
|
-
|
|
695
|
-
(1) Issue types: files, dependencies, unlisted, unresolved, exports, nsExports, classMembers, types, nsTypes, enumMembers, duplicates
|
|
696
|
-
|
|
697
|
-
Examples:
|
|
698
|
-
|
|
699
|
-
$ knip
|
|
700
|
-
$ knip --production
|
|
701
|
-
$ knip --workspace packages/client --include files,dependencies
|
|
702
|
-
$ knip -c ./config/knip.json --reporter compact
|
|
703
|
-
$ knip --reporter codeowners --reporter-options '{"path":".github/CODEOWNERS"}'
|
|
704
|
-
$ knip --debug --debug-file-filter '(specific|particular)-module'
|
|
705
|
-
|
|
706
|
-
More documentation and bug reports: https://github.com/webpro/knip
|
|
707
|
-
|
|
708
|
-
## Potential boost with `--no-gitignore`
|
|
709
|
-
|
|
710
|
-
To increase performance in a large monorepo, check out [Potential boost with `--no-gitignore`][51].
|
|
711
|
-
|
|
712
|
-
## Comparison & Migration
|
|
713
|
-
|
|
714
|
-
This table is an ongoing comparison. Based on their docs (please report any mistakes):
|
|
715
|
-
|
|
716
|
-
| Feature | **knip** | [depcheck][52] | [unimported][53] | [ts-unused-exports][54] | [ts-prune][55] |
|
|
717
|
-
| :---------------------- | :------: | :------------: | :--------------: | :---------------------: | :------------: |
|
|
718
|
-
| Unused files | ✅ | - | ✅ | - | - |
|
|
719
|
-
| Unused dependencies | ✅ | ✅ | ✅ | - | - |
|
|
720
|
-
| Unlisted dependencies | ✅ | ✅ | ✅ | - | - |
|
|
721
|
-
| [Plugins][2] | ✅ | ✅ | ❌ | - | - |
|
|
722
|
-
| [Compilers][3] | ✅ | - | - | - | - |
|
|
723
|
-
| Unused exports | ✅ | - | - | ✅ | ✅ |
|
|
724
|
-
| Unused class members | ✅ | - | - | - | - |
|
|
725
|
-
| Unused enum members | ✅ | - | - | - | - |
|
|
726
|
-
| Duplicate exports | ✅ | - | - | ❌ | ❌ |
|
|
727
|
-
| Search namespaces | ✅ | - | - | ✅ | ❌ |
|
|
728
|
-
| Custom reporters | ✅ | - | - | - | - |
|
|
729
|
-
| JavaScript support | ✅ | ✅ | ✅ | - | - |
|
|
730
|
-
| Configure entry files | ✅ | ❌ | ✅ | ❌ | ❌ |
|
|
731
|
-
| [Workspaces][1] | ✅ | ❌ | ❌ | - | - |
|
|
732
|
-
| ESLint plugin available | - | - | - | ✅ | - |
|
|
733
|
-
|
|
734
|
-
✅ = Supported, ❌ = Not supported, - = Out of scope
|
|
735
|
-
|
|
736
|
-
Below some similar commands to get another idea of what Knip does in comparison:
|
|
737
|
-
|
|
738
|
-
### depcheck
|
|
739
|
-
|
|
740
|
-
The following commands are similar:
|
|
741
|
-
|
|
742
|
-
depcheck
|
|
743
|
-
knip --dependencies
|
|
744
|
-
|
|
745
|
-
### unimported
|
|
746
|
-
|
|
747
|
-
The following commands are similar:
|
|
748
|
-
|
|
749
|
-
unimported
|
|
750
|
-
knip --production --dependencies --include files
|
|
751
|
-
|
|
752
|
-
Also see [production mode][25].
|
|
753
|
-
|
|
754
|
-
### ts-unused-exports
|
|
755
|
-
|
|
756
|
-
The following commands are similar:
|
|
757
|
-
|
|
758
|
-
ts-unused-exports
|
|
759
|
-
knip --include exports,types,nsExports,nsTypes
|
|
760
|
-
knip --exports # Adds unused enum and class members
|
|
761
|
-
|
|
762
|
-
### ts-prune
|
|
763
|
-
|
|
764
|
-
The following commands are similar:
|
|
765
|
-
|
|
766
|
-
ts-prune
|
|
767
|
-
knip --include exports,types
|
|
768
|
-
knip --exports # Adds unused exports/types in namespaces and unused enum/class members
|
|
769
|
-
|
|
770
|
-
## Projects using Knip
|
|
771
|
-
|
|
772
|
-
Many thanks to some of the early adopters of Knip:
|
|
773
|
-
|
|
774
|
-
- [Block Protocol][56]
|
|
775
|
-
- [DeepmergeTS][57]
|
|
776
|
-
- [eslint-plugin-functional][58]
|
|
777
|
-
- [freeCodeCamp.org][59]
|
|
778
|
-
- [is-immutable-type][60]
|
|
779
|
-
- [IsaacScript][61]
|
|
780
|
-
- [Nuxt][62]
|
|
781
|
-
- [Owncast][63]
|
|
782
|
-
- [release-it][64]
|
|
783
|
-
- [Template TypeScript Node Package][65]
|
|
784
|
-
- [Tipi][66]
|
|
785
|
-
|
|
786
|
-
## Articles, etc.
|
|
787
|
-
|
|
788
|
-
- Discord: hang out in [The Knip Barn][10]
|
|
789
|
-
- Ask your questions in the [Knip knowledge base][67] (powered by OpenAI and [7-docs][68], experimental!)
|
|
790
|
-
- Smashing Magazine: [Knip: An Automated Tool For Finding Unused Files, Exports, And Dependencies][69]
|
|
791
|
-
- Effective TypeScript: [Recommendation Update: ✂️ Use knip to detect dead code and types][70]
|
|
792
|
-
- Josh Goldberg: [Speeding Up Centered Part 4: Unused Code Bloat][71]
|
|
793
|
-
|
|
794
|
-
## Why "Knip"?
|
|
795
|
-
|
|
796
|
-
Knip is Dutch for a "cut". A Dutch expression is "ergens ge**knip**t voor zijn", which means to be perfectly suited for
|
|
797
|
-
the job. I'm motivated to make Knip perfectly suited for the job of cutting projects to perfection! ✂️
|
|
798
|
-
|
|
799
|
-
## Really, another unused file/dependency/export finder?
|
|
800
|
-
|
|
801
|
-
As listed above, there are already some great packages available if you want to find unused dependencies OR unused
|
|
802
|
-
exports. I love the Unix philosophy ("do one thing well"). But looking at what Knip does, I believe it's efficient to
|
|
803
|
-
handle multiple concerns in a single tool. When building a dependency graph of the project, an abstract syntax tree for
|
|
804
|
-
each file, and traversing all of this, why not collect the various issues in one go?
|
|
8
|
+
This package is part of [the Knip monorepo][2].
|
|
805
9
|
|
|
806
10
|
## Contributors
|
|
807
11
|
|
|
808
12
|
Special thanks to the wonderful people who have contributed to this project:
|
|
809
13
|
|
|
810
|
-
[![Contributors][
|
|
14
|
+
[![Contributors][4]][3]
|
|
811
15
|
|
|
812
|
-
[1]:
|
|
813
|
-
[2]:
|
|
814
|
-
[3]:
|
|
815
|
-
[4]:
|
|
816
|
-
[5]: #custom-reporters
|
|
817
|
-
[6]: #preprocessers
|
|
818
|
-
[7]: #jsdoc-tags
|
|
819
|
-
[8]: https://labs.openai.com/s/xZQACaLepaKya0PRUPtIN5dC
|
|
820
|
-
[9]: ./assets/cow-with-orange-scissors-van-gogh-style.webp
|
|
821
|
-
[10]: https://discord.gg/r5uXTtbTpc
|
|
822
|
-
[11]: https://twitter.com/webprolific
|
|
823
|
-
[12]: https://fosstodon.org/@webpro
|
|
824
|
-
[13]: https://github.com/webpro/knip/issues
|
|
825
|
-
[14]: #getting-started
|
|
826
|
-
[15]: #installation
|
|
827
|
-
[16]: #default-configuration
|
|
828
|
-
[17]: #lets-go
|
|
829
|
-
[18]: #configuration
|
|
830
|
-
[19]: #entry-files
|
|
831
|
-
[20]: #ignore-files-binaries-dependencies-and-workspaces
|
|
832
|
-
[21]: #public-exports
|
|
833
|
-
[22]: #ignore-exports-used-in-file
|
|
834
|
-
[23]: #include-exports-in-entry-files
|
|
835
|
-
[24]: #paths
|
|
836
|
-
[25]: #production-mode
|
|
837
|
-
[26]: #strict
|
|
838
|
-
[27]: #ignore-internal-exports
|
|
839
|
-
[28]: #plugins-1
|
|
840
|
-
[29]: #output
|
|
841
|
-
[30]: #screenshots
|
|
842
|
-
[31]: #reading-the-report
|
|
843
|
-
[32]: #rules--filters
|
|
844
|
-
[33]: #fixing-issues
|
|
845
|
-
[34]: #command-line-options
|
|
846
|
-
[35]: #potential-boost-with---no-gitignore
|
|
847
|
-
[36]: #comparison--migration
|
|
848
|
-
[37]: #depcheck
|
|
849
|
-
[38]: #unimported
|
|
850
|
-
[39]: #ts-unused-exports
|
|
851
|
-
[40]: #ts-prune
|
|
852
|
-
[41]: #projects-using-knip
|
|
853
|
-
[42]: #articles-etc
|
|
854
|
-
[43]: #why-knip
|
|
855
|
-
[44]: #really-another-unused-filedependencyexport-finder
|
|
856
|
-
[45]: #contributors
|
|
857
|
-
[46]: https://nx.dev/concepts/integrated-vs-package-based
|
|
858
|
-
[47]: ./docs/writing-a-plugin.md
|
|
859
|
-
[48]: ./docs/compilers.md
|
|
860
|
-
[49]: ./docs/handling-issues.md
|
|
861
|
-
[50]: ./docs/reporters-and-preprocessors.md
|
|
862
|
-
[51]: ./docs/perf-boost-with-no-gitignore.md
|
|
863
|
-
[52]: https://github.com/depcheck/depcheck
|
|
864
|
-
[53]: https://github.com/smeijer/unimported
|
|
865
|
-
[54]: https://github.com/pzavolinsky/ts-unused-exports
|
|
866
|
-
[55]: https://github.com/nadeesha/ts-prune
|
|
867
|
-
[56]: https://github.com/blockprotocol/blockprotocol
|
|
868
|
-
[57]: https://github.com/RebeccaStevens/deepmerge-ts
|
|
869
|
-
[58]: https://github.com/eslint-functional/eslint-plugin-functional
|
|
870
|
-
[59]: https://github.com/freeCodeCamp/freeCodeCamp
|
|
871
|
-
[60]: https://github.com/RebeccaStevens/is-immutable-type
|
|
872
|
-
[61]: https://github.com/IsaacScript/isaacscript
|
|
873
|
-
[62]: https://github.com/nuxt/nuxt
|
|
874
|
-
[63]: https://github.com/owncast/owncast
|
|
875
|
-
[64]: https://github.com/release-it/release-it
|
|
876
|
-
[65]: https://github.com/JoshuaKGoldberg/template-typescript-node-package
|
|
877
|
-
[66]: https://github.com/meienberger/runtipi
|
|
878
|
-
[67]: https://knip.deno.dev
|
|
879
|
-
[68]: https://github.com/7-docs/7-docs
|
|
880
|
-
[69]: https://www.smashingmagazine.com/2023/08/knip-automated-tool-find-unused-files-exports-dependencies/
|
|
881
|
-
[70]: https://effectivetypescript.com/2023/07/29/knip/
|
|
882
|
-
[71]: https://www.joshuakgoldberg.com/blog/speeding-up-centered-part-4-unused-code-bloat/
|
|
883
|
-
[72]: https://github.com/webpro/knip/graphs/contributors
|
|
884
|
-
[73]: https://contrib.rocks/image?repo=webpro/knip
|
|
885
|
-
[plugin-angular]: ./src/plugins/angular
|
|
886
|
-
[plugin-ava]: ./src/plugins/ava
|
|
887
|
-
[plugin-babel]: ./src/plugins/babel
|
|
888
|
-
[plugin-capacitor]: ./src/plugins/capacitor
|
|
889
|
-
[plugin-changesets]: ./src/plugins/changesets
|
|
890
|
-
[plugin-commitizen]: ./src/plugins/commitizen
|
|
891
|
-
[plugin-commitlint]: ./src/plugins/commitlint
|
|
892
|
-
[plugin-cspell]: ./src/plugins/cspell
|
|
893
|
-
[plugin-cypress]: ./src/plugins/cypress
|
|
894
|
-
[plugin-drizzle]: ./src/plugins/drizzle
|
|
895
|
-
[plugin-eslint]: ./src/plugins/eslint
|
|
896
|
-
[plugin-gatsby]: ./src/plugins/gatsby
|
|
897
|
-
[plugin-github-actions]: ./src/plugins/github-actions
|
|
898
|
-
[plugin-husky]: ./src/plugins/husky
|
|
899
|
-
[plugin-jest]: ./src/plugins/jest
|
|
900
|
-
[plugin-lefthook]: ./src/plugins/lefthook
|
|
901
|
-
[plugin-lint-staged]: ./src/plugins/lint-staged
|
|
902
|
-
[plugin-markdownlint]: ./src/plugins/markdownlint
|
|
903
|
-
[plugin-mocha]: ./src/plugins/mocha
|
|
904
|
-
[plugin-next]: ./src/plugins/next
|
|
905
|
-
[plugin-npm-package-json-lint]: ./src/plugins/npm-package-json-lint
|
|
906
|
-
[plugin-nx]: ./src/plugins/nx
|
|
907
|
-
[plugin-nyc]: ./src/plugins/nyc
|
|
908
|
-
[plugin-playwright]: ./src/plugins/playwright
|
|
909
|
-
[plugin-playwright-ct]: ./src/plugins/playwright-ct
|
|
910
|
-
[plugin-postcss]: ./src/plugins/postcss
|
|
911
|
-
[plugin-prettier]: ./src/plugins/prettier
|
|
912
|
-
[plugin-release-it]: ./src/plugins/release-it
|
|
913
|
-
[plugin-remark]: ./src/plugins/remark
|
|
914
|
-
[plugin-remix]: ./src/plugins/remix
|
|
915
|
-
[plugin-rollup]: ./src/plugins/rollup
|
|
916
|
-
[plugin-semantic-release]: ./src/plugins/semantic-release
|
|
917
|
-
[plugin-sentry]: ./src/plugins/sentry
|
|
918
|
-
[plugin-storybook]: ./src/plugins/storybook
|
|
919
|
-
[plugin-stryker]: ./src/plugins/stryker
|
|
920
|
-
[plugin-stylelint]: ./src/plugins/stylelint
|
|
921
|
-
[plugin-svelte]: ./src/plugins/svelte
|
|
922
|
-
[plugin-tailwind]: ./src/plugins/tailwind
|
|
923
|
-
[plugin-typedoc]: ./src/plugins/typedoc
|
|
924
|
-
[plugin-typescript]: ./src/plugins/typescript
|
|
925
|
-
[plugin-vite]: ./src/plugins/vite
|
|
926
|
-
[plugin-vitest]: ./src/plugins/vitest
|
|
927
|
-
[plugin-webpack]: ./src/plugins/webpack
|
|
16
|
+
[1]: https://knip.dev
|
|
17
|
+
[2]: https://github.com/webpro/knip
|
|
18
|
+
[3]: https://github.com/webpro/knip/graphs/contributors
|
|
19
|
+
[4]: https://contrib.rocks/image?repo=webpro/knip
|