From fb27f3f37d7977409dfb7722cc428a0d4bc822ac Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Tue, 14 Jan 2025 20:43:35 +0800 Subject: [PATCH] feat: rename debug keys --- packages/kit/src/module/define.ts | 2 +- .../app/plugins/{debug.ts => debug-hooks.ts} | 2 +- packages/nuxt/src/core/app.ts | 2 +- packages/nuxt/src/core/builder.ts | 8 ++++---- packages/nuxt/src/core/nitro.ts | 2 +- packages/nuxt/src/core/nuxt.ts | 16 ++++++++++++---- packages/nuxt/src/pages/module.ts | 2 +- packages/schema/src/config/common.ts | 18 +++++++++++++++--- packages/schema/src/config/vite.ts | 6 ++++-- packages/schema/src/index.ts | 1 + packages/schema/src/types/debug.ts | 14 ++++++++++++++ packages/webpack/src/presets/vue.ts | 2 +- 12 files changed, 56 insertions(+), 19 deletions(-) rename packages/nuxt/src/app/plugins/{debug.ts => debug-hooks.ts} (88%) create mode 100644 packages/schema/src/types/debug.ts diff --git a/packages/kit/src/module/define.ts b/packages/kit/src/module/define.ts index 5414481338..bbefdb4441 100644 --- a/packages/kit/src/module/define.ts +++ b/packages/kit/src/module/define.ts @@ -120,7 +120,7 @@ function _defineNuxtModule< // Measure setup time if (setupTime > 5000 && uniqueKey !== '@nuxt/telemetry') { logger.warn(`Slow module \`${uniqueKey || ''}\` took \`${setupTime}ms\` to setup.`) - } else if (nuxt.options.debug && nuxt.options.debug.log) { + } else if (nuxt.options.debug && nuxt.options.debug.modules) { logger.info(`Module \`${uniqueKey || ''}\` took \`${setupTime}ms\` to setup.`) } diff --git a/packages/nuxt/src/app/plugins/debug.ts b/packages/nuxt/src/app/plugins/debug-hooks.ts similarity index 88% rename from packages/nuxt/src/app/plugins/debug.ts rename to packages/nuxt/src/app/plugins/debug-hooks.ts index ccb841672c..d56d99735e 100644 --- a/packages/nuxt/src/app/plugins/debug.ts +++ b/packages/nuxt/src/app/plugins/debug-hooks.ts @@ -2,7 +2,7 @@ import { createDebugger } from 'hookable' import { defineNuxtPlugin } from '../nuxt' export default defineNuxtPlugin({ - name: 'nuxt:debug', + name: 'nuxt:debug:hooks', enforce: 'pre', setup (nuxtApp) { createDebugger(nuxtApp.hooks, { tag: 'nuxt-app' }) diff --git a/packages/nuxt/src/core/app.ts b/packages/nuxt/src/core/app.ts index 172738b2fd..84706cf0d6 100644 --- a/packages/nuxt/src/core/app.ts +++ b/packages/nuxt/src/core/app.ts @@ -87,7 +87,7 @@ export async function generateApp (nuxt: Nuxt, app: NuxtApp, options: { filter?: const perf = performance.now() - start const setupTime = Math.round((perf * 100)) / 100 - if ((nuxt.options.debug && nuxt.options.debug.log) || setupTime > 500) { + if ((nuxt.options.debug && nuxt.options.debug.templates) || setupTime > 500) { logger.info(`Compiled \`${template.filename}\` in ${setupTime}ms`) } diff --git a/packages/nuxt/src/core/builder.ts b/packages/nuxt/src/core/builder.ts index 8a552b91cb..77922e5084 100644 --- a/packages/nuxt/src/core/builder.ts +++ b/packages/nuxt/src/core/builder.ts @@ -119,7 +119,7 @@ function createWatcher () { function createGranularWatcher () { const nuxt = useNuxt() - if (nuxt.options.debug && nuxt.options.debug.log) { + if (nuxt.options.debug && nuxt.options.debug.watchers) { // eslint-disable-next-line no-console console.time('[nuxt] builder:chokidar:watch') } @@ -164,7 +164,7 @@ function createGranularWatcher () { }) watcher.on('ready', () => { pending-- - if (nuxt.options.debug && nuxt.options.debug.log && !pending) { + if (nuxt.options.debug && nuxt.options.debug.watchers && !pending) { // eslint-disable-next-line no-console console.timeEnd('[nuxt] builder:chokidar:watch') } @@ -175,7 +175,7 @@ function createGranularWatcher () { async function createParcelWatcher () { const nuxt = useNuxt() - if (nuxt.options.debug && nuxt.options.debug.log) { + if (nuxt.options.debug && nuxt.options.debug.watchers) { // eslint-disable-next-line no-console console.time('[nuxt] builder:parcel:watch') } @@ -201,7 +201,7 @@ async function createParcelWatcher () { ], }) watcher.then((subscription) => { - if (nuxt.options.debug && nuxt.options.debug.log) { + if (nuxt.options.debug && nuxt.options.debug.watchers) { // eslint-disable-next-line no-console console.timeEnd('[nuxt] builder:parcel:watch') } diff --git a/packages/nuxt/src/core/nitro.ts b/packages/nuxt/src/core/nitro.ts index 88f416a9b8..0b4e59fdd9 100644 --- a/packages/nuxt/src/core/nitro.ts +++ b/packages/nuxt/src/core/nitro.ts @@ -52,7 +52,7 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) { const isNuxtV4 = nuxt.options.future?.compatibilityVersion === 4 const nitroConfig: NitroConfig = defu(nuxt.options.nitro, { - debug: nuxt.options.debug && nuxt.options.debug.nitro, + debug: nuxt.options.debug ? nuxt.options.debug.nitro : false, rootDir: nuxt.options.rootDir, workspaceDir: nuxt.options.workspaceDir, srcDir: nuxt.options.serverDir, diff --git a/packages/nuxt/src/core/nuxt.ts b/packages/nuxt/src/core/nuxt.ts index fa602a85d7..35410566cc 100644 --- a/packages/nuxt/src/core/nuxt.ts +++ b/packages/nuxt/src/core/nuxt.ts @@ -551,9 +551,13 @@ async function initNuxt (nuxt: Nuxt) { addPlugin(resolve(nuxt.options.appDir, 'plugins/preload.server')) } - // Add nuxt app debugger - if (nuxt.options.debug && nuxt.options.debug.browser) { - addPlugin(resolve(nuxt.options.appDir, 'plugins/debug')) + // Add nuxt app hooks debugger + if ( + nuxt.options.debug + && nuxt.options.debug.hooks + && (nuxt.options.debug.hooks === true || nuxt.options.debug.hooks.client) + ) { + addPlugin(resolve(nuxt.options.appDir, 'plugins/debug-hooks')) } // Add experimental Chrome devtools timings support @@ -811,7 +815,11 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise { nuxt.hooks.addHooks(opts.overrides.hooks) } - if (nuxt.options.debug && nuxt.options.debug.hooks) { + if ( + nuxt.options.debug + && nuxt.options.debug.hooks + && (nuxt.options.debug.hooks === true || nuxt.options.debug.hooks.server) + ) { createDebugger(nuxt.hooks, { tag: 'nuxt' }) } diff --git a/packages/nuxt/src/pages/module.ts b/packages/nuxt/src/pages/module.ts index 1f3c0007f0..55fd8fa963 100644 --- a/packages/nuxt/src/pages/module.ts +++ b/packages/nuxt/src/pages/module.ts @@ -176,7 +176,7 @@ export default defineNuxtModule({ const options: TypedRouterOptions = { routesFolder: [], dts: resolve(nuxt.options.buildDir, declarationFile), - logs: nuxt.options.debug && nuxt.options.debug.log, + logs: nuxt.options.debug && nuxt.options.debug.router, async beforeWriteFiles (rootPage) { rootPage.children.forEach(child => child.delete()) const pages = nuxt.apps.default?.pages || await resolvePagesRoutes(nuxt) diff --git a/packages/schema/src/config/common.ts b/packages/schema/src/config/common.ts index 3e12c01eae..ff464939eb 100644 --- a/packages/schema/src/config/common.ts +++ b/packages/schema/src/config/common.ts @@ -8,6 +8,7 @@ import { defu } from 'defu' import { findWorkspaceDir } from 'pkg-types' import type { RuntimeConfig } from '../types/config' +import type { NuxtDebugOptions } from '../types/debug' export default defineUntypedSchema({ /** @@ -271,16 +272,27 @@ export default defineUntypedSchema({ * } * ``` * - * @type {boolean | { log?: boolean, hooks?: boolean, nitro?: boolean, browser?: boolean, prod?: boolean }} + * @type {boolean | (typeof import('../src/types/debug').NuxtDebugOptions) | undefined} */ debug: { - $resolve: (val: boolean | { log?: boolean, hooks?: boolean, nitro?: boolean, browser?: boolean, prod?: boolean } | undefined) => { + $resolve: (val: boolean | NuxtDebugOptions | undefined) => { val ??= isDebug if (val === false) { return val } if (val === true) { - return { log: true, hooks: true, nitro: true, browser: true, prod: true } + return { + templates: true, + modules: true, + watchers: true, + hooks: { + client: true, + server: true, + }, + nitro: true, + router: true, + hydration: true, + } satisfies Required } return val }, diff --git a/packages/schema/src/config/vite.ts b/packages/schema/src/config/vite.ts index df971eefef..9d49537a12 100644 --- a/packages/schema/src/config/vite.ts +++ b/packages/schema/src/config/vite.ts @@ -2,6 +2,7 @@ import { consola } from 'consola' import { resolve } from 'pathe' import { isTest } from 'std-env' import { defineUntypedSchema } from 'untyped' +import type { NuxtDebugOptions } from '../types/debug' export default defineUntypedSchema({ /** @@ -20,9 +21,10 @@ export default defineUntypedSchema({ }, define: { $resolve: async (val: Record | undefined, get) => { - const [isDev, isDebug] = await Promise.all([get('dev'), get('debug')]) as [boolean, boolean] + const [isDev, debug] = await Promise.all([get('dev'), get('debug')]) as [boolean, boolean | NuxtDebugOptions] + return { - '__VUE_PROD_HYDRATION_MISMATCH_DETAILS__': isDebug, + '__VUE_PROD_HYDRATION_MISMATCH_DETAILS__': Boolean(debug && (debug === true || debug.hydration)), 'process.dev': isDev, 'import.meta.dev': isDev, 'process.test': isTest, diff --git a/packages/schema/src/index.ts b/packages/schema/src/index.ts index a682bb2f61..8ecabf766a 100644 --- a/packages/schema/src/index.ts +++ b/packages/schema/src/index.ts @@ -8,6 +8,7 @@ export type { AppHeadMetaObject, MetaObject, MetaObjectRaw, HeadAugmentations } export type { ModuleDefinition, ModuleMeta, ModuleOptions, ModuleSetupInstallResult, ModuleSetupReturn, NuxtModule, ResolvedModuleOptions } from './types/module' export type { Nuxt, NuxtApp, NuxtPlugin, NuxtPluginTemplate, NuxtTemplate, NuxtTypeTemplate, NuxtServerTemplate, ResolvedNuxtTemplate } from './types/nuxt' export type { RouterConfig, RouterConfigSerializable, RouterOptions } from './types/router' +export type { NuxtDebugOptions } from './types/debug' // Schema export { default as NuxtConfigSchema } from './config/index' diff --git a/packages/schema/src/types/debug.ts b/packages/schema/src/types/debug.ts new file mode 100644 index 0000000000..d21a6c3905 --- /dev/null +++ b/packages/schema/src/types/debug.ts @@ -0,0 +1,14 @@ +import type { NitroOptions } from 'nitro/types' + +export interface NuxtDebugOptions { + templates?: boolean + modules?: boolean + watchers?: boolean + nitro?: NitroOptions['debug'] + hydration?: boolean + router?: boolean + hooks?: boolean | { + server?: boolean + client?: boolean + } +} diff --git a/packages/webpack/src/presets/vue.ts b/packages/webpack/src/presets/vue.ts index 05cf6b22ea..915acc51ad 100644 --- a/packages/webpack/src/presets/vue.ts +++ b/packages/webpack/src/presets/vue.ts @@ -33,6 +33,6 @@ export function vue (ctx: WebpackConfigContext) { ctx.config.plugins!.push(new webpack.DefinePlugin({ '__VUE_OPTIONS_API__': 'true', '__VUE_PROD_DEVTOOLS__': 'false', - '__VUE_PROD_HYDRATION_MISMATCH_DETAILS__': ctx.nuxt.options.debug && ctx.nuxt.options.debug.prod, + '__VUE_PROD_HYDRATION_MISMATCH_DETAILS__': ctx.nuxt.options.debug && ctx.nuxt.options.debug.hydration, })) }