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

💡 Override package.json and/or return used deps #882

Open
omidantilong opened this issue Dec 18, 2024 · 7 comments
Open

💡 Override package.json and/or return used deps #882

omidantilong opened this issue Dec 18, 2024 · 7 comments
Labels
feature request Feature request

Comments

@omidantilong
Copy link

Suggest an idea for Knip

Hey! I'm new to knip but I've been really impressed with it so far, so firstly just wanted to say thanks for the time and effort in maintaining this. It's an awesome tool ❤️

So, I have a potentially weird use-case... At the moment, if I run knip against a bunch of built files, anything that is explicitly added as dependency in package.json is (correctly) not deemed to be missing.

But lets say I'm less concerned with whether or not imports are actually deemed to be "missing", I just want to get back a list of all the imports from statically analysing a bunch of files. The idea being that i can then use that list of modules to know exactly what should be in a stripped-back node_modules folder.

I can get this to work in a rudimentary way simply by commenting out this line. Of course doing that, everything is grouped under "unlisted".

There's more than one way to solve this I think:

  • Allow passing in a custom package object to override the local package.jsondepcheck exposes this option, and it works well
  • Add a config option to return listed deps as well as unlisted — but I appreciate this isn't the problem knip sets out to solve. But it could still be useful for folks who want to analyse imports and still get the other benefits knip brings

I'd be happy to open a PR for this if I can get it working. Apologies if this has been asked before, or if there's a way to do it already. I struggled to find anything when searching.

Thanks! ☺️

@omidantilong omidantilong added the feature request Feature request label Dec 18, 2024
@webpro
Copy link
Collaborator

webpro commented Dec 18, 2024

Thank you for the kind words, Omid!

Not sure if this useful for your use case, but I guess my main idea here is to eventually expose the module and dependency graph. If you'd add a inspect(graph) right after this statement:

await collectUnusedExports();

...does that contain the information you need? The idea is that you can then build on top of this graph any way you need. Unfortunately we're not there yet, but it's still useful to know already if this use case would fit.

@omidantilong
Copy link
Author

Hey @webpro thanks for the quick reply! Just taking a look at this now - finishing up at work for xmas so it's been a busy day...

Sorry if I've misunderstood — I added the inspect(graph) call, but it's not doing anything. Are you saying that's not actually implemented yet?

I think that would probably solve my use-case though, just going off the name graph 😄 If I understand correctly, it's essentially, "show me everything that knip encountered in its scan, regardless of whether it's unused or not"?

@webpro
Copy link
Collaborator

webpro commented Dec 18, 2024

Sorry I wasn't clear, I meant to call inspect(graph) to log the graph so you can see what it contains.

@omidantilong
Copy link
Author

Hmm yeah, I tried adding that directly under collectUnusedExports() and it doesn't seem to do anything... i am probably doing something wrong! Could you share a sample output?

@webpro
Copy link
Collaborator

webpro commented Dec 18, 2024

make sure to use something like tsx or bun with src/cli.ts or build it first

example from running knip in it's own repo

Screenshot 2024-12-18 at 8 10 29 PM

@omidantilong
Copy link
Author

I was just editing the file directly inside node_modules in the project I'm tinkering with. I know my changes are being picked up as that's how i figured out removing the isHandled check worked. But it seems like any console logs I add or inspect calls are getting swallowed somewhere.

I've just cloned the repo and if i run bun packages/knip/src/cli.ts i get the same output as you. That looks like it would do exactly what I need! Would be awesome if a helper function was provided to convert the nested Maps/Sets into something that can be stringified and written to a file.

As you said you're not quite there yet with this feature, what else is left to do?

Thanks a lot for your patience with me! 🙂

@webpro
Copy link
Collaborator

webpro commented Dec 18, 2024

That looks like it would do exactly what I need!

Nice, feedback like this helps to understand its potential better. Feel free to indicate if something's missing or would be cumbersome to use.

Would be awesome if a helper function was provided to convert the nested Maps/Sets into something that can be stringified and written to a file.

See serialize and deserialize in this repo (used by --cache)

As you said you're not quite there yet with this feature, what else is left to do?

It's mostly that exposing this might mean more efforts and maintenance for me. Also see https://knip.dev/reference/faq#why-doesnt-knip-have

Thanks a lot for your patience with me! 🙂

Always, love this project as it helps people making maintenance easier and more fun.

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

No branches or pull requests

2 participants