mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-19 01:45:53 +00:00
fix(nuxt,schema,vite,webpack): analyze w/o overriding config (#23856)
This commit is contained in:
parent
755cf20534
commit
cd76c617fa
@ -78,11 +78,13 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
|
|||||||
esbuild: {
|
esbuild: {
|
||||||
options: { exclude: excludePattern }
|
options: { exclude: excludePattern }
|
||||||
},
|
},
|
||||||
analyze: nuxt.options.build.analyze && {
|
analyze: nuxt.options.build.analyze && (nuxt.options.build.analyze === true || nuxt.options.build.analyze.enabled)
|
||||||
template: 'treemap',
|
? {
|
||||||
projectRoot: nuxt.options.rootDir,
|
template: 'treemap',
|
||||||
filename: join(nuxt.options.analyzeDir, '{name}.html')
|
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),
|
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'),
|
renderer: resolve(distDir, 'core/runtime/nitro/renderer'),
|
||||||
errorHandler: resolve(distDir, 'core/runtime/nitro/error'),
|
errorHandler: resolve(distDir, 'core/runtime/nitro/error'),
|
||||||
|
@ -97,7 +97,7 @@ export default defineUntypedSchema({
|
|||||||
templates: [],
|
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).
|
* 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
|
* @example
|
||||||
@ -106,20 +106,17 @@ export default defineUntypedSchema({
|
|||||||
* analyzerMode: 'static'
|
* 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: {
|
analyze: {
|
||||||
$resolve: async (val, get) => {
|
$resolve: async (val, get) => {
|
||||||
if (val !== true) {
|
|
||||||
return val ?? false
|
|
||||||
}
|
|
||||||
const rootDir = await get('rootDir')
|
const rootDir = await get('rootDir')
|
||||||
const analyzeDir = await get('analyzeDir')
|
const analyzeDir = await get('analyzeDir')
|
||||||
return {
|
return defu(typeof val === 'boolean' ? { enabled: val } : val, {
|
||||||
template: 'treemap',
|
template: 'treemap',
|
||||||
projectRoot: rootDir,
|
projectRoot: rootDir,
|
||||||
filename: join(analyzeDir, '{name}.html')
|
filename: join(analyzeDir, '{name}.html')
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { join } from 'pathe'
|
import { defu } from 'defu'
|
||||||
import { defineUntypedSchema } from 'untyped'
|
import { defineUntypedSchema } from 'untyped'
|
||||||
|
|
||||||
export default defineUntypedSchema({
|
export default defineUntypedSchema({
|
||||||
@ -13,20 +13,11 @@ export default defineUntypedSchema({
|
|||||||
* analyzerMode: 'static'
|
* analyzerMode: 'static'
|
||||||
* }
|
* }
|
||||||
* ```
|
* ```
|
||||||
* @type {boolean | typeof import('webpack-bundle-analyzer').BundleAnalyzerPlugin.Options}
|
* @type {boolean | { enabled?: boolean } & typeof import('webpack-bundle-analyzer').BundleAnalyzerPlugin.Options}
|
||||||
*/
|
*/
|
||||||
analyze: {
|
analyze: {
|
||||||
$resolve: async (val, get) => {
|
$resolve: async (val, get) => {
|
||||||
if (val !== true) {
|
return defu(val, await get('build.analyze'))
|
||||||
return val ?? false
|
|
||||||
}
|
|
||||||
const rootDir = await get('rootDir')
|
|
||||||
const analyzeDir = await get('analyzeDir')
|
|
||||||
return {
|
|
||||||
template: 'treemap',
|
|
||||||
projectRoot: rootDir,
|
|
||||||
filename: join(analyzeDir, '{name}.html')
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -129,7 +129,7 @@ export async function buildClient (ctx: ViteBuildContext) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add analyze plugin if needed
|
// 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)))
|
clientConfig.plugins!.push(...await import('./plugins/analyze').then(r => r.analyzePlugin(ctx)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
import type { Plugin } from 'vite'
|
import type { Plugin } from 'vite'
|
||||||
import { transform } from 'esbuild'
|
import { transform } from 'esbuild'
|
||||||
import { visualizer } from 'rollup-plugin-visualizer'
|
import { visualizer } from 'rollup-plugin-visualizer'
|
||||||
|
import defu from 'defu'
|
||||||
|
import type { NuxtOptions } from 'nuxt/schema'
|
||||||
import type { ViteBuildContext } from '../vite'
|
import type { ViteBuildContext } from '../vite'
|
||||||
|
|
||||||
export function analyzePlugin (ctx: ViteBuildContext): Plugin[] {
|
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 [
|
return [
|
||||||
{
|
{
|
||||||
@ -22,8 +25,8 @@ export function analyzePlugin (ctx: ViteBuildContext): Plugin[] {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
visualizer({
|
visualizer({
|
||||||
...ctx.nuxt.options.build.analyze,
|
...analyzeOptions,
|
||||||
filename: 'filename' in ctx.nuxt.options.build.analyze ? ctx.nuxt.options.build.analyze.filename!.replace('{name}', 'client') : undefined,
|
filename: 'filename' in analyzeOptions ? analyzeOptions.filename!.replace('{name}', 'client') : undefined,
|
||||||
title: 'Client bundle stats',
|
title: 'Client bundle stats',
|
||||||
gzipSize: true
|
gzipSize: true
|
||||||
})
|
})
|
||||||
|
@ -83,7 +83,7 @@ function clientOptimization (_ctx: WebpackConfigContext) {
|
|||||||
function clientPlugins (ctx: WebpackConfigContext) {
|
function clientPlugins (ctx: WebpackConfigContext) {
|
||||||
// webpack Bundle Analyzer
|
// webpack Bundle Analyzer
|
||||||
// https://github.com/webpack-contrib/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)
|
const statsDir = resolve(ctx.options.analyzeDir)
|
||||||
|
|
||||||
ctx.config.plugins!.push(new BundleAnalyzerPlugin({
|
ctx.config.plugins!.push(new BundleAnalyzerPlugin({
|
||||||
|
Loading…
Reference in New Issue
Block a user