feat(kit,nuxt): respect nuxt ignore patterns in nitro (#22808)

This commit is contained in:
Daniel Roe 2023-08-25 13:08:38 +01:00 committed by GitHub
parent a70904fd8b
commit 54a6eab406
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 45 additions and 11 deletions

View File

@ -1,6 +1,6 @@
import { existsSync, readFileSync } from 'node:fs'
import ignore from 'ignore'
import { join, relative } from 'pathe'
import { join, relative, resolve } from 'pathe'
import { tryUseNuxt } from './context'
/**
@ -16,14 +16,7 @@ export function isIgnored (pathname: string): boolean {
if (!nuxt._ignore) {
nuxt._ignore = ignore(nuxt.options.ignoreOptions)
const resolvedIgnore = nuxt.options.ignore.flatMap(s => resolveGroupSyntax(s))
nuxt._ignore.add(resolvedIgnore)
const nuxtignoreFile = join(nuxt.options.rootDir, '.nuxtignore')
if (existsSync(nuxtignoreFile)) {
nuxt._ignore.add(readFileSync(nuxtignoreFile, 'utf-8'))
}
nuxt._ignore.add(resolveIgnorePatterns())
}
const cwds = nuxt.options._layers?.map(layer => layer.cwd).sort((a, b) => b.length - a.length)
@ -35,6 +28,31 @@ export function isIgnored (pathname: string): boolean {
return !!(relativePath && nuxt._ignore.ignores(relativePath))
}
export function resolveIgnorePatterns (relativePath?: string): string[] {
const nuxt = tryUseNuxt()
// Happens with CLI reloads
if (!nuxt) {
return []
}
if (!nuxt._ignorePatterns) {
nuxt._ignorePatterns = nuxt.options.ignore.flatMap(s => resolveGroupSyntax(s))
const nuxtignoreFile = join(nuxt.options.rootDir, '.nuxtignore')
if (existsSync(nuxtignoreFile)) {
const contents = readFileSync(nuxtignoreFile, 'utf-8')
nuxt._ignorePatterns.push(...contents.trim().split(/\r?\n/))
}
}
if (relativePath) {
return nuxt._ignorePatterns.map(p => p.startsWith('*') || p.startsWith('!*') ? p : relative(relativePath, resolve(nuxt.options.rootDir, p)))
}
return nuxt._ignorePatterns
}
/**
* 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:

View File

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

View File

@ -3,7 +3,7 @@ import { cpus } from 'node:os'
import { join, relative, resolve } from 'pathe'
import { build, copyPublicAssets, createDevServer, createNitro, prepare, prerender, scanHandlers, writeTypes } from 'nitropack'
import type { Nitro, NitroConfig } from 'nitropack'
import { logger } from '@nuxt/kit'
import { logger, resolveIgnorePatterns } from '@nuxt/kit'
import escapeRE from 'escape-string-regexp'
import { defu } from 'defu'
import fsExtra from 'fs-extra'
@ -207,6 +207,7 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
// Resolve user-provided paths
nitroConfig.srcDir = resolve(nuxt.options.rootDir, nuxt.options.srcDir, nitroConfig.srcDir!)
nitroConfig.ignore = [...(nitroConfig.ignore || []), ...resolveIgnorePatterns(nitroConfig.srcDir)]
// Add fallback server for `ssr: false`
if (!nuxt.options.ssr) {

View File

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

View File

@ -894,6 +894,14 @@ describe('ignore list', () => {
const html = await $fetch('/ignore/composables')
expect(html).toContain('was import ignored: true')
})
it('should ignore scanned nitro handlers in .nuxtignore', async () => {
const html = await $fetch('/ignore/scanned')
expect(html).not.toContain('this should be ignored')
})
it.skipIf(isDev())('should ignore public assets in .nuxtignore', async () => {
const html = await $fetch('/ignore/public-asset')
expect(html).not.toContain('this should be ignored')
})
})
describe('server tree shaking', () => {

View File

@ -1 +1,3 @@
composables/ignored.*
**/ignore/public-asset
server/routes/ignore/scanned.ts

View File

@ -0,0 +1 @@
this should be ignored

View File

@ -0,0 +1,3 @@
export default defineEventHandler(() => {
return 'this should be ignored'
})