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

Commit

Permalink
refactor: migrate to hookable's createDebugger
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Oct 14, 2022
1 parent c66f16f commit 5fa1f91
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
19 changes: 4 additions & 15 deletions packages/nuxt/src/app/plugins/debug.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
import { createDebugger } from 'hookable'
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))
}
// @ts-expect-error remove in next version of hookable
createDebugger(nuxtApp.hooks, {
tag: 'nuxt app'
})
})
8 changes: 8 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 @@ -149,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, {
tag: 'nitro build'
})
nitro.hooks.hook('close', close)
}

// Setup handlers
const devMiddlewareHandler = dynamicEventHandler()
Expand Down
12 changes: 10 additions & 2 deletions 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,7 +183,7 @@ async function initNuxt (nuxt: Nuxt) {
addPlugin(resolve(nuxt.options.appDir, 'plugins/preload.server'))
}

// Track components used to render for webpack
// Add nuxt app debugger
if (nuxt.options.debug) {
addPlugin(resolve(nuxt.options.appDir, 'plugins/debug'))
}
Expand Down Expand Up @@ -234,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
11 changes: 3 additions & 8 deletions packages/nuxt/src/core/runtime/nitro/plugins/debug.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { createDebugger } from 'hookable'
import type { NitroAppPlugin } from 'nitropack'

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

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

nitro.hooks.afterEach(({ name }) => {
console.timeEnd(wrapName(name))
createDebugger(nitro.hooks, {
tag: 'nitro app'
})
}

0 comments on commit 5fa1f91

Please sign in to comment.