feat(nuxt): granular debug options

This commit is contained in:
Anthony Fu 2025-01-14 16:17:50 +08:00
parent 06b1dfdaac
commit 4457fc2a97
No known key found for this signature in database
GPG Key ID: 179936958CD423FF
10 changed files with 45 additions and 13 deletions

View File

@ -120,7 +120,7 @@ function _defineNuxtModule<
// Measure setup time
if (setupTime > 5000 && uniqueKey !== '@nuxt/telemetry') {
logger.warn(`Slow module \`${uniqueKey || '<no name>'}\` took \`${setupTime}ms\` to setup.`)
} else if (nuxt.options.debug) {
} else if (nuxt.options.debug && nuxt.options.debug.log) {
logger.info(`Module \`${uniqueKey || '<no name>'}\` took \`${setupTime}ms\` to setup.`)
}

View File

@ -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 || setupTime > 500) {
if ((nuxt.options.debug && nuxt.options.debug.log) || setupTime > 500) {
logger.info(`Compiled \`${template.filename}\` in ${setupTime}ms`)
}

View File

@ -119,7 +119,7 @@ function createWatcher () {
function createGranularWatcher () {
const nuxt = useNuxt()
if (nuxt.options.debug) {
if (nuxt.options.debug && nuxt.options.debug.log) {
// 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 && !pending) {
if (nuxt.options.debug && nuxt.options.debug.log && !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) {
if (nuxt.options.debug && nuxt.options.debug.log) {
// 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) {
if (nuxt.options.debug && nuxt.options.debug.log) {
// eslint-disable-next-line no-console
console.timeEnd('[nuxt] builder:parcel:watch')
}

View File

@ -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,
debug: nuxt.options.debug && nuxt.options.debug.nitro,
rootDir: nuxt.options.rootDir,
workspaceDir: nuxt.options.workspaceDir,
srcDir: nuxt.options.serverDir,

View File

@ -552,7 +552,7 @@ async function initNuxt (nuxt: Nuxt) {
}
// Add nuxt app debugger
if (nuxt.options.debug) {
if (nuxt.options.debug && nuxt.options.debug.browser) {
addPlugin(resolve(nuxt.options.appDir, 'plugins/debug'))
}
@ -811,7 +811,7 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
nuxt.hooks.addHooks(opts.overrides.hooks)
}
if (nuxt.options.debug) {
if (nuxt.options.debug && nuxt.options.debug.hooks) {
createDebugger(nuxt.hooks, { tag: 'nuxt' })
}

View File

@ -176,7 +176,7 @@ export default defineNuxtModule({
const options: TypedRouterOptions = {
routesFolder: [],
dts: resolve(nuxt.options.buildDir, declarationFile),
logs: nuxt.options.debug,
logs: nuxt.options.debug && nuxt.options.debug.log,
async beforeWriteFiles (rootPage) {
rootPage.children.forEach(child => child.delete())
const pages = nuxt.apps.default?.pages || await resolvePagesRoutes(nuxt)

View File

@ -260,9 +260,30 @@ export default defineUntypedSchema({
* At the moment, it prints out hook names and timings on the server, and
* logs hook arguments as well in the browser.
*
* You can also set this to an object to enable specific debug options.
* ```js
* {
* log: true, // Log additional information on terminal
* hooks: true, // Log hooks calls and timings
* nitro: true, // Set nitro debug mode
* browser: true, // Log additional information in browser console
* prod: true // Log additional information in production mode (hydration mismatch, etc.)
* }
* ```
*
* @type {boolean | { log?: boolean, hooks?: boolean, nitro?: boolean, browser?: boolean, prod?: boolean }}
*/
debug: {
$resolve: val => val ?? isDebug,
$resolve: (val: boolean | { log?: boolean, hooks?: boolean, nitro?: boolean, browser?: boolean, prod?: boolean } | undefined) => {
val ??= isDebug
if (val === false) {
return val
}
if (val === true) {
return { log: true, hooks: true, nitro: true, browser: true, prod: true }
}
return val
},
},
/**

View File

@ -76,9 +76,10 @@ export interface NuxtBuilder {
}
// Normalized Nuxt options available as `nuxt.options.*`
export interface NuxtOptions extends Omit<ConfigSchema, 'vue' | 'sourcemap' | 'builder' | 'postcss' | 'webpack'> {
export interface NuxtOptions extends Omit<ConfigSchema, 'vue' | 'sourcemap' | 'debug' | 'builder' | 'postcss' | 'webpack'> {
vue: Omit<ConfigSchema['vue'], 'config'> & { config?: Partial<Filter<VueAppConfig, string | boolean>> }
sourcemap: Required<Exclude<ConfigSchema['sourcemap'], boolean>>
debug: Required<Exclude<ConfigSchema['debug'], true>>
builder: '@nuxt/vite-builder' | '@nuxt/webpack-builder' | '@nuxt/rspack-builder' | NuxtBuilder
postcss: Omit<ConfigSchema['postcss'], 'order'> & { order: Exclude<ConfigSchema['postcss']['order'], string> }
webpack: ConfigSchema['webpack'] & {

View File

@ -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,
'__VUE_PROD_HYDRATION_MISMATCH_DETAILS__': ctx.nuxt.options.debug && ctx.nuxt.options.debug.prod,
}))
}

10
playground/modules/foo.ts Normal file
View File

@ -0,0 +1,10 @@
import { defineNuxtModule, useNuxt } from 'nuxt/kit'
export default defineNuxtModule({
setup (options, nuxt) {
nuxt.options.alias['@foo'] = '/path/to/foo'
const nuxt2 = useNuxt()
nuxt2.options.buildId = 'foo'
},
})