Skip to content
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
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']))
})
})