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

Commit

Permalink
feat(nuxt): add hook debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Sep 20, 2022
1 parent 896622e commit 25ee725
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
20 changes: 20 additions & 0 deletions packages/nuxt/src/app/plugins/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { defineNuxtPlugin } from '#app'

const wrapName = (event: string) => process.server ? `[nuxt] ${event}` : event

export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.hooks.beforeEach(({ name }) => {
console.time(wrapName(name))
})

nuxtApp.hooks.afterEach(({ name, args }) => {
if (process.client) {
console.groupCollapsed(name)
console.timeLog(name, args)
console.groupEnd()
}
if (process.server) {
console.timeEnd(wrapName(name))
}
})
})
5 changes: 5 additions & 0 deletions packages/nuxt/src/core/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,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 Down
5 changes: 5 additions & 0 deletions packages/nuxt/src/core/nuxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ async function initNuxt (nuxt: Nuxt) {
addPlugin(resolve(nuxt.options.appDir, 'plugins/preload.server'))
}

// Track components used to render for webpack
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
13 changes: 13 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,13 @@
import type { NitroAppPlugin } from 'nitropack'

const wrapName = (event: string) => process.server ? `[nitro] ${event}` : event

export default <NitroAppPlugin> function (nitro) {
nitro.hooks.beforeEach(({ name }) => {
console.time(wrapName(name))
})

nitro.hooks.afterEach(({ name }) => {
console.timeEnd(wrapName(name))
})
}
7 changes: 5 additions & 2 deletions packages/schema/src/config/_common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,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

0 comments on commit 25ee725

Please sign in to comment.