Skip to content

Announcing Knip v4

Published: 2024-01-16

I’m happy to announce that Knip v4 is available!

The work took over a month and the process of slimming down to speed up ended up really well: significant faster runs and reduced memory usage. In the meantime, v3 continued to receive more contributions, plugins and bug fixes.

Highlights

Compared to v3, here are the highlights:

  • Performance: significant speed bump (up to 80%!)
  • Performance: globbing in combo with .gitignore is a lot more efficient
  • Configuration: built-in compilers (for Astro, MDX, Svelte & Vue)
  • The ignore option has been improved
  • Internal refactoring to serialize data for future improvements like caching.

The actual performance win in your projects depends on various factors like size and complexity.

Major Changes

The changes have been tested against various repositories, but it’s possible that you will encounter false positives caused by the major refactoring that has been done. If you do, please report!

Unused Class Members

Finding unused class members is no longer enabled by default. Here’s why it’s now opt-in:

  • When using Knip for the first time on a large repository it can crash after a while with an out of memory error. This is a terrible experience.
  • Plenty of codebases don’t use classes at all, keeping TS programs in memory is a waste of resources.
  • Many configurations already exclude classMembers from the output.

Enable unused class members by using the CLI argument or the configuration option:

Terminal window
knip --include classMembers
{
"include": ["classMembers"]
}

Now that unused class members is opt-in and better organized within Knip, it might be interesting to start looking at opt-ins for other unused members, such as those of types and interfaces.

By the way, enum members are “cheap” with the v4 refactor, so those are still included by default.

Compilers

You can remove the compilers option from your configuration. Since you can override them, your custom compilers can stay where they are. This also means that you can go back from knip.ts to knip.json if you prefer.

Ignore Files

The ignore option accepted patterns like examples/, but if you want to ignore the files inside this folder you should update to globs like examples/**.

What’s Next?

The refactoring for this release opens the door to more optimizations, such as caching. I’m also very excited to see how deeper integrations such as in GitHub Actions or IDEs like VS Code or WebStorm may further develop.

Remember, if you are you using Knip at work your company can sponsor me!

One More Thing…

An idea I’ve been toying with is “tagged exports”. The idea is that you can tag exports in a JSDoc comment. The tag does not need to be part of the JSDoc or TSDoc spec. For example:

/** @custom */
export const myExport = 1;

Then, include or exclude such tagged exports from the report like so:

Terminal window
knip --experimental-tags=+custom
knip --experimental-tags=-custom,-internal

This way, you can either focus on or ignore specific tagged exports with tags you define yourself. This also works for individual class or enum members.

Once this feature is intuitive and stable, the experimental flag will be removed and option(s) added to the Knip configuration file. The docs are in the CLI reference.

Let’s Go!

What are you waiting for? Start using Knip v4 today!

Terminal window
npm install -D knip

Remember, Knip it before you ship it! Have a great day ☀️

ISC License © 2024 Lars Kappert