Skip to content

Commit

Permalink
fix: globsExclude (#645)
Browse files Browse the repository at this point in the history
  • Loading branch information
lishaobos committed Jul 18, 2023
1 parent cf67b8c commit 2aafa44
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/core/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@ function normalizeResolvers(resolvers: (ComponentResolver | ComponentResolver[])
return toArray(resolvers).flat().map(r => typeof r === 'function' ? { resolve: r, type: 'component' } : r)
}

function resolveGlobsExclude(root: string, glob: string) {
const excludeReg = /^!/
return `${excludeReg.test(glob) ? '!' : ''}${resolve(root, glob.replace(excludeReg, ''))}`
}

export function resolveOptions(options: Options, root: string): ResolvedOptions {
const resolved = Object.assign({}, defaultOptions, options) as ResolvedOptions
resolved.resolvers = normalizeResolvers(resolved.resolvers)
resolved.extensions = toArray(resolved.extensions)

if (resolved.globs) {
resolved.globs = toArray(resolved.globs).map((glob: string) => slash(resolve(root, glob)))
resolved.globs = toArray(resolved.globs).map((glob: string) => slash(resolveGlobsExclude(root, glob)))
resolved.resolvedDirs = []
}
else {
Expand All @@ -40,7 +45,7 @@ export function resolveOptions(options: Options, root: string): ResolvedOptions
: `{${resolved.extensions.join(',')}}`

resolved.dirs = toArray(resolved.dirs)
resolved.resolvedDirs = resolved.dirs.map(i => slash(resolve(root, i)))
resolved.resolvedDirs = resolved.dirs.map(i => slash(resolveGlobsExclude(root, i)))

resolved.globs = resolved.resolvedDirs.map(i => resolved.deep
? slash(join(i, `**/*.${extsGlob}`))
Expand Down
26 changes: 26 additions & 0 deletions test/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,30 @@ describe('search', () => {

expect(cleanup(ctx.componentNameMap)).toMatchSnapshot()
})

it('should globs exclude work', () => {
const ctx = new Context({
globs: [
'src/components/*.vue',
'!src/components/ComponentA.vue',
],
})
ctx.setRoot(root)
ctx.searchGlob()

expect(cleanup(ctx.componentNameMap).map(i => i.as)).not.toEqual(expect.arrayContaining(['ComponentA']))
})

it('should globs exclude work with dirs', () => {
const ctx = new Context({
dirs: [
'src/components',
'!src/components/book',
],
})
ctx.setRoot(root)
ctx.searchGlob()

expect(cleanup(ctx.componentNameMap).map(i => i.as)).not.toEqual(expect.arrayContaining(['Book']))
})
})

0 comments on commit 2aafa44

Please sign in to comment.