Skip to content

Commit

Permalink
feat(nuxi,schema): add support for setting nuxt logLevel (#19369)
Browse files Browse the repository at this point in the history
  • Loading branch information
Baroshem committed Mar 7, 2023
1 parent fe14961 commit 3f1e023
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 16 deletions.
9 changes: 4 additions & 5 deletions packages/nuxi/src/commands/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { defineNuxtCommand } from './index'
export default defineNuxtCommand({
meta: {
name: 'analyze',
usage: 'npx nuxi analyze [rootDir]',
usage: 'npx nuxi analyze [--log-level] [rootDir]',
description: 'Build nuxt and analyze production bundle (experimental)'
},
async invoke (args) {
Expand All @@ -24,10 +24,9 @@ export default defineNuxtCommand({

const nuxt = await loadNuxt({
rootDir,
config: {
build: {
analyze: true
}
overrides: {
build: { analyze: true },
logLevel: args['log-level']
}
})

Expand Down
3 changes: 2 additions & 1 deletion packages/nuxi/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { defineNuxtCommand } from './index'
export default defineNuxtCommand({
meta: {
name: 'build',
usage: 'npx nuxi build [--prerender] [--dotenv] [rootDir]',
usage: 'npx nuxi build [--prerender] [--dotenv] [--log-level] [rootDir]',
description: 'Build nuxt for production deployment'
},
async invoke (args) {
Expand All @@ -33,6 +33,7 @@ export default defineNuxtCommand({
}
},
overrides: {
logLevel: args['log-level'],
_generate: args.prerender
}
})
Expand Down
16 changes: 13 additions & 3 deletions packages/nuxi/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { defineNuxtCommand } from './index'
export default defineNuxtCommand({
meta: {
name: 'dev',
usage: 'npx nuxi dev [rootDir] [--dotenv] [--clipboard] [--open, -o] [--port, -p] [--host, -h] [--https] [--ssl-cert] [--ssl-key]',
usage: 'npx nuxi dev [rootDir] [--dotenv] [--log-level] [--clipboard] [--open, -o] [--port, -p] [--host, -h] [--https] [--ssl-cert] [--ssl-key]',
description: 'Run nuxt development server'
},
async invoke (args) {
Expand Down Expand Up @@ -48,7 +48,10 @@ export default defineNuxtCommand({

const config = await loadNuxtConfig({
cwd: rootDir,
overrides: { dev: true }
overrides: {
dev: true,
logLevel: args['log-level']
}
})

const listener = await listen(serverHandler, {
Expand Down Expand Up @@ -88,7 +91,14 @@ export default defineNuxtCommand({
await distWatcher.close()
}

currentNuxt = await loadNuxt({ rootDir, dev: true, ready: false })
currentNuxt = await loadNuxt({
rootDir,
dev: true,
ready: false,
overrides: {
logLevel: args['log-level']
}
})

currentNuxt.hooks.hookOnce('restart', async (options) => {
if (options?.hard && process.send) {
Expand Down
10 changes: 8 additions & 2 deletions packages/nuxi/src/commands/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ import { defineNuxtCommand } from './index'
export default defineNuxtCommand({
meta: {
name: 'prepare',
usage: 'npx nuxi prepare',
usage: 'npx nuxi prepare [--log-level] [rootDir]',
description: 'Prepare nuxt for development/build'
},
async invoke (args) {
process.env.NODE_ENV = process.env.NODE_ENV || 'production'
const rootDir = resolve(args._[0] || '.')

const { loadNuxt } = await loadKit(rootDir)
const nuxt = await loadNuxt({ rootDir, config: { _prepare: true } })
const nuxt = await loadNuxt({
rootDir,
overrides: {
_prepare: true,
logLevel: args['log-level']
}
})
await clearDir(nuxt.options.buildDir)

await buildNuxt(nuxt)
Expand Down
10 changes: 8 additions & 2 deletions packages/nuxi/src/commands/typecheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ import { defineNuxtCommand } from './index'
export default defineNuxtCommand({
meta: {
name: 'typecheck',
usage: 'npx nuxi typecheck [rootDir]',
usage: 'npx nuxi typecheck [--log-level] [rootDir]',
description: 'Runs `vue-tsc` to check types throughout your app.'
},
async invoke (args) {
process.env.NODE_ENV = process.env.NODE_ENV || 'production'
const rootDir = resolve(args._[0] || '.')

const { loadNuxt, buildNuxt } = await loadKit(rootDir)
const nuxt = await loadNuxt({ rootDir, config: { _prepare: true } })
const nuxt = await loadNuxt({
rootDir,
overrides: {
_prepare: true,
logLevel: args['log-level']
}
})

// Generate types and build nuxt instance
await writeTypes(nuxt)
Expand Down
18 changes: 18 additions & 0 deletions packages/schema/src/config/build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defineUntypedSchema } from 'untyped'
import { defu } from 'defu'
import { join } from 'pathe'
import { isTest } from 'std-env'

export default defineUntypedSchema({
/**
Expand Down Expand Up @@ -38,6 +39,23 @@ export default defineUntypedSchema({
},
},

/**
* Log level when building logs.
*
* Defaults to 'silent' when running in CI or when a TTY is not available.
* This option is then used as 'silent' in Vite and 'none' in Webpack
*
* @type {'silent' | 'info' | 'verbose'}
*/
logLevel: {
$resolve: (val) => {
if (val && !['silent', 'info', 'verbose'].includes(val)) {
console.warn(`Invalid \`logLevel\` option: \`${val}\`. Must be one of: \`silent\`, \`info\`, \`verbose\`.`)
}
return val ?? (isTest ? 'silent' : 'info')
}
},

/**
* Shared build configuration.
*/
Expand Down
11 changes: 9 additions & 2 deletions packages/vite/src/vite.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as vite from 'vite'
import { join, resolve } from 'pathe'
import type { Nuxt } from '@nuxt/schema'
import type { InlineConfig, SSROptions } from 'vite'
import type { Nuxt, NuxtOptions } from '@nuxt/schema'
import type { InlineConfig, SSROptions, UserConfig } from 'vite'
import { logger, isIgnored, resolvePath, addVitePlugin } from '@nuxt/kit'
import type { Options as VueOptions } from '@vitejs/plugin-vue'
import type { Options as VueJsxOptions } from '@vitejs/plugin-vue-jsx'
Expand Down Expand Up @@ -41,6 +41,7 @@ export async function bundle (nuxt: Nuxt) {
entry,
config: vite.mergeConfig(
{
logLevel: logLevelMap[nuxt.options.logLevel] ?? logLevelMap.info,
resolve: {
alias: {
...nuxt.options.alias,
Expand Down Expand Up @@ -147,3 +148,9 @@ export async function bundle (nuxt: Nuxt) {
await buildClient(ctx)
await buildServer(ctx)
}

const logLevelMap: Record<NuxtOptions['logLevel'], UserConfig['logLevel']> = {
silent: 'silent',
info: 'info',
verbose: 'info'
}
10 changes: 9 additions & 1 deletion packages/webpack/src/presets/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { resolve, normalize } from 'pathe'
// @ts-expect-error missing types
import TimeFixPlugin from 'time-fix-plugin'
import WebpackBar from 'webpackbar'
import type { Configuration } from 'webpack'
import webpack from 'webpack'
import { logger } from '@nuxt/kit'
// @ts-expect-error missing types
import FriendlyErrorsWebpackPlugin from '@nuxt/friendly-errors-webpack-plugin'
import escapeRegExp from 'escape-string-regexp'
import { joinURL } from 'ufo'
import type { NuxtOptions } from '@nuxt/schema'
import type { WarningFilter } from '../plugins/warning-ignore'
import WarningIgnorePlugin from '../plugins/warning-ignore'
import type { WebpackConfigContext } from '../utils/config'
Expand Down Expand Up @@ -40,7 +42,7 @@ function baseConfig (ctx: WebpackConfigContext) {
mode: ctx.isDev ? 'development' : 'production',
cache: getCache(ctx),
output: getOutput(ctx),
stats: 'none',
stats: statsMap[ctx.nuxt.options.logLevel] ?? statsMap.info,
...ctx.config
}
}
Expand Down Expand Up @@ -244,3 +246,9 @@ function getEnv (ctx: WebpackConfigContext) {

return _env
}

const statsMap: Record<NuxtOptions['logLevel'], Configuration['stats']> = {
silent: 'none',
info: 'normal',
verbose: 'verbose'
}

0 comments on commit 3f1e023

Please sign in to comment.