Skip to content

Commit 6656ec5

Browse files
authored
fix: fix how globs resolves, excludes nested node_modules folder by default (#826)
1 parent c69425e commit 6656ec5

File tree

4 files changed

+48
-16
lines changed

4 files changed

+48
-16
lines changed

src/core/options.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { slash, toArray } from '@antfu/utils'
44
import { getPackageInfoSync, isPackageExists } from 'local-pkg'
55
import { detectTypeImports } from './type-imports/detect'
66

7-
export const defaultOptions: Omit<Required<Options>, 'include' | 'exclude' | 'excludeNames' | 'transformer' | 'globs' | 'directives' | 'types' | 'version'> = {
7+
export const defaultOptions: Omit<Required<Options>, 'include' | 'exclude' | 'excludeNames' | 'transformer' | 'globs' | 'globsExclude' | 'directives' | 'types' | 'version'> = {
88
dirs: 'src/components',
99
extensions: 'vue',
1010
deep: true,
@@ -17,7 +17,6 @@ export const defaultOptions: Omit<Required<Options>, 'include' | 'exclude' | 'ex
1717
transformerUserResolveFunctions: true,
1818

1919
resolvers: [],
20-
globsExclude: [],
2120

2221
importPathTransform: v => v,
2322

@@ -30,7 +29,7 @@ function normalizeResolvers(resolvers: (ComponentResolver | ComponentResolver[])
3029

3130
function resolveGlobsExclude(root: string, glob: string) {
3231
const excludeReg = /^!/
33-
return `${excludeReg.test(glob) ? '!' : ''}${resolve(root, glob.replace(excludeReg, ''))}`
32+
return slash(`${excludeReg.test(glob) ? '!' : ''}${resolve(root, glob.replace(excludeReg, ''))}`)
3433
}
3534

3635
export function resolveOptions(options: Options, root: string): ResolvedOptions {
@@ -39,7 +38,8 @@ export function resolveOptions(options: Options, root: string): ResolvedOptions
3938
resolved.extensions = toArray(resolved.extensions)
4039

4140
if (resolved.globs) {
42-
resolved.globs = toArray(resolved.globs).map((glob: string) => slash(resolveGlobsExclude(root, glob)))
41+
resolved.globs = toArray(resolved.globs)
42+
.map(glob => resolveGlobsExclude(root, glob))
4343
resolved.resolvedDirs = []
4444
}
4545
else {
@@ -48,17 +48,38 @@ export function resolveOptions(options: Options, root: string): ResolvedOptions
4848
: `{${resolved.extensions.join(',')}}`
4949

5050
resolved.dirs = toArray(resolved.dirs)
51-
resolved.resolvedDirs = resolved.dirs.map(i => slash(resolveGlobsExclude(root, i)))
5251

53-
resolved.globs = resolved.resolvedDirs.map(i => resolved.deep
54-
? slash(join(i, `**/*.${extsGlob}`))
55-
: slash(join(i, `*.${extsGlob}`)),
56-
)
52+
const globs = resolved.dirs.map(i => resolveGlobsExclude(root, i))
53+
54+
resolved.resolvedDirs = globs.filter(i => !i.startsWith('!'))
55+
resolved.globs = globs.map((i) => {
56+
let prefix = ''
57+
if (i.startsWith('!')) {
58+
prefix = '!'
59+
i = i.slice(1)
60+
}
61+
return resolved.deep
62+
? prefix + slash(join(i, `**/*.${extsGlob}`))
63+
: prefix + slash(join(i, `*.${extsGlob}`))
64+
})
5765

5866
if (!resolved.extensions.length)
5967
throw new Error('[unplugin-vue-components] `extensions` option is required to search for components')
6068
}
6169

70+
if (!resolved.globsExclude)
71+
resolved.globsExclude = [`**/node_modules/**`]
72+
resolved.globsExclude = toArray(resolved.globsExclude || [])
73+
.map(i => resolveGlobsExclude(root, i))
74+
75+
// Move negated globs to globsExclude
76+
resolved.globs = resolved.globs.filter((i) => {
77+
if (!i.startsWith('!'))
78+
return true
79+
resolved.globsExclude.push(i.slice(1))
80+
return false
81+
})
82+
6283
resolved.dts = !resolved.dts
6384
? false
6485
: resolve(

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export interface Options {
9898
/**
9999
* Negated glob patterns to exclude files from being detected as components.
100100
*
101-
* @default []
101+
* @default ['<root>/**\/node_modules/**']
102102
*/
103103
globsExclude?: string | string[]
104104

@@ -209,6 +209,7 @@ export type ResolvedOptions = Omit<
209209
dirs: string[]
210210
resolvedDirs: string[]
211211
globs: string[]
212+
globsExclude: string[]
212213
dts: string | false
213214
root: string
214215
}

test/search.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { relative, resolve } from 'pathe'
2-
import { describe, expect, it } from 'vitest'
2+
import { describe, expect, it, onTestFailed } from 'vitest'
33
import { Context } from '../src/core/context'
44

55
const root = resolve(__dirname, '../examples/vite-vue3')
@@ -64,10 +64,18 @@ describe('search', () => {
6464
'!src/components/book',
6565
],
6666
})
67+
68+
onTestFailed(() => {
69+
console.error('resolved options')
70+
console.error(ctx.options)
71+
})
72+
6773
ctx.setRoot(root)
6874
ctx.searchGlob()
6975

70-
expect(cleanup(ctx.componentNameMap).map(i => i.as)).not.toEqual(expect.arrayContaining(['Book']))
76+
expect(cleanup(ctx.componentNameMap).map(i => i.as))
77+
.not
78+
.contain('Book')
7179
})
7280

7381
it('should excludeNames', () => {

vitest.config.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import { defineConfig } from 'vite'
22

33
export default defineConfig({
44
test: {
5-
deps: {
6-
inline: [
7-
'@babel/types',
8-
],
5+
server: {
6+
deps: {
7+
inline: [
8+
'@babel/types',
9+
],
10+
},
911
},
1012
},
1113
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy