From 5364d0444ccc5353c8152623b6590d6bbc8efb1e Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 21 Oct 2021 20:51:44 +0100 Subject: [PATCH] feat(nuxi): bundle analyzer (#701) Co-authored-by: Pooya Parsa --- src/context.ts | 5 +++-- src/package.json | 2 +- src/rollup/config.ts | 16 ++++++++++------ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/context.ts b/src/context.ts index 64d3d4adc9..edf432670f 100644 --- a/src/context.ts +++ b/src/context.ts @@ -4,6 +4,7 @@ import defu from 'defu' import { createHooks, Hookable, NestedHooks } from 'hookable' import type { Preset } from 'unenv' import type { NuxtHooks, NuxtOptions } from '@nuxt/kit' +import type { PluginVisualizerOptions } from 'rollup-plugin-visualizer' import { tryImport, resolvePath, detectTarget, extendPreset } from './utils' import * as PRESETS from './presets' import type { NodeExternalsOptions } from './rollup/plugins/externals' @@ -28,7 +29,7 @@ export interface NitroContext { minify: boolean sourceMap: boolean externals: boolean | NodeExternalsOptions - analyze: boolean + analyze: false | PluginVisualizerOptions entry: string node: boolean preset: string @@ -94,7 +95,7 @@ export function getNitroContext (nuxtOptions: NuxtOptions, input: NitroInput): N minify: undefined, sourceMap: undefined, externals: undefined, - analyze: undefined, + analyze: nuxtOptions.build.analyze as any, entry: undefined, node: undefined, preset: undefined, diff --git a/src/package.json b/src/package.json index af3997a67a..f1e895ca9e 100644 --- a/src/package.json +++ b/src/package.json @@ -58,8 +58,8 @@ "pathe": "^0.2.0", "pretty-bytes": "^5.6.0", "rollup": "^2.58.0", - "rollup-plugin-analyzer": "^4.0.0", "rollup-plugin-terser": "^7.0.2", + "rollup-plugin-visualizer": "^5.5.2", "serve-placeholder": "^1.2.4", "serve-static": "^1.14.1", "std-env": "^2.3.1", diff --git a/src/rollup/config.ts b/src/rollup/config.ts index 78a8c04cee..d01b83a5d8 100644 --- a/src/rollup/config.ts +++ b/src/rollup/config.ts @@ -12,7 +12,7 @@ import replace from '@rollup/plugin-replace' import virtual from '@rollup/plugin-virtual' import wasmPlugin from '@rollup/plugin-wasm' import inject from '@rollup/plugin-inject' -import analyze from 'rollup-plugin-analyzer' +import { visualizer } from 'rollup-plugin-visualizer' import * as unenv from 'unenv' import type { Preset } from 'unenv' @@ -309,11 +309,6 @@ export const getRollupConfig = (nitroContext: NitroContext) => { // https://github.com/rollup/plugins/tree/master/packages/inject rollupConfig.plugins.push(inject(env.inject)) - if (nitroContext.analyze) { - // https://github.com/doesdev/rollup-plugin-analyzer - rollupConfig.plugins.push(analyze()) - } - // https://github.com/TrySound/rollup-plugin-terser // https://github.com/terser/terser#minify-nitroContext if (nitroContext.minify) { @@ -328,5 +323,14 @@ export const getRollupConfig = (nitroContext: NitroContext) => { })) } + if (nitroContext.analyze) { + // https://github.com/btd/rollup-plugin-visualizer + rollupConfig.plugins.push(visualizer({ + ...nitroContext.analyze, + filename: nitroContext.analyze.filename.replace('{name}', 'nitro'), + title: 'Nitro Server bundle stats' + })) + } + return rollupConfig }