feat(nuxt): add hook debug mode (#7690)

This commit is contained in:
Daniel Roe 2022-10-15 11:56:15 +01:00 committed by GitHub
parent 329fc63a02
commit 6dcff8e428
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 32 additions and 6 deletions

View File

@ -54,7 +54,7 @@
"globby": "^13.1.2", "globby": "^13.1.2",
"h3": "^0.7.21", "h3": "^0.7.21",
"hash-sum": "^2.0.0", "hash-sum": "^2.0.0",
"hookable": "^5.4.0", "hookable": "^5.4.1",
"knitwork": "^0.1.2", "knitwork": "^0.1.2",
"magic-string": "^0.26.7", "magic-string": "^0.26.7",
"mlly": "^0.5.16", "mlly": "^0.5.16",

View File

@ -0,0 +1,6 @@
import { createDebugger } from 'hookable'
import { defineNuxtPlugin } from '#app'
export default defineNuxtPlugin((nuxtApp) => {
createDebugger(nuxtApp.hooks, { tag: 'nuxt-app' })
})

View File

@ -17,6 +17,7 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
// Resolve config // Resolve config
const _nitroConfig = ((nuxt.options as any).nitro || {}) as NitroConfig const _nitroConfig = ((nuxt.options as any).nitro || {}) as NitroConfig
const nitroConfig: NitroConfig = defu(_nitroConfig, <NitroConfig>{ const nitroConfig: NitroConfig = defu(_nitroConfig, <NitroConfig>{
debug: nuxt.options.debug,
rootDir: nuxt.options.rootDir, rootDir: nuxt.options.rootDir,
workspaceDir: nuxt.options.workspaceDir, workspaceDir: nuxt.options.workspaceDir,
srcDir: nuxt.options.serverDir, srcDir: nuxt.options.serverDir,

View File

@ -1,5 +1,5 @@
import { join, normalize, resolve } from 'pathe' import { join, normalize, resolve } from 'pathe'
import { createHooks } from 'hookable' import { createHooks, createDebugger } from 'hookable'
import type { Nuxt, NuxtOptions, NuxtConfig, ModuleContainer, NuxtHooks } from '@nuxt/schema' import type { Nuxt, NuxtOptions, NuxtConfig, ModuleContainer, NuxtHooks } from '@nuxt/schema'
import { loadNuxtConfig, LoadNuxtOptions, nuxtCtx, installModule, addComponent, addVitePlugin, addWebpackPlugin, tryResolveModule, addPlugin } from '@nuxt/kit' import { loadNuxtConfig, LoadNuxtOptions, nuxtCtx, installModule, addComponent, addVitePlugin, addWebpackPlugin, tryResolveModule, addPlugin } from '@nuxt/kit'
// Temporary until finding better placement // Temporary until finding better placement
@ -183,6 +183,11 @@ async function initNuxt (nuxt: Nuxt) {
addPlugin(resolve(nuxt.options.appDir, 'plugins/preload.server')) addPlugin(resolve(nuxt.options.appDir, 'plugins/preload.server'))
} }
// Add nuxt app debugger
if (nuxt.options.debug) {
addPlugin(resolve(nuxt.options.appDir, 'plugins/debug'))
}
for (const m of modulesToInstall) { for (const m of modulesToInstall) {
if (Array.isArray(m)) { if (Array.isArray(m)) {
await installModule(m[0], m[1]) await installModule(m[0], m[1])
@ -229,6 +234,10 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
const nuxt = createNuxt(options) const nuxt = createNuxt(options)
if (nuxt.options.debug) {
createDebugger(nuxt.hooks, { tag: 'nuxt' })
}
if (opts.ready !== false) { if (opts.ready !== false) {
await nuxt.ready() await nuxt.ready()
} }

View File

@ -1,5 +1,5 @@
import { join, resolve } from 'pathe' import { join, resolve } from 'pathe'
import { isDevelopment } from 'std-env' import { isDebug, isDevelopment } from 'std-env'
import createRequire from 'create-require' import createRequire from 'create-require'
import { pascalCase } from 'scule' import { pascalCase } from 'scule'
import jiti from 'jiti' import jiti from 'jiti'
@ -149,11 +149,14 @@ export default defineUntypedSchema({
/** /**
* Set to `true` to enable debug mode. * Set to `true` to enable debug mode.
* *
* By default, it's only enabled in development mode. * At the moment, it prints out hook names and timings on the server, and
* logs hook arguments as well in the browser.
*
* @version 2 * @version 2
* @version 3
*/ */
debug: { debug: {
$resolve: async (val, get) => val ?? await get('dev') $resolve: async (val, get) => val ?? isDebug
}, },
/** /**

View File

@ -8380,6 +8380,13 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"hookable@npm:^5.4.1":
version: 5.4.1
resolution: "hookable@npm:5.4.1"
checksum: 2a48150436bf70b4b756bcf15694809032c490c1747aba363a6de928a96712e808b0918de06c933e082cc2042c3f051b593d400df6af4773cd9ad1e85d677839
languageName: node
linkType: hard
"hosted-git-info@npm:^2.1.4": "hosted-git-info@npm:^2.1.4":
version: 2.8.9 version: 2.8.9
resolution: "hosted-git-info@npm:2.8.9" resolution: "hosted-git-info@npm:2.8.9"
@ -11029,7 +11036,7 @@ __metadata:
globby: ^13.1.2 globby: ^13.1.2
h3: ^0.7.21 h3: ^0.7.21
hash-sum: ^2.0.0 hash-sum: ^2.0.0
hookable: ^5.4.0 hookable: ^5.4.1
knitwork: ^0.1.2 knitwork: ^0.1.2
magic-string: ^0.26.7 magic-string: ^0.26.7
mlly: ^0.5.16 mlly: ^0.5.16