webpro-nl / knip

✂️ Find unused files, dependencies and exports in your JavaScript and TypeScript projects. Knip it before you ship it!
https://knip.dev
ISC License
6.65k stars 154 forks source link

Knip doesn't resolve imports in Nuxt projects #748

Closed BMFreed closed 1 month ago

BMFreed commented 1 month ago

Reproduction url

https://codesandbox.io/p/devbox/priceless-aryabhata-zl8s4x?file=%2Fapp.vue%3A8%2C8

Description of the issue

Knip doesn't seem to be working correctly in Nuxt projects. Here is an example when we import a function in app.vue: image

And that same function is then reported by Knip as an unused file, while the import is unresolved: image

For some reason, Knip doesn't resolve Nuxt paths properly.

Running pnpm knip --tsConfig .nuxt/tsconfig.json or copying the tsconfig.json from .nuxt folder into the main folder does not fix the issue.

webpro commented 1 month ago

The issue is that baseUrl isn't set in either of the tsconfig.json files.

For now you can do this:

{
  // https://nuxt.com/docs/guide/concepts/typescript
  "extends": "./.nuxt/tsconfig.json",
  "compilerOptions": {
    "baseUrl": ".nuxt"
  }
}

Or wait until the next release of Knip, this will have a patch to handle it for you.

BMFreed commented 1 month ago

The issue is that baseUrl isn't set in either of the tsconfig.json files.

For now you can do this:

{
  // https://nuxt.com/docs/guide/concepts/typescript
  "extends": "./.nuxt/tsconfig.json",
  "compilerOptions": {
    "baseUrl": ".nuxt"
  }
}

Or wait until the next release of Knip, this will have a patch to handle it for you.

It worked! Thank you so much!

webpro commented 1 month ago

:rocket: This issue has been resolved in v5.27.1. See Release 5.27.1 for release notes.

Using Knip in a commercial project? Please consider becoming a sponsor.

BMFreed commented 1 month ago

Unfortunately, there is another issue regarding Nuxt. If Vue isn't listed in dependencies or devDependencies - knip doesn't seem to enable the Vue plugin. In the reproduction URL I tried to enable the Vue plugin manually within knip.json by setting "vue" to true and to the default (as mentioned here) config value, but this doesn't work.

Can this be fixed without needing to add vue to dependencies or devDependencies?

Thanks in advance!

webpro commented 1 month ago

Can this be fixed without needing to add vue to dependencies or devDependencies?

Like you said, you can enable the Vue plugin manually. You can use --debug to see whether the plugin is enabled or not, and what it finds. I'd need a reproduction to look into it (please file a new issue then).