From 754ff0c9e7eaf3914090fbe51691b59250571eb4 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 28 Feb 2022 16:11:46 +0000 Subject: [PATCH] feat: `.nuxtignore` support and `isIgnored` kit utility (#3424) --- packages/kit/package.json | 1 + packages/kit/src/ignore.ts | 27 +++++++++++++++++++++ packages/kit/src/index.ts | 1 + packages/kit/src/resolve.ts | 3 ++- packages/nuxi/src/commands/dev.ts | 2 +- packages/nuxt3/package.json | 1 - packages/nuxt3/src/components/module.ts | 3 --- packages/nuxt3/src/components/scan.ts | 3 ++- packages/nuxt3/src/core/builder.ts | 4 +-- packages/nuxt3/test/scan-components.test.ts | 6 ++++- packages/schema/build.config.ts | 1 + packages/schema/src/config/_common.ts | 10 +++++--- packages/schema/src/types/nuxt.ts | 2 ++ packages/vite/src/vite.ts | 5 +++- yarn.lock | 2 +- 15 files changed, 56 insertions(+), 15 deletions(-) create mode 100644 packages/kit/src/ignore.ts diff --git a/packages/kit/package.json b/packages/kit/package.json index cad636d139..edc5770de0 100644 --- a/packages/kit/package.json +++ b/packages/kit/package.json @@ -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", diff --git a/packages/kit/src/ignore.ts b/packages/kit/src/ignore.ts new file mode 100644 index 0000000000..2b348d5d02 --- /dev/null +++ b/packages/kit/src/ignore.ts @@ -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) +} diff --git a/packages/kit/src/index.ts b/packages/kit/src/index.ts index 1293b654d9..b283f63f3c 100644 --- a/packages/kit/src/index.ts +++ b/packages/kit/src/index.ts @@ -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' diff --git a/packages/kit/src/resolve.ts b/packages/kit/src/resolve.ts index 3c810fc977..06af993c4d 100644 --- a/packages/kit/src/resolve.ts +++ b/packages/kit/src/resolve.ts @@ -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)) } diff --git a/packages/nuxi/src/commands/dev.ts b/packages/nuxi/src/commands/dev.ts index ce71d7ef15..e08bb5d685 100644 --- a/packages/nuxi/src/commands/dev.ts +++ b/packages/nuxi/src/commands/dev.ts @@ -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`) } diff --git a/packages/nuxt3/package.json b/packages/nuxt3/package.json index 601c824ae7..e95363c716 100644 --- a/packages/nuxt3/package.json +++ b/packages/nuxt3/package.json @@ -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", diff --git a/packages/nuxt3/src/components/module.ts b/packages/nuxt3/src/components/module.ts index 4034f08673..7a01452cee 100644 --- a/packages/nuxt3/src/components/module.ts +++ b/packages/nuxt3/src/components/module.ts @@ -85,11 +85,8 @@ export default defineNuxtModule({ 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) diff --git a/packages/nuxt3/src/components/scan.ts b/packages/nuxt3/src/components/scan.ts index 1cca1fc5f7..8b283e7120 100644 --- a/packages/nuxt3/src/components/scan.ts +++ b/packages/nuxt3/src/components/scan.ts @@ -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 } diff --git a/packages/nuxt3/src/core/builder.ts b/packages/nuxt3/src/core/builder.ts index 75d5aa9674..70c25b86fc 100644 --- a/packages/nuxt3/src/core/builder.ts +++ b/packages/nuxt3/src/core/builder.ts @@ -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' ] }) diff --git a/packages/nuxt3/test/scan-components.test.ts b/packages/nuxt3/test/scan-components.test.ts index 8a1b42c427..f7401e964b 100644 --- a/packages/nuxt3/test/scan-components.test.ts +++ b/packages/nuxt3/test/scan-components.test.ts @@ -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'), diff --git a/packages/schema/build.config.ts b/packages/schema/build.config.ts index 9a6b53d591..dcffdb11ef 100644 --- a/packages/schema/build.config.ts +++ b/packages/schema/build.config.ts @@ -34,6 +34,7 @@ export default defineBuildConfig({ 'webpack-hot-middleware', 'postcss', 'consola', + 'ignore', // Implicit '@vue/compiler-core', '@vue/shared' diff --git a/packages/schema/src/config/_common.ts b/packages/schema/src/config/_common.ts index 96d91836aa..36d682b004 100644 --- a/packages/schema/src/config/_common.ts +++ b/packages/schema/src/config/_common.ts @@ -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) }, diff --git a/packages/schema/src/types/nuxt.ts b/packages/schema/src/types/nuxt.ts index 93f287c8ab..eeee4e27ca 100644 --- a/packages/schema/src/types/nuxt.ts +++ b/packages/schema/src/types/nuxt.ts @@ -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 diff --git a/packages/vite/src/vite.ts b/packages/vite/src/vite.ts index b37f988e67..2f730c1154 100644 --- a/packages/vite/src/vite.ts +++ b/packages/vite/src/vite.ts @@ -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 diff --git a/yarn.lock b/yarn.lock index 1ff00ea7a5..ac6efe158f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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