🧩 Finding references to `.json` files
Opened this issue · 10 comments
Discuss anything related to Knip
Hello :)
I'm using knip --trace
to get and process the dependency graph, however it won't list .json
files I import in my .ts
, .tsx
files. Is/will that be possible? :)
Since .json is not a default extension you could try to add a compiler for it and do something like this:
{
compilers: {
json: text => `export default ${JSON.parse(text)}`
}
}
Sadly it didn't work. I had to filter out the tsconfig.json
, because it threw errors, but no success anyway.
Also, my JSON files are in public
directory (NextJS)
"compilerOptions": {
"paths": {
"@locales/*": ["public/locales/*"]
}
}
but it didn't work either when I manually moved a couple of JSONs to the src/something
and import from there.
Thanks for trying to help though
Without a reproduction or something it's mostly guessing as there isn't much to look into. Haven't had a need for this myself nor did i see similar reports.
Apologies, here's the repro https://github.com/bartekczyz/knip-json-references-repro
@webpro mind taking a look? 😅
Well, I'm currently at work. This open source business isn't that lucrative! No problem, no offense, but I'll be doing this as time and motivation permits. Sometimes instantly, sometimes... not.
Sorry mate, didn't want to sound so pushy :<
No worries, valid use case and repro after all.
Here's a version you could try:
npm i -D https://pkg.pr.new/knip@aced042
The downside of this approach, and the reason it's not in main yet, is that all the *.json
files are now part of the set of project files, so you might need to exclude unused *.json
files from the project
files (e.g. project: ["!package.json", "!path/to/some.json"]
I do see the value of the use case though, and this is only an issue when actually adding the json
compiler. But let's test it a bit better first. Notes:
- Since JSON is valid JS, eg.
text => export default ${text}
should work for any JSON (also the invalidtsconfig.json
) - Other file types like CSS could be included this way using compilers as well.
It worked like I expected, thanks!
all the *.json files are now part of the set of project files, so you might need to exclude unused *.json files from the project files (e.g. project: ["!package.json", "!path/to/some.json"]
Currently we use knip
only to build a graph of dependencies, so ☝🏻 is not a problem for us and we didn't have to exclude anything from project files (except filtering JSONs in the json compiler function).
One thing that regressed is cache files appearing in the cwd:
pnpm knip --directory ../../ --include-entry-exports --trace --cache