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
7.04k stars 174 forks source link

Suggestion: support finding references in MDX #494

Closed justingrant closed 8 months ago

justingrant commented 9 months ago

It's be great if knip supported .mdx files which are used by Storybook and other documentation generators. In our project, there are a few internal-facing exports that are referenced only in our docs but don't (currently) have runtime code accessing them.

A good example of this is a color palette, where we export categories of colors (reds, greens, etc.) so our dev docs can show a well-arranged palette page in the docs for our team members to choose from. But no actual code in our project imports greens. Instead, we import individual colors. But greens is not an unused export, because it's used by our docs.

Other than for documentation, AFAIK MDX is also sometimes used more generally as a low-cost way to generate mostly-static content.

webpro commented 9 months ago

There's a built-in compiler for MDX files, but it's currently limited to being auto-enabled only if @mdx-js/mdx or astro is in the list of dependencies (source code).

I guess we can add a dependency to the list of "enablers" here. What should that be? Some Storybook plugin?

To enable it today (without waiting for a fix in v4). In Knip v3, this was how you had to add it explicitly, and this should still work in v4:

https://knip.dev/v3/features/compilers#mdx

You can use any MDX compiler instead of the suggested one in the example (or even a regex like the other examples on the same page). Or copy the regex from Knip source code.

adufr commented 9 months ago

It looks like Storybook supports MDX without the need of any subpackage, so the dependency to add could just be storybook.

webpro commented 8 months ago

Going to close this one:

There's still the option to use different/custom compiler.

cylewaitforit commented 5 months ago

@webpro Writing stories in MDX was removed but writing docs for you storybooks in MDX is very much still in place.