feat: .nuxtignore support and isIgnored kit utility (#3424)

This commit is contained in:
Daniel Roe 2022-02-28 16:11:46 +00:00 committed by GitHub
parent d6102de08f
commit 754ff0c9e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 56 additions and 15 deletions

View File

@ -19,6 +19,7 @@
"defu": "^5.0.1",
"globby": "^13.1.1",
"hash-sum": "^2.0.0",
"ignore": "^5.2.0",
"jiti": "^1.13.0",
"knitwork": "^0.1.0",
"lodash.template": "^4.5.0",

View File

@ -0,0 +1,27 @@
import { existsSync, readFileSync } from 'fs'
import ignore from 'ignore'
import { join, relative } from 'pathe'
import { useNuxt } from './context'
/**
* Return a filter function to filter an array of paths
*/
export function isIgnored (pathname: string): boolean {
const nuxt = useNuxt()
if (!nuxt._ignore) {
nuxt._ignore = ignore(nuxt.options.ignoreOptions)
nuxt._ignore.add(nuxt.options.ignore)
const nuxtignoreFile = join(nuxt.options.rootDir, '.nuxtignore')
if (existsSync(nuxtignoreFile)) {
nuxt._ignore.add(readFileSync(nuxtignoreFile, 'utf-8'))
}
}
const relativePath = relative(nuxt.options.rootDir, pathname)
if (relativePath.startsWith('..')) {
return false
}
return relativePath && nuxt._ignore.ignores(relativePath)
}

View File

@ -13,6 +13,7 @@ export * from './build'
export * from './compatibility'
export * from './components'
export * from './context'
export * from './ignore'
export * from './pages'
export * from './plugin'
export * from './resolve'

View File

@ -4,6 +4,7 @@ import { basename, dirname, resolve, join, normalize, isAbsolute } from 'pathe'
import { globby } from 'globby'
import { useNuxt } from './context'
import { tryResolveModule } from './internal/cjs'
import { isIgnored } from './ignore'
export interface ResolvePathOptions {
/** Base for resolving paths from. Default is Nuxt rootDir. */
@ -141,5 +142,5 @@ async function existsSensitive (path: string) {
export async function resolveFiles (path: string, pattern: string | string[]) {
const files = await globby(pattern, { cwd: path, followSymbolicLinks: true })
return files.map(p => resolve(path, p))
return files.filter(p => !isIgnored(p)).map(p => resolve(path, p))
}

View File

@ -72,7 +72,7 @@ export default defineNuxtCommand({
watcher.on('all', (event, file) => {
if (!currentNuxt) { return }
if (file.startsWith(currentNuxt.options.buildDir)) { return }
if (file.match(/nuxt\.config\.(js|ts|mjs|cjs)$/)) {
if (file.match(/(nuxt\.config\.(js|ts|mjs|cjs)|\.nuxtignore)$/)) {
dLoad(true, `${relative(rootDir, file)} updated`)
}

View File

@ -48,7 +48,6 @@
"h3": "^0.3.9",
"hash-sum": "^2.0.0",
"hookable": "^5.1.1",
"ignore": "^5.2.0",
"knitwork": "^0.1.0",
"mlly": "^0.4.3",
"murmurhash-es": "^0.1.1",

View File

@ -85,11 +85,8 @@ export default defineNuxtModule<ComponentsOptions>({
extensions,
pattern: dirOptions.pattern || `**/*.{${extensions.join(',')},}`,
ignore: [
'**/*.stories.{js,ts,jsx,tsx}', // ignore storybook files
'**/*{M,.m,-m}ixin.{js,ts,jsx,tsx}', // ignore mixins
'**/*.{spec,test}.{js,ts,jsx,tsx}', // ignore tests
'**/*.d.ts', // .d.ts files
// TODO: support nuxt ignore patterns
...(dirOptions.ignore || [])
],
transpile: (transpile === 'auto' ? dirPath.includes('node_modules') : transpile)

View File

@ -2,6 +2,7 @@ import { basename, extname, join, dirname, relative } from 'pathe'
import { globby } from 'globby'
import { pascalCase, splitByCase } from 'scule'
import type { Component, ComponentsDir } from '@nuxt/schema'
import { isIgnored } from '@nuxt/kit'
// vue@2 src/shared/util.js
// TODO: update to vue3?
@ -34,7 +35,7 @@ export async function scanComponents (dirs: ComponentsDir[], srcDir: string): Pr
for (const _file of await globby(dir.pattern!, { cwd: dir.path, ignore: dir.ignore })) {
const filePath = join(dir.path, _file)
if (scannedPaths.find(d => filePath.startsWith(d))) {
if (scannedPaths.find(d => filePath.startsWith(d)) || isIgnored(filePath)) {
continue
}

View File

@ -1,6 +1,6 @@
import chokidar from 'chokidar'
import type { Nuxt } from '@nuxt/schema'
import { tryImportModule } from '@nuxt/kit'
import { isIgnored, tryImportModule } from '@nuxt/kit'
import { createApp, generateApp } from './app'
export async function build (nuxt: Nuxt) {
@ -37,8 +37,8 @@ function watch (nuxt: Nuxt) {
cwd: nuxt.options.srcDir,
ignoreInitial: true,
ignored: [
isIgnored,
'.nuxt',
'.output',
'node_modules'
]
})

View File

@ -1,11 +1,15 @@
import { resolve } from 'path'
import { ComponentsDir } from '@nuxt/schema'
import { expect, it } from 'vitest'
import { expect, it, vi } from 'vitest'
import { scanComponents } from '../src/components/scan'
const fixtureDir = resolve(__dirname, 'fixture')
const rFixture = (...p) => resolve(fixtureDir, ...p)
vi.mock('@nuxt/kit', () => ({
isIgnored: () => false
}))
const dirs: ComponentsDir[] = [
{
path: rFixture('components'),

View File

@ -34,6 +34,7 @@ export default defineBuildConfig({
'webpack-hot-middleware',
'postcss',
'consola',
'ignore',
// Implicit
'@vue/compiler-core',
'@vue/shared'

View File

@ -1,5 +1,5 @@
import { join, resolve } from 'pathe'
import { isDevelopment, isTest } from 'std-env'
import { isDevelopment } from 'std-env'
import createRequire from 'create-require'
import { pascalCase } from 'scule'
import jiti from 'jiti'
@ -570,6 +570,7 @@ export default {
* }
* ```
* @version 2
* @version 3
*/
ignoreOptions: undefined,
@ -577,6 +578,7 @@ export default {
* Any file in `pages/`, `layouts/`, `middleware/` or `store/` will be ignored during
* building if its filename starts with the prefix specified by `ignorePrefix`.
* @version 2
* @version 3
*/
ignorePrefix: '-',
@ -584,11 +586,13 @@ export default {
* More customizable than `ignorePrefix`: all files matching glob patterns specified
* inside the `ignore` array will be ignored in building.
* @version 2
* @version 3
*/
ignore: {
$resolve: (val, get) => [
'**/*.test.*',
'**/*.spec.*',
'**/*.stories.{js,ts,jsx,tsx}', // ignore storybook files
'**/*.{spec,test}.{js,ts,jsx,tsx}', // ignore tests
'.output',
get('ignorePrefix') && `**/${get('ignorePrefix')}*.*`
].concat(val).filter(Boolean)
},

View File

@ -1,10 +1,12 @@
import type { Hookable } from 'hookable'
import type { Ignore } from 'ignore'
import type { NuxtHooks } from './hooks'
import type { NuxtOptions } from './config'
export interface Nuxt {
// Private fields
_version: string
_ignore?: Ignore
/** The resolved Nuxt configuration. */
options: NuxtOptions

View File

@ -2,7 +2,7 @@ import * as vite from 'vite'
import { resolve } from 'pathe'
import type { Nuxt } from '@nuxt/schema'
import type { InlineConfig, SSROptions } from 'vite'
import { logger } from '@nuxt/kit'
import { logger, isIgnored } from '@nuxt/kit'
import type { Options } from '@vitejs/plugin-vue'
import { sanitizeFilePath } from 'mlly'
import { getPort } from 'get-port-please'
@ -64,6 +64,9 @@ export async function bundle (nuxt: Nuxt) {
virtual(nuxt.vfs)
],
server: {
watch: {
ignored: isIgnored
},
hmr: {
clientPort: hmrPort,
port: hmrPort

View File

@ -2997,6 +2997,7 @@ __metadata:
defu: ^5.0.1
globby: ^13.1.1
hash-sum: ^2.0.0
ignore: ^5.2.0
jiti: ^1.13.0
knitwork: ^0.1.0
lodash.template: ^4.5.0
@ -15695,7 +15696,6 @@ __metadata:
h3: ^0.3.9
hash-sum: ^2.0.0
hookable: ^5.1.1
ignore: ^5.2.0
knitwork: ^0.1.0
mlly: ^0.4.3
murmurhash-es: ^0.1.1