knip icon indicating copy to clipboard operation
knip copied to clipboard

🧩 Finding references to `.json` files

Open bartekczyz opened this issue 1 year ago • 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? :)

bartekczyz avatar Dec 18 '24 14:12 bartekczyz

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)}`
  }
}

webpro avatar Dec 18 '24 14:12 webpro

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

bartekczyz avatar Dec 19 '24 08:12 bartekczyz

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.

webpro avatar Dec 19 '24 08:12 webpro

Apologies, here's the repro https://github.com/bartekczyz/knip-json-references-repro

bartekczyz avatar Dec 19 '24 09:12 bartekczyz

@webpro mind taking a look? 😅

bartekczyz avatar Dec 19 '24 11:12 bartekczyz

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.

webpro avatar Dec 19 '24 13:12 webpro

Sorry mate, didn't want to sound so pushy :<

bartekczyz avatar Dec 19 '24 21:12 bartekczyz

No worries, valid use case and repro after all.

webpro avatar Dec 22 '24 16:12 webpro

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 invalid tsconfig.json)
  • Other file types like CSS could be included this way using compilers as well.

webpro avatar Dec 22 '24 17:12 webpro

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

bartekczyz avatar Dec 27 '24 09:12 bartekczyz

Closing this issue as part of a general cleanup to keep this project sustainable and optimize my time working on it. If you think this is inappropriate or if there is no workaround and you feel stuck, feel free to open a new issue. Thanks for your understanding.

webpro avatar Aug 10 '25 07:08 webpro