Skip to content

Commit 0d7d1df

Browse files
feat: allow using regex or function in exclude option (#53)
1 parent 83b4261 commit 0d7d1df

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

playground/nuxt.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ export default defineNuxtConfig({
2424
],
2525

2626
componentMeta: {
27-
debug: 2
27+
debug: 2,
28+
exclude: [/test/i, (component: any) => {
29+
return component.global
30+
}]
2831
},
2932

3033
pinceau: {

src/options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface ModuleOptions {
88
debug?: boolean | 2
99
componentDirs: (string | ComponentsDir)[]
1010
components?: ComponentsOptions[]
11-
exclude?: string[]
11+
exclude?: (string | RegExp | ((component: any) => boolean))[]
1212
checkerOptions?: MetaCheckerOptions
1313
transformers?: ((component: any, code: string) => ({ component: any; code: string }))[]
1414
globalsOnly?: boolean

src/parser.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,29 @@ export function useComponentMetaParser (
2323

2424
const outputPath = join(outputDir, 'component-meta')
2525

26+
const isExcluded = (component: any) => {
27+
return exclude.find((excludeRule) => {
28+
switch (typeof excludeRule) {
29+
case 'string':
30+
return component.filePath.includes(excludeRule)
31+
case 'object':
32+
return excludeRule instanceof RegExp ? excludeRule.test(component.filePath) : false
33+
case 'function':
34+
return excludeRule(component)
35+
default:
36+
return false
37+
}
38+
})
39+
}
40+
2641
/**
2742
* Initialize component data object from components
2843
*/
2944
const components = {
3045
...(_components || []).reduce(
3146
(acc: any, component: any) => {
3247
// Locally support exclude as it seem broken from createComponentMetaCheckerByJsonConfig
33-
if (exclude.find(excludePath => component.filePath.includes(excludePath))) { return acc }
48+
if (isExcluded(component)) { return acc }
3449

3550
if (!component.filePath || !component.pascalName) { return acc }
3651

@@ -68,8 +83,7 @@ export function useComponentMetaParser (
6883
include: [
6984
'**/*',
7085
...componentDirs.map(dir => `${typeof dir === 'string' ? dir : (dir?.path || '')}/**/*`)
71-
],
72-
exclude
86+
]
7387
},
7488
checkerOptions
7589
)

0 commit comments

Comments
 (0)