2022-12-11 21:44:52 +00:00
|
|
|
import type { Plugin } from 'vite'
|
2021-11-05 08:58:03 +00:00
|
|
|
import { transform } from 'esbuild'
|
|
|
|
import { visualizer } from 'rollup-plugin-visualizer'
|
2023-10-23 11:19:16 +00:00
|
|
|
import defu from 'defu'
|
|
|
|
import type { NuxtOptions } from 'nuxt/schema'
|
2024-06-13 22:35:00 +00:00
|
|
|
import type { RenderedModule } from 'rollup'
|
2022-12-11 21:44:52 +00:00
|
|
|
import type { ViteBuildContext } from '../vite'
|
2021-11-05 08:58:03 +00:00
|
|
|
|
|
|
|
export function analyzePlugin (ctx: ViteBuildContext): Plugin[] {
|
2023-10-23 11:19:16 +00:00
|
|
|
const analyzeOptions = defu({}, ctx.nuxt.options.build.analyze) as Exclude<NuxtOptions['build']['analyze'], boolean>
|
|
|
|
if (!analyzeOptions.enabled) { return [] }
|
2023-04-14 12:53:21 +00:00
|
|
|
|
2021-11-05 08:58:03 +00:00
|
|
|
return [
|
|
|
|
{
|
2022-02-10 09:29:49 +00:00
|
|
|
name: 'nuxt:analyze-minify',
|
2021-11-05 08:58:03 +00:00
|
|
|
async generateBundle (_opts, outputBundle) {
|
2024-06-13 22:35:00 +00:00
|
|
|
for (const _bundleId in outputBundle) {
|
|
|
|
const bundle = outputBundle[_bundleId]
|
2021-11-05 08:58:03 +00:00
|
|
|
if (bundle.type !== 'chunk') { continue }
|
2024-06-13 22:35:00 +00:00
|
|
|
const minifiedModuleEntryPromises: Array<Promise<[string, RenderedModule]>> = []
|
|
|
|
for (const moduleId in bundle.modules) {
|
|
|
|
const module = bundle.modules[moduleId]
|
|
|
|
minifiedModuleEntryPromises.push(
|
|
|
|
transform(module.code || '', { minify: true })
|
|
|
|
.then(result => [moduleId, { ...module, code: result.code }]),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
bundle.modules = Object.fromEntries(await Promise.all(minifiedModuleEntryPromises))
|
2021-11-05 08:58:03 +00:00
|
|
|
}
|
2024-04-05 18:08:32 +00:00
|
|
|
},
|
2021-11-05 08:58:03 +00:00
|
|
|
},
|
|
|
|
visualizer({
|
2023-10-23 11:19:16 +00:00
|
|
|
...analyzeOptions,
|
|
|
|
filename: 'filename' in analyzeOptions ? analyzeOptions.filename!.replace('{name}', 'client') : undefined,
|
2021-11-05 08:58:03 +00:00
|
|
|
title: 'Client bundle stats',
|
2024-04-05 18:08:32 +00:00
|
|
|
gzipSize: true,
|
|
|
|
}),
|
2021-11-05 08:58:03 +00:00
|
|
|
]
|
|
|
|
}
|