Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow using regex or function in exclude option #53

Merged
merged 2 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }))[]
}
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