Skip to content

Commit 7f63c75

Browse files
committedOct 17, 2023
Update compilers docs + fixtures
1 parent 02bc3e3 commit 7f63c75

File tree

5 files changed

+46
-21
lines changed

5 files changed

+46
-21
lines changed
 

‎README.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ themselves and/or `entry` files for Knip to analyze.
318318

319319
See each plugin's documentation for its default values.
320320

321+
In an Astro, Svelte or Vue project? Make sure to see [compilers][46] and add some extra configuration.
322+
321323
#### `config`
322324

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

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

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

369371
#### Create a new plugin
370372

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

374376
### Compilers
@@ -391,15 +393,15 @@ export default {
391393
};
392394
```
393395

394-
Read [Compilers][48] for more details and examples.
396+
Read [Compilers][46] for more details and examples.
395397

396398
### Ignore files, binaries, dependencies and workspaces
397399

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

400402
```json
401403
{
402-
"ignore": ["**/*.d.ts", "**/fixtures"],
404+
"ignore": ["**/fixtures"],
403405
"ignoreBinaries": ["zip", "docker-compose"],
404406
"ignoreDependencies": ["hidden-package"],
405407
"ignoreWorkspaces": ["packages/ignore", "packages/examples/**"]
@@ -883,9 +885,9 @@ Special thanks to the wonderful people who have contributed to this project:
883885
[43]: #why-knip
884886
[44]: #really-another-unused-filedependencyexport-finder
885887
[45]: #contributors
886-
[46]: https://nx.dev/concepts/integrated-vs-package-based
887-
[47]: ./docs/writing-a-plugin.md
888-
[48]: ./docs/compilers.md
888+
[46]: ./docs/compilers.md
889+
[47]: https://nx.dev/concepts/integrated-vs-package-based
890+
[48]: ./docs/writing-a-plugin.md
889891
[49]: ./docs/handling-issues.md
890892
[50]: ./docs/reporters-and-preprocessors.md
891893
[51]: ./docs/perf-boost-with-no-gitignore.md

‎docs/compilers.md

+35-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Compilers
22

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

88
## Prerequisites
99

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

2222
## Examples
2323

24+
- [Astro][1]
25+
- [MDX][2]
26+
- [Vue][3]
27+
- [Svelte][4]
28+
29+
### Astro
30+
31+
Use a configuration like this to compile non-standard files in Astro projects:
32+
33+
```ts
34+
export default {
35+
ignore: '.astro/types.d.ts',
36+
compilers: {
37+
astro: (text: string) => [...text.matchAll(/import[^;]+/g)].join('\n'),
38+
css: (text: string) => [...text.matchAll(/(?<=@)import[^;]+/g)].join('\n'),
39+
mdx: (text: string) => [...text.matchAll(/import[^;]+/g)].join('\n'),
40+
},
41+
};
42+
```
43+
44+
Knip has an [Astro plugin][5] to save you some configuration. It's enabled automatically.
45+
2446
### MDX
2547

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

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

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

6586
### Svelte
6687

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

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

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

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

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

@@ -103,5 +122,10 @@ export default {
103122
};
104123
```
105124

106-
[1]: https://www.npmjs.com/package/@mdx-js/mdx
107-
[2]: ../src/plugins/svelte
125+
[1]: #astro
126+
[2]: #mdx
127+
[3]: #vue
128+
[4]: #svelte
129+
[5]: ../src/plugins/astro
130+
[6]: https://www.npmjs.com/package/@mdx-js/mdx
131+
[7]: ../src/plugins/svelte

‎docs/handling-issues.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Here are a few things to consider when Knip reports unused files:
3838

3939
```json
4040
{
41-
"ignore": ["**/*.d.ts", "**/__mocks__", "**/__fixtures__"]
41+
"ignore": ["**/__mocks__", "**/__fixtures__"]
4242
}
4343
```
4444

‎fixtures/compilers-vue/knip.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const compiler = /<script\b[^>]*>([\s\S]*?)<\/script>/gm;
22

33
export default {
4-
ignore: '*.d.ts',
54
entry: ['parent.vue'],
65
project: '**/*.{ts,vue}',
76
compilers: {

‎fixtures/plugins/svelte/knip.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export default {
2-
ignore: ['.svelte-kit', '**/*.d.ts'],
2+
ignore: ['.svelte-kit'],
33
paths: {
44
'$app/*': ['node_modules/@sveltejs/kit/src/runtime/app/*'],
55
},

0 commit comments

Comments
 (0)
Please sign in to comment.