Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

feat(nuxt): add hook debug mode #7690

Merged
merged 10 commits into from
Oct 15, 2022
Merged
2 changes: 1 addition & 1 deletion packages/nuxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"globby": "^13.1.2",
"h3": "^0.7.21",
"hash-sum": "^2.0.0",
"hookable": "^5.3.0",
"hookable": "^5.4.0",
"knitwork": "^0.1.2",
"magic-string": "^0.26.7",
"mlly": "^0.5.16",
Expand Down
9 changes: 9 additions & 0 deletions packages/nuxt/src/app/plugins/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createDebugger } from 'hookable'
import { defineNuxtPlugin } from '#app'

export default defineNuxtPlugin((nuxtApp) => {
// @ts-expect-error remove in next version of hookable
createDebugger(nuxtApp.hooks, {
tag: 'nuxt app'
})
})
13 changes: 13 additions & 0 deletions packages/nuxt/src/core/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { resolvePath } from '@nuxt/kit'
import defu from 'defu'
import fsExtra from 'fs-extra'
import { toEventHandler, dynamicEventHandler } from 'h3'
import { createDebugger } from 'hookable'
import { distDir } from '../dirs'
import { ImportProtectionPlugin } from './plugins/import-protection'

Expand Down Expand Up @@ -129,6 +130,11 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
exclude: [/core[\\/]runtime[\\/]nitro[\\/]renderer/]
}))

if (nuxt.options.debug) {
nitroConfig.plugins = nitroConfig.plugins || []
nitroConfig.plugins.push(resolve(distDir, 'core/runtime/nitro/plugins/debug'))
}

// Extend nitro config with hook
await nuxt.callHook('nitro:config', nitroConfig)

Expand All @@ -144,6 +150,13 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {

// Connect hooks
nuxt.hook('close', () => nitro.hooks.callHook('close'))
if (nuxt.options.debug) {
// @ts-expect-error remove in next version of hookable
const { close } = createDebugger(nitro.hooks, {
pi0 marked this conversation as resolved.
Show resolved Hide resolved
tag: 'nitro build'
})
nitro.hooks.hook('close', close)
}

// Setup handlers
const devMiddlewareHandler = dynamicEventHandler()
Expand Down
15 changes: 14 additions & 1 deletion packages/nuxt/src/core/nuxt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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 { loadNuxtConfig, LoadNuxtOptions, nuxtCtx, installModule, addComponent, addVitePlugin, addWebpackPlugin, tryResolveModule, addPlugin } from '@nuxt/kit'
// Temporary until finding better placement
Expand Down Expand Up @@ -183,6 +183,11 @@ async function initNuxt (nuxt: Nuxt) {
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) {
if (Array.isArray(m)) {
await installModule(m[0], m[1])
Expand Down Expand Up @@ -229,6 +234,14 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {

const nuxt = createNuxt(options)

// TODO: parallel kit:compatibility calls in defineModule
// if (nuxt.options.debug) {
// // @ts-expect-error remove in next version of hookable
// createDebugger(nuxt.hooks, {
// tag: 'nuxt build'
// })
// }

if (opts.ready !== false) {
await nuxt.ready()
}
Expand Down
8 changes: 8 additions & 0 deletions packages/nuxt/src/core/runtime/nitro/plugins/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createDebugger } from 'hookable'
import type { NitroAppPlugin } from 'nitropack'

export default <NitroAppPlugin> function (nitro) {
createDebugger(nitro.hooks, {
tag: 'nitro app'
})
}
7 changes: 5 additions & 2 deletions packages/schema/src/config/_common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,15 @@ export default defineUntypedSchema({

/**
* Set to `true` to enable debug mode.
*
* At the moment, it prints out hook names and timings on the server, and
* logs hook arguments as well in the browser.
*
* By default, it's only enabled in development mode.
* @version 2
* @version 3
*/
debug: {
$resolve: async (val, get) => val ?? await get('dev')
$resolve: async (val, get) => val ?? false
},

/**
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8286,10 +8286,10 @@ __metadata:
languageName: node
linkType: hard

"hookable@npm:^5.2.2, hookable@npm:^5.3.0":
version: 5.3.0
resolution: "hookable@npm:5.3.0"
checksum: 5a170b790fc472fae11cc8ebc7a1336e7063c3f0a9697cefb5b18d455fcda4d1eadf21c254a27c0625fbbe31f736fd662797c21e6425e9c8cf073182b2ecae7f
"hookable@npm:^5.2.2, hookable@npm:^5.3.0, hookable@npm:^5.4.0":
version: 5.4.0
resolution: "hookable@npm:5.4.0"
checksum: 810376b27eab8a8c05684c2c4cb394c7837ac3cf7a91a63f0468cef908a87c982ab2f0320dce0bceaa5fac99006f361a113eb739d0ae3801a9137586b4707f0a
languageName: node
linkType: hard

Expand Down Expand Up @@ -10942,7 +10942,7 @@ __metadata:
globby: ^13.1.2
h3: ^0.7.21
hash-sum: ^2.0.0
hookable: ^5.3.0
hookable: ^5.4.0
knitwork: ^0.1.2
magic-string: ^0.26.7
mlly: ^0.5.16
Expand Down