fix(nuxt,schema,vite,webpack): analyze w/o overriding config (#23856)

This commit is contained in:
Daniel Roe 2023-10-23 20:19:16 +09:00 committed by GitHub
parent 755cf20534
commit cd76c617fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 22 additions and 29 deletions

View File

@ -78,11 +78,13 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
esbuild: {
options: { exclude: excludePattern }
},
analyze: nuxt.options.build.analyze && {
template: 'treemap',
projectRoot: nuxt.options.rootDir,
filename: join(nuxt.options.analyzeDir, '{name}.html')
},
analyze: nuxt.options.build.analyze && (nuxt.options.build.analyze === true || nuxt.options.build.analyze.enabled)
? {
template: 'treemap',
projectRoot: nuxt.options.rootDir,
filename: join(nuxt.options.analyzeDir, '{name}.html')
}
: false,
scanDirs: nuxt.options._layers.map(layer => (layer.config.serverDir || layer.config.srcDir) && resolve(layer.cwd, layer.config.serverDir || resolve(layer.config.srcDir, 'server'))).filter(Boolean),
renderer: resolve(distDir, 'core/runtime/nitro/renderer'),
errorHandler: resolve(distDir, 'core/runtime/nitro/error'),

View File

@ -97,7 +97,7 @@ export default defineUntypedSchema({
templates: [],
/**
* Nuxt uses `webpack-bundle-analyzer` to visualize your bundles and how to optimize them.
* Nuxt allows visualizing your bundles and how to optimize them.
*
* Set to `true` to enable bundle analysis, or pass an object with options: [for webpack](https://github.com/webpack-contrib/webpack-bundle-analyzer#options-for-plugin) or [for vite](https://github.com/btd/rollup-plugin-visualizer#options).
* @example
@ -106,20 +106,17 @@ export default defineUntypedSchema({
* analyzerMode: 'static'
* }
* ```
* @type {boolean | typeof import('webpack-bundle-analyzer').BundleAnalyzerPlugin.Options | typeof import('rollup-plugin-visualizer').PluginVisualizerOptions}
* @type {boolean | { enabled?: boolean } & ((0 extends 1 & typeof import('webpack-bundle-analyzer').BundleAnalyzerPlugin.Options ? {} : typeof import('webpack-bundle-analyzer').BundleAnalyzerPlugin.Options) | typeof import('rollup-plugin-visualizer').PluginVisualizerOptions)}
*/
analyze: {
$resolve: async (val, get) => {
if (val !== true) {
return val ?? false
}
const rootDir = await get('rootDir')
const analyzeDir = await get('analyzeDir')
return {
return defu(typeof val === 'boolean' ? { enabled: val } : val, {
template: 'treemap',
projectRoot: rootDir,
filename: join(analyzeDir, '{name}.html')
}
})
}
}
},

View File

@ -1,4 +1,4 @@
import { join } from 'pathe'
import { defu } from 'defu'
import { defineUntypedSchema } from 'untyped'
export default defineUntypedSchema({
@ -13,20 +13,11 @@ export default defineUntypedSchema({
* analyzerMode: 'static'
* }
* ```
* @type {boolean | typeof import('webpack-bundle-analyzer').BundleAnalyzerPlugin.Options}
* @type {boolean | { enabled?: boolean } & typeof import('webpack-bundle-analyzer').BundleAnalyzerPlugin.Options}
*/
analyze: {
$resolve: async (val, get) => {
if (val !== true) {
return val ?? false
}
const rootDir = await get('rootDir')
const analyzeDir = await get('analyzeDir')
return {
template: 'treemap',
projectRoot: rootDir,
filename: join(analyzeDir, '{name}.html')
}
return defu(val, await get('build.analyze'))
}
},

View File

@ -129,7 +129,7 @@ export async function buildClient (ctx: ViteBuildContext) {
}
// Add analyze plugin if needed
if (ctx.nuxt.options.build.analyze) {
if (ctx.nuxt.options.build.analyze && (ctx.nuxt.options.build.analyze === true || ctx.nuxt.options.build.analyze.enabled)) {
clientConfig.plugins!.push(...await import('./plugins/analyze').then(r => r.analyzePlugin(ctx)))
}

View File

@ -1,10 +1,13 @@
import type { Plugin } from 'vite'
import { transform } from 'esbuild'
import { visualizer } from 'rollup-plugin-visualizer'
import defu from 'defu'
import type { NuxtOptions } from 'nuxt/schema'
import type { ViteBuildContext } from '../vite'
export function analyzePlugin (ctx: ViteBuildContext): Plugin[] {
if (typeof ctx.nuxt.options.build.analyze === 'boolean') { return [] }
const analyzeOptions = defu({}, ctx.nuxt.options.build.analyze) as Exclude<NuxtOptions['build']['analyze'], boolean>
if (!analyzeOptions.enabled) { return [] }
return [
{
@ -22,8 +25,8 @@ export function analyzePlugin (ctx: ViteBuildContext): Plugin[] {
}
},
visualizer({
...ctx.nuxt.options.build.analyze,
filename: 'filename' in ctx.nuxt.options.build.analyze ? ctx.nuxt.options.build.analyze.filename!.replace('{name}', 'client') : undefined,
...analyzeOptions,
filename: 'filename' in analyzeOptions ? analyzeOptions.filename!.replace('{name}', 'client') : undefined,
title: 'Client bundle stats',
gzipSize: true
})

View File

@ -83,7 +83,7 @@ function clientOptimization (_ctx: WebpackConfigContext) {
function clientPlugins (ctx: WebpackConfigContext) {
// webpack Bundle Analyzer
// https://github.com/webpack-contrib/webpack-bundle-analyzer
if (!ctx.isDev && ctx.name === 'client' && ctx.userConfig.analyze) {
if (!ctx.isDev && ctx.name === 'client' && ctx.userConfig.analyze && (ctx.userConfig.analyze === true || ctx.userConfig.analyze.enabled)) {
const statsDir = resolve(ctx.options.analyzeDir)
ctx.config.plugins!.push(new BundleAnalyzerPlugin({