Skip to content

Commit

Permalink
Update compilers docs + fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Oct 17, 2023
1 parent 02bc3e3 commit 7f63c75
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 21 deletions.
16 changes: 9 additions & 7 deletions README.md
Expand Up @@ -318,6 +318,8 @@ themselves and/or `entry` files for Knip to analyze.

See each plugin's documentation for its default values.

In an Astro, Svelte or Vue project? Make sure to see [compilers][46] and add some extra configuration.

#### `config`

Plugins usually include `config` files. They are handled by the plugin's custom dependency finder, which returns all
Expand Down Expand Up @@ -355,7 +357,7 @@ level by setting it to `false` there, and vice versa.
#### Multi-project repositories

Some repositories have a single `package.json`, but consist of multiple projects with configuration files across the
repository (such as the [Nx "intregrated repo" style][46]). Let's assume some of these projects are apps and have their
repository (such as the [Nx "intregrated repo" style][47]). Let's assume some of these projects are apps and have their
own Cypress configuration and test files. In that case, we could configure the Cypress plugin like this:

```json
Expand All @@ -368,7 +370,7 @@ own Cypress configuration and test files. In that case, we could configure the C

#### Create a new plugin

Getting false positives because a plugin is missing? Want to help out? Please read more at [writing a plugin][47]. This
Getting false positives because a plugin is missing? Want to help out? Please read more at [writing a plugin][48]. This
guide also contains more details if you want to learn more about plugins and why they are useful.

### Compilers
Expand All @@ -391,15 +393,15 @@ export default {
};
```

Read [Compilers][48] for more details and examples.
Read [Compilers][46] for more details and examples.

### Ignore files, binaries, dependencies and workspaces

There are a few ways to tell Knip to ignore certain files, binaries, dependencies and workspaces. Some examples:

```json
{
"ignore": ["**/*.d.ts", "**/fixtures"],
"ignore": ["**/fixtures"],
"ignoreBinaries": ["zip", "docker-compose"],
"ignoreDependencies": ["hidden-package"],
"ignoreWorkspaces": ["packages/ignore", "packages/examples/**"]
Expand Down Expand Up @@ -883,9 +885,9 @@ Special thanks to the wonderful people who have contributed to this project:
[43]: #why-knip
[44]: #really-another-unused-filedependencyexport-finder
[45]: #contributors
[46]: https://nx.dev/concepts/integrated-vs-package-based
[47]: ./docs/writing-a-plugin.md
[48]: ./docs/compilers.md
[46]: ./docs/compilers.md
[47]: https://nx.dev/concepts/integrated-vs-package-based
[48]: ./docs/writing-a-plugin.md
[49]: ./docs/handling-issues.md
[50]: ./docs/reporters-and-preprocessors.md
[51]: ./docs/perf-boost-with-no-gitignore.md
Expand Down
46 changes: 35 additions & 11 deletions docs/compilers.md
@@ -1,9 +1,9 @@
# Compilers

Projects may have source files that are not JavaScript or TypeScript, and thus require compilation (or transpilation, or
pre-processing, you name it). Files like `.mdx`, `.vue` and `.svelte` may also import dependencies and other files. So
ideally, these files are included in the analysis to get a better overview of what files and dependencies are used or
not. To this end, Knip v2 supports a compiler for any file extension.
pre-processing, you name it). Files like `.mdx`, `.astro`, `.vue` and `.svelte` may also import dependencies and other
files. So ideally, these files are included in the analysis to get a better overview of what files and dependencies are
used or not. To this end, Knip v2 supports a compiler for any file extension.

## Prerequisites

Expand All @@ -21,9 +21,31 @@ This may also be an `async` function.

## Examples

- [Astro][1]
- [MDX][2]
- [Vue][3]
- [Svelte][4]

### Astro

Use a configuration like this to compile non-standard files in Astro projects:

```ts
export default {
ignore: '.astro/types.d.ts',
compilers: {
astro: (text: string) => [...text.matchAll(/import[^;]+/g)].join('\n'),
css: (text: string) => [...text.matchAll(/(?<=@)import[^;]+/g)].join('\n'),
mdx: (text: string) => [...text.matchAll(/import[^;]+/g)].join('\n'),
},
};
```

Knip has an [Astro plugin][5] to save you some configuration. It's enabled automatically.

### MDX

Here's an example using [@mdx-js/mdx][1] v1.6.22
Here's an example using [@mdx-js/mdx][6] v1.6.22

```ts
const compile = require('@mdx-js/mdx');
Expand All @@ -45,7 +67,6 @@ Here's a fully configured `knip.ts` with a "compiler" for `.vue` files in Vue pr
const compiler = /<script\b[^>]*>([\s\S]*?)<\/script>/gm;

export default {
ignore: '*.d.ts',
entry: ['src/main.ts', 'vite.config.ts'],
project: '**/*.{ts,vue}',
compilers: {
Expand All @@ -64,7 +85,7 @@ cases it's enough to extract and return only the `import` statements.

### Svelte

Here's another example, this one is for Svelte:
Use a configuration like this to compile non-standard files in Svelte projects:

```ts
import sveltePreprocess from 'svelte-preprocess';
Expand All @@ -73,7 +94,6 @@ import { preprocess, compile } from 'svelte/compiler';
const sveltePreprocessor = sveltePreprocess();

export default {
ignore: ['**/*.d.ts'],
paths: {
// This ain't pretty, but Svelte basically does the same
'$app/*': ['node_modules/@sveltejs/kit/src/runtime/app/*'],
Expand All @@ -91,8 +111,7 @@ export default {
```

The compiler for `.svelte` files in this example is the actual Svelte compiler, this is the recommended way whenever
available. Knip has a [Svelte plugin][2] to save you little bit of configuration. It's enabled automatically, so there's
nothing to configure.
available. Knip has a [Svelte plugin][7] to save you some configuration. It's enabled automatically.

Just for reference, this also seems to work pretty well (but may err on certain syntax or edge cases):

Expand All @@ -103,5 +122,10 @@ export default {
};
```

[1]: https://www.npmjs.com/package/@mdx-js/mdx
[2]: ../src/plugins/svelte
[1]: #astro
[2]: #mdx
[3]: #vue
[4]: #svelte
[5]: ../src/plugins/astro
[6]: https://www.npmjs.com/package/@mdx-js/mdx
[7]: ../src/plugins/svelte
2 changes: 1 addition & 1 deletion docs/handling-issues.md
Expand Up @@ -38,7 +38,7 @@ Here are a few things to consider when Knip reports unused files:

```json
{
"ignore": ["**/*.d.ts", "**/__mocks__", "**/__fixtures__"]
"ignore": ["**/__mocks__", "**/__fixtures__"]
}
```

Expand Down
1 change: 0 additions & 1 deletion fixtures/compilers-vue/knip.ts
@@ -1,7 +1,6 @@
const compiler = /<script\b[^>]*>([\s\S]*?)<\/script>/gm;

export default {
ignore: '*.d.ts',
entry: ['parent.vue'],
project: '**/*.{ts,vue}',
compilers: {
Expand Down
2 changes: 1 addition & 1 deletion fixtures/plugins/svelte/knip.ts
@@ -1,5 +1,5 @@
export default {
ignore: ['.svelte-kit', '**/*.d.ts'],
ignore: ['.svelte-kit'],
paths: {
'$app/*': ['node_modules/@sveltejs/kit/src/runtime/app/*'],
},
Expand Down

0 comments on commit 7f63c75

Please sign in to comment.