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

fix: fix how globs resolves, excludes nested node_modules folder by default #826

Merged
merged 6 commits into from
Feb 19, 2025
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
39 changes: 30 additions & 9 deletions src/core/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { slash, toArray } from '@antfu/utils'
import { getPackageInfoSync, isPackageExists } from 'local-pkg'
import { detectTypeImports } from './type-imports/detect'

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

resolvers: [],
globsExclude: [],

importPathTransform: v => v,

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

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

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

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

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

resolved.globs = resolved.resolvedDirs.map(i => resolved.deep
? slash(join(i, `**/*.${extsGlob}`))
: slash(join(i, `*.${extsGlob}`)),
)
const globs = resolved.dirs.map(i => resolveGlobsExclude(root, i))

resolved.resolvedDirs = globs.filter(i => !i.startsWith('!'))
resolved.globs = globs.map((i) => {
let prefix = ''
if (i.startsWith('!')) {
prefix = '!'
i = i.slice(1)
}
return resolved.deep
? prefix + slash(join(i, `**/*.${extsGlob}`))
: prefix + slash(join(i, `*.${extsGlob}`))
})

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

if (!resolved.globsExclude)
resolved.globsExclude = [`**/node_modules/**`]
resolved.globsExclude = toArray(resolved.globsExclude || [])
.map(i => resolveGlobsExclude(root, i))

// Move negated globs to globsExclude
resolved.globs = resolved.globs.filter((i) => {
if (!i.startsWith('!'))
return true
resolved.globsExclude.push(i.slice(1))
return false
})

resolved.dts = !resolved.dts
? false
: resolve(
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export interface Options {
/**
* Negated glob patterns to exclude files from being detected as components.
*
* @default []
* @default ['<root>/**\/node_modules/**']
*/
globsExclude?: string | string[]

Expand Down Expand Up @@ -209,6 +209,7 @@ export type ResolvedOptions = Omit<
dirs: string[]
resolvedDirs: string[]
globs: string[]
globsExclude: string[]
dts: string | false
root: string
}
Expand Down
12 changes: 10 additions & 2 deletions test/search.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { relative, resolve } from 'pathe'
import { describe, expect, it } from 'vitest'
import { describe, expect, it, onTestFailed } from 'vitest'
import { Context } from '../src/core/context'

const root = resolve(__dirname, '../examples/vite-vue3')
Expand Down Expand Up @@ -64,10 +64,18 @@ describe('search', () => {
'!src/components/book',
],
})

onTestFailed(() => {
console.error('resolved options')
console.error(ctx.options)
})

ctx.setRoot(root)
ctx.searchGlob()

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

it('should excludeNames', () => {
Expand Down
10 changes: 6 additions & 4 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { defineConfig } from 'vite'

export default defineConfig({
test: {
deps: {
inline: [
'@babel/types',
],
server: {
deps: {
inline: [
'@babel/types',
],
},
},
},
})