Skip to content

Commit

Permalink
feat(nuxi): bundle analyzer (#701)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooya Parsa <pyapar@gmail.com>
  • Loading branch information
danielroe and pi0 authored Oct 21, 2021
1 parent f71714d commit 5364d04
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 10 additions & 6 deletions src/rollup/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}

0 comments on commit 5364d04

Please sign in to comment.