fix(kit): resolve group syntax of `ignore` (#15884)

This commit is contained in:
Alexander Lichter 2023-01-20 17:23:16 +01:00 committed by GitHub
parent 7c1712013c
commit fa2672485a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 3 deletions

View File

@ -0,0 +1,26 @@
import { expect, it, describe } from 'vitest'
import { resolveGroupSyntax } from './ignore.js'
describe('resolveGroupSyntax', () => {
it('should resolve single group syntax', () => {
expect(resolveGroupSyntax('**/*.{spec}.{js,ts}')).toStrictEqual([
'**/*.spec.js',
'**/*.spec.ts'
])
})
it('should resolve multi-group syntax', () => {
expect(resolveGroupSyntax('**/*.{spec,test}.{js,ts}')).toStrictEqual([
'**/*.spec.js',
'**/*.spec.ts',
'**/*.test.js',
'**/*.test.ts'
])
})
it('should do nothing with normal globs', () => {
expect(resolveGroupSyntax('**/*.spec.js')).toStrictEqual([
'**/*.spec.js'
])
})
})

View File

@ -16,7 +16,9 @@ export function isIgnored (pathname: string): boolean {
if (!nuxt._ignore) {
nuxt._ignore = ignore(nuxt.options.ignoreOptions)
nuxt._ignore.add(nuxt.options.ignore)
const resolvedIgnore = nuxt.options.ignore.flatMap(s => resolveGroupSyntax(s))
nuxt._ignore.add(resolvedIgnore)
const nuxtignoreFile = join(nuxt.options.rootDir, '.nuxtignore')
if (existsSync(nuxtignoreFile)) {
@ -32,3 +34,27 @@ export function isIgnored (pathname: string): boolean {
}
return !!(relativePath && nuxt._ignore.ignores(relativePath))
}
/**
* This function turns string containing groups '**\/*.{spec,test}.{js,ts}' into an array of strings.
* For example will '**\/*.{spec,test}.{js,ts}' be resolved to:
* ['**\/*.spec.js', '**\/*.spec.ts', '**\/*.test.js', '**\/*.test.ts']
*
* @param group string containing the group syntax
* @returns {string[]} array of strings without the group syntax
*/
export function resolveGroupSyntax (group: string): string[] {
let groups = [group]
while (groups.some(group => group.includes('{'))) {
groups = groups.flatMap((group) => {
const [head, ...tail] = group.split('{')
if (tail.length) {
const [body, ...rest] = tail.join('{').split('}')
return body.split(',').map(part => `${head}${part}${rest.join('')}`)
}
return group
})
}
return groups
}

View File

@ -12,7 +12,7 @@ export * from './build'
export * from './compatibility'
export * from './components'
export * from './context'
export * from './ignore'
export { isIgnored } from './ignore'
export * from './layout'
export * from './pages'
export * from './plugin'

View File

@ -0,0 +1 @@
throw new Error('this-should-not-load.spec.js should not be loaded')

View File

@ -1,5 +1,6 @@
import { resolve } from 'node:path'
import { defineConfig } from 'vite'
import { configDefaults } from 'vitest/config'
import { isWindows } from 'std-env'
export default defineConfig({
@ -13,6 +14,8 @@ export default defineConfig({
tsconfigRaw: '{}'
},
test: {
testTimeout: isWindows ? 60000 : 10000
testTimeout: isWindows ? 60000 : 10000,
// Excluded plugin because it should throw an error when accidentally loaded via Nuxt
exclude: [...configDefaults.exclude, '**/this-should-not-load.spec.js']
}
})