Skip to content

Commit 993e878

Browse files
committed
Use knip
1 parent dbd6309 commit 993e878

File tree

3 files changed

+446
-5
lines changed

3 files changed

+446
-5
lines changed

knip.config.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type { KnipConfig } from 'knip';
2+
import { parse, type SFCScriptBlock, type SFCStyleBlock } from 'vue/compiler-sfc';
3+
4+
function getScriptBlockContent(block: SFCScriptBlock | null): string[] {
5+
if (!block) return [];
6+
if (block.src) return [`import '${block.src}'`];
7+
return [block.content];
8+
}
9+
10+
function getStyleBlockContent(block: SFCStyleBlock | null): string[] {
11+
if (!block) return [];
12+
if (block.src) return [`@import '${block.src}';`];
13+
return [block.content];
14+
}
15+
16+
function getStyleImports(content: string): string {
17+
return [...content.matchAll(/(?<=@)import[^;]+/g)].join('\n');
18+
}
19+
20+
const config = {
21+
entry: ['resources/js/{index,exports}.js', 'resources/js/frontend/helpers.js'],
22+
project: ['resources/js/**/*.{js,vue}'],
23+
compilers: {
24+
vue: (text: string, filename: string) => {
25+
const { descriptor } = parse(text, { filename, sourceMap: false });
26+
return [
27+
...getScriptBlockContent(descriptor.script),
28+
...getScriptBlockContent(descriptor.scriptSetup),
29+
...descriptor.styles.flatMap(getStyleBlockContent).map(getStyleImports),
30+
].join('\n');
31+
},
32+
},
33+
} satisfies KnipConfig;
34+
35+
export default config;

0 commit comments

Comments
 (0)