fix(nuxt): re-initialise nuxt._ignore after all modules run (#26680)

This commit is contained in:
Inesh Bose 2024-04-19 09:48:19 +01:00 committed by GitHub
parent d25e6eeede
commit 00a47be49a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 31 additions and 13 deletions

View File

@ -1,5 +1,17 @@
import { describe, expect, it } from 'vitest' import { describe, expect, it, vi } from 'vitest'
import { resolveGroupSyntax } from './ignore.js' import type { Nuxt, NuxtOptions } from '@nuxt/schema'
import { isIgnored, resolveGroupSyntax, resolveIgnorePatterns } from './ignore.js'
import * as context from './context.js'
describe('isIgnored', () => {
it('should populate _ignore', () => {
const mockNuxt = { options: { ignore: ['my-dir'] } as NuxtOptions } as Nuxt
vi.spyOn(context, 'tryUseNuxt').mockReturnValue(mockNuxt)
expect(isIgnored('my-dir/my-file.ts')).toBe(true)
expect(resolveIgnorePatterns()?.includes('my-dir')).toBe(true)
})
})
describe('resolveGroupSyntax', () => { describe('resolveGroupSyntax', () => {
it('should resolve single group syntax', () => { it('should resolve single group syntax', () => {

View File

@ -38,19 +38,17 @@ export function resolveIgnorePatterns (relativePath?: string): string[] {
return [] return []
} }
if (!nuxt._ignorePatterns) { const ignorePatterns = nuxt.options.ignore.flatMap(s => resolveGroupSyntax(s))
nuxt._ignorePatterns = nuxt.options.ignore.flatMap(s => resolveGroupSyntax(s))
const nuxtignoreFile = join(nuxt.options.rootDir, '.nuxtignore') const nuxtignoreFile = join(nuxt.options.rootDir, '.nuxtignore')
if (existsSync(nuxtignoreFile)) { if (existsSync(nuxtignoreFile)) {
const contents = readFileSync(nuxtignoreFile, 'utf-8') const contents = readFileSync(nuxtignoreFile, 'utf-8')
nuxt._ignorePatterns.push(...contents.trim().split(/\r?\n/)) ignorePatterns.push(...contents.trim().split(/\r?\n/))
}
} }
if (relativePath) { if (relativePath) {
// Map ignore patterns based on if they start with * or !* // Map ignore patterns based on if they start with * or !*
return nuxt._ignorePatterns.map((p) => { return ignorePatterns.map((p) => {
const [_, negation = '', pattern] = p.match(NEGATION_RE) || [] const [_, negation = '', pattern] = p.match(NEGATION_RE) || []
if (pattern[0] === '*') { if (pattern[0] === '*') {
return p return p
@ -59,7 +57,7 @@ export function resolveIgnorePatterns (relativePath?: string): string[] {
}) })
} }
return nuxt._ignorePatterns return ignorePatterns
} }
/** /**

View File

@ -84,6 +84,7 @@
"globby": "^14.0.1", "globby": "^14.0.1",
"h3": "^1.11.1", "h3": "^1.11.1",
"hookable": "^5.5.3", "hookable": "^5.5.3",
"ignore": "^5.3.1",
"jiti": "^1.21.0", "jiti": "^1.21.0",
"klona": "^2.0.6", "klona": "^2.0.6",
"knitwork": "^1.1.0", "knitwork": "^1.1.0",

View File

@ -1,7 +1,8 @@
import { dirname, join, normalize, relative, resolve } from 'pathe' import { dirname, join, normalize, relative, resolve } from 'pathe'
import { createDebugger, createHooks } from 'hookable' import { createDebugger, createHooks } from 'hookable'
import ignore from 'ignore'
import type { LoadNuxtOptions } from '@nuxt/kit' import type { LoadNuxtOptions } from '@nuxt/kit'
import { addBuildPlugin, addComponent, addPlugin, addRouteMiddleware, addServerPlugin, addVitePlugin, addWebpackPlugin, installModule, loadNuxtConfig, logger, nuxtCtx, resolveAlias, resolveFiles, resolvePath, tryResolveModule, useNitro } from '@nuxt/kit' import { addBuildPlugin, addComponent, addPlugin, addRouteMiddleware, addServerPlugin, addVitePlugin, addWebpackPlugin, installModule, loadNuxtConfig, logger, nuxtCtx, resolveAlias, resolveFiles, resolveIgnorePatterns, resolvePath, tryResolveModule, useNitro } from '@nuxt/kit'
import { resolvePath as _resolvePath } from 'mlly' import { resolvePath as _resolvePath } from 'mlly'
import type { Nuxt, NuxtHooks, NuxtOptions } from 'nuxt/schema' import type { Nuxt, NuxtHooks, NuxtOptions } from 'nuxt/schema'
import type { PackageJson } from 'pkg-types' import type { PackageJson } from 'pkg-types'
@ -453,6 +454,10 @@ async function initNuxt (nuxt: Nuxt) {
} }
} }
// (Re)initialise ignore handler with resolved ignores from modules
nuxt._ignore = ignore(nuxt.options.ignoreOptions)
nuxt._ignore.add(resolveIgnorePatterns())
await nuxt.callHook('modules:done') await nuxt.callHook('modules:done')
if (nuxt.options.experimental.appManifest) { if (nuxt.options.experimental.appManifest) {

View File

@ -75,7 +75,6 @@ export interface Nuxt {
// Private fields. // Private fields.
_version: string _version: string
_ignore?: Ignore _ignore?: Ignore
_ignorePatterns?: string[]
/** The resolved Nuxt configuration. */ /** The resolved Nuxt configuration. */
options: NuxtOptions options: NuxtOptions

View File

@ -317,6 +317,9 @@ importers:
hookable: hookable:
specifier: ^5.5.3 specifier: ^5.5.3
version: 5.5.3 version: 5.5.3
ignore:
specifier: ^5.3.1
version: 5.3.1
jiti: jiti:
specifier: ^1.21.0 specifier: ^1.21.0
version: 1.21.0 version: 1.21.0