Skip to content

Commit

Permalink
feat: allow using regex or function in exclude option (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
stafyniaksacha committed Mar 21, 2023
1 parent 83b4261 commit 0d7d1df
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
5 changes: 4 additions & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export default defineNuxtConfig({
],

componentMeta: {
debug: 2
debug: 2,
exclude: [/test/i, (component: any) => {
return component.global
}]
},

pinceau: {
Expand Down
2 changes: 1 addition & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface ModuleOptions {
debug?: boolean | 2
componentDirs: (string | ComponentsDir)[]
components?: ComponentsOptions[]
exclude?: string[]
exclude?: (string | RegExp | ((component: any) => boolean))[]
checkerOptions?: MetaCheckerOptions
transformers?: ((component: any, code: string) => ({ component: any; code: string }))[]
globalsOnly?: boolean
Expand Down
20 changes: 17 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,29 @@ export function useComponentMetaParser (

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

const isExcluded = (component: any) => {
return exclude.find((excludeRule) => {
switch (typeof excludeRule) {
case 'string':
return component.filePath.includes(excludeRule)
case 'object':
return excludeRule instanceof RegExp ? excludeRule.test(component.filePath) : false
case 'function':
return excludeRule(component)
default:
return false
}
})
}

/**
* Initialize component data object from components
*/
const components = {
...(_components || []).reduce(
(acc: any, component: any) => {
// Locally support exclude as it seem broken from createComponentMetaCheckerByJsonConfig
if (exclude.find(excludePath => component.filePath.includes(excludePath))) { return acc }
if (isExcluded(component)) { return acc }

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

Expand Down Expand Up @@ -68,8 +83,7 @@ export function useComponentMetaParser (
include: [
'**/*',
...componentDirs.map(dir => `${typeof dir === 'string' ? dir : (dir?.path || '')}/**/*`)
],
exclude
]
},
checkerOptions
)
Expand Down

0 comments on commit 0d7d1df

Please sign in to comment.