Skip to content

Commit

Permalink
Update docs (Configuring Project Files)
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Jun 17, 2024
1 parent d3a3927 commit e10ac2e
Showing 1 changed file with 61 additions and 18 deletions.
79 changes: 61 additions & 18 deletions packages/docs/src/content/docs/guides/configuring-project-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,32 @@ Let's dive in and expand on all of these.

## Unused files

Files are reported as unused if they are in the set of project files, but not in
the set of files resolved from the entry files:
Files are reported as unused if they are in the set of `project` files, but not
in the set of files resolved from the `entry` files. In other words, they're
calculated like so:

```
unused files = project files - (entry files + resolved files)
```

:::tip

To exclude files from the set of project files, first look at using negated
`project` patterns. This is recommended over `ignore` patterns.

:::

See [entry files][1] to see where Knip looks for entry files. Read on to learn
how to fine-tune the sets of entry and project files.
more about fine-tuning the sets of entry and project files.

## Negated patterns

Let's take a look at using negated patterns for `entry` and `project` files. If
you think there are too many files in the analysis, this could be the first step
in selecting the right files for the analysis.

Say we need to explicitly add route files as entry files, except those starting
with an underscore. Then we can use a negated pattern like so:
For example, we need to explicitly add route files as entry files, except those
starting with an underscore. Then we can use a negated pattern like so:

```json
{
Expand All @@ -54,18 +62,24 @@ reported as unused files, we can use negated `project` patterns:
}
```

By the way, we don't want to simply add all files as entry files for two
reasons:
It's not recommended to add all files as entry files for two reasons:

1. Knip does not report unused exports in entry files.
2. Configuring `entry` and `project` files properly allows Knip to find unused
files.

:::tip

Do not add too many `entry` files, you'll miss out on both unused exports and
unused files.

:::

## Ignore issues in specific files

Use `ignore` if a certain file contain unused exports that we want to ignore.
For example, this might happen with a generated file that exports "everything"
and we don't want the unused exports of this file to be reported:
For example, this might happen with generated files that export "everything" and
we don't want the unused exports of such files to be reported:

```json
{
Expand All @@ -75,14 +89,15 @@ and we don't want the unused exports of this file to be reported:
}
```

## Exclude non-production files
## Production Mode

In default mode, Knip includes all test files and other non-production files in
the analysis. To find out what files, dependencies and exports are unused in
production source files, use [production mode][2].

How NOT to exclude test files from the analysis? For a better understanding of
how Knip works, here's a list of options that don't work, and why:
How to exclude test and other non-production files from the analysis? For a
better understanding of how Knip works, here's a list of options that DON'T
work, and why.

❌   Don't do this:

Expand All @@ -94,27 +109,35 @@ how Knip works, here's a list of options that don't work, and why:

This is not a good idea, since `ignore` patterns have only one goal: to exclude
issues in matching files from the report. Files matching `ignore` patterns are
not excluded from the analysis, only their issues are not reported. This also
hurts performance, since the files are first analyzed, and eventually filtered
out.
not excluded from the analysis, only their issues are not reported.

:::tip

The goal of `ignore` patterns is to exclude the issues in matching files from
the report. These files are not excluded from the analysis.

:::

This is also not efficient, since the files are first analyzed, and eventually
filtered out.

❌   Also don't do this:

```json
{
"entry": ["!**/*.test.js"]
"entry": ["index.ts", "!**/*.test.js"]
}
```

This won't help if dependencies like Vitest or Ava are listed, because their
plugins will add test files as entry files anyway, which you can't and shouldn't
undo or override here.
undo or override here. Configure plugins individually if necessary.

❌   Also don't do this:

```json
{
"project": ["!**/*.spec.ts"]
"project": ["**/*.ts", "!**/*.spec.ts"]
}
```

Expand Down Expand Up @@ -157,5 +180,25 @@ using a separate configuration file for production mode:
knip --production --config knip.production.json
```

## Defaults & Plugins

To reiterate, the default `entry` and `project` files for each workspace:

```json
{
"entry": [
"{index,main,cli}.{js,cjs,mjs,jsx,ts,cts,mts,tsx}",
"src/{index,main,cli}.{js,cjs,mjs,jsx,ts,cts,mts,tsx}"
],
"project": ["**/*.{js,cjs,mjs,jsx,ts,cts,mts,tsx}"]
}
```

Next to this, there are other places where [Knip looks for entry files][1].

Additionally, [plugins have plenty of entry files configured][3] that are
automatically added as well.

[1]: ../explanations/entry-files.md
[2]: ../features/production-mode.md
[3]: ../explanations/plugins.md#entry-files

0 comments on commit e10ac2e

Please sign in to comment.