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

🧩 Finding references to .json files #883

Open
bartekczyz opened this issue Dec 18, 2024 · 10 comments
Open

🧩 Finding references to .json files #883

bartekczyz opened this issue Dec 18, 2024 · 10 comments
Labels
discussion Discussion

Comments

@bartekczyz
Copy link

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 bartekczyz added the discussion Discussion label Dec 18, 2024
@webpro
Copy link
Collaborator

webpro commented Dec 18, 2024

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

@bartekczyz
Copy link
Author

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

@webpro
Copy link
Collaborator

webpro commented Dec 19, 2024

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 webpro closed this as completed Dec 19, 2024
@bartekczyz
Copy link
Author

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

@bartekczyz
Copy link
Author

@webpro mind taking a look? 😅

@webpro
Copy link
Collaborator

webpro commented Dec 19, 2024

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.

@bartekczyz
Copy link
Author

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

@webpro
Copy link
Collaborator

webpro commented Dec 22, 2024

No worries, valid use case and repro after all.

@webpro
Copy link
Collaborator

webpro commented Dec 22, 2024

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.

@bartekczyz
Copy link
Author

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

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