Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comparison between knip and Treeshaking #845

Closed
vincent890120 opened this issue Nov 22, 2024 · 3 comments
Closed

Comparison between knip and Treeshaking #845

vincent890120 opened this issue Nov 22, 2024 · 3 comments
Labels
discussion Discussion

Comments

@vincent890120
Copy link

Discuss anything related to Knip

I find knip very useful, but I want to understand what the difference is between knip and Treeshaking in dealing with invalid code?

@vincent890120 vincent890120 added the discussion Discussion label Nov 22, 2024
@webpro
Copy link
Collaborator

webpro commented Nov 25, 2024

That's a great question!

Knip and tree-shaking have their main goal in common: strip out anything that's unused to optimize UX.

However, there's more to the story. Let's try to break it down a bit (non-exhaustive):

  • Activity/time
    • Tree-shaking is a build/compile time activity.
    • Knip is a linter, part of QA (during dev/maintenance or in watch mode it's directly a dev/IDE time activity).
  • Source vs production code
    • Tree-shaking is typically done on bundled production code.
    • Knip analyzes only source code.
  • Scope
    • Tree-shaking can also operate on third-party code.
    • Knip reports and fixes only issues with source code.
  • Knip has a broader scope:
    • Besides optimizing UX, Knip focuses on DX as well: keeping unused things around is bad in terms of maintenance and cognitive load (also see Less is more).
    • Knip also includes non-production code and dependencies in the process by default (like tests, stories, etc).
    • Knip reports more than only unused things. Example: relying on transitive dependencies is not a good idea, so Knip reports unlisted dependencies (also see issue types).

Other things they have in common:

  • There's a few more details regarding CommonJS and ESM that complicate things for both types of tooling (also see Working with CommonJS from a Knip perspective).
  • They're both not perfect. They complement each other, so in general I'd advice to use both for best results.

@webpro webpro closed this as completed in 7d7802c Nov 25, 2024
@webpro
Copy link
Collaborator

webpro commented Nov 25, 2024

https://knip.dev/reference/faq#isnt-tree-shaking-enough

@webpro
Copy link
Collaborator

webpro commented Nov 26, 2024

🚀 This issue has been resolved in v5.38.0. See Release 5.38.0 for release notes.

Using Knip in a commercial project? Please consider becoming a sponsor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Discussion
Projects
None yet
Development

No branches or pull requests

2 participants