diff --git a/packages/docs/src/content/docs/features/compilers.md b/packages/docs/src/content/docs/features/compilers.md index 0bbc74eff..f175eb8a8 100644 --- a/packages/docs/src/content/docs/features/compilers.md +++ b/packages/docs/src/content/docs/features/compilers.md @@ -134,3 +134,38 @@ const config = { export default config; ``` + +### MDX + +In a project with MDX, the compiler is automatically enabled when `@mdx-js/*` or +`astro` are found in the list of dependencies +([source code](https://github.com/webpro-nl/knip/blob/main/packages/knip/src/compilers/mdx.ts)) +. Override the MDX compiler if + +- The built-in "compiler" is not automatically enabled because your code doesn't + use the dependencies in the built-in list. + + ```typescript + // knip.config.ts + import mdx from 'knip/dist/compilers/mdx'; + + const config: KnipConfig = { + // Manually enable built-in MDX compiler + compilers: { + mdx: mdx.compiler, + }, + }; + ``` + +- The built-in "compiler" is not enough + + ```typescript + // knip.config.ts + import { compile } from 'lib-that-has-the-mdx-compiler'; + + const config: KnipConfig = { + compilers: { + mdx: (text: string, filePath: string) => compile(text), + }, + }; + ``` diff --git a/packages/knip/src/compilers/mdx.ts b/packages/knip/src/compilers/mdx.ts index 13ee7717b..1cd98817e 100644 --- a/packages/knip/src/compilers/mdx.ts +++ b/packages/knip/src/compilers/mdx.ts @@ -1,7 +1,21 @@ import { fencedCodeBlockMatcher, importMatcher } from './compilers.js'; import type { HasDependency } from './types.js'; -const condition = (hasDependency: HasDependency) => hasDependency('@mdx-js/mdx') || hasDependency('astro'); +// https://mdxjs.com/packages/ +const mdxDependencies = [ + 'astro', + '@mdx-js/esbuild', + '@mdx-js/loader', + '@mdx-js/mdx', + '@mdx-js/node-loader', + '@mdx-js/preact', + '@mdx-js/react', + '@mdx-js/rollup', + '@mdx-js/vue', + 'remark-mdx', +]; + +const condition = (hasDependency: HasDependency) => mdxDependencies.some(hasDependency); const compiler = (text: string) => [...text.replace(fencedCodeBlockMatcher, '').matchAll(importMatcher)].join('\n');