-
-
Notifications
You must be signed in to change notification settings - Fork 195
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
How to handle unused code in a shared package within a monorepo #472
Comments
So you'd need to use |
Hey @webpro ! I'll share the repository with you a bit later. I'll also share the solution I was able to come up with. It's a bit strange, but it works. Perhaps you can provide some insights on how to improve it. |
I don't have a minimal repro currently, but enabling that flag in our repo gave me a billion false positives, unfortunately :( I would say it doesn't follow source maps, is the problem? Entry file used to detect code usage in the package is |
Yes, I had the same issue. I had to use the |
Hey @webpro! So, once again, the problem I want to solve is - when using a typescript monorepo, when there are unused things in packages/shared, knip should complained about them. In the example repository, the packages/shared exports two functions, As already mentioned, to detect unused items from packages/shared in other applications, the Since npm cannot import non-js files, it means that we need to convert all files in packages/shared from // packages/shared/package.json
{
"name": "@kp/shared",
"main": "build/index.js",
"types": "build/index.d.ts",
} Accordingly, if knip has the entry set to src/index.ts, it will complain about all files as unused because other applications import items from packages/shared specifically through packages/shared/build/index.js as the entry. It means we need to specify the entry for knip for packages/shared as build/index.js, and everything should work. But I couldn't make it work either, as knip continued to complain about unused files that were actually used in other applications. // knip.js
'packages/shared': {
entry: ['build/index.d.ts'],
ignore: ['src/**/*', 'build/helpers/**/*.d.ts'],
}, Only with this configuration, I was able to make knip complain about the unused function |
The issue primarily stems from the fact that Knip is about source code, not build artifacts. Adding build artifacts as entry files to the configuration does not "fix the chain", especially considering they're Feel free to bend and flex Knip any way you like, but I just can't support this use case atm for multiple reasons, and I'm not sure if that will ever be the case. |
Hey @webpro!
Exactly! That's why I had to use the --no-gitignore flag. During my latest experiment with integrating knip into another project, I made a slight improvement to the solution. By specifying the Shared folder packages.json - https://github.com/what1s1ove/knip-playground-ts/blob/main/packages/shared/package.json Once again, you rightly noted that this solution won't suit everyone, as I mentioned earlier in the post, since the builds and workflows of all monorepo projects vary, unfortunately. There are so many tools nowadays to achieve the same thing... |
Hello, guys!
The question is as follows: currently, when adding any unused imports to the
packages/shared
module in the monorepo repository, knip does not raise any complaints about them (see the image below). Is there a way to make knip complain aboutbar
?Knip config:
Project structure:
The text was updated successfully, but these errors were encountered: