2021-11-05 08:58:03 +00:00
|
|
|
import { Plugin } from 'vite'
|
|
|
|
import { transform } from 'esbuild'
|
|
|
|
import { visualizer } from 'rollup-plugin-visualizer'
|
|
|
|
import { ViteBuildContext } from '../vite'
|
|
|
|
|
|
|
|
export function analyzePlugin (ctx: ViteBuildContext): Plugin[] {
|
|
|
|
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) {
|
|
|
|
for (const [_bundleId, bundle] of Object.entries(outputBundle)) {
|
|
|
|
if (bundle.type !== 'chunk') { continue }
|
|
|
|
const originalEntries = Object.entries(bundle.modules)
|
|
|
|
const minifiedEntries = await Promise.all(originalEntries.map(async ([moduleId, module]) => {
|
|
|
|
const { code } = await transform(module.code || '', { minify: true })
|
|
|
|
return [moduleId, { ...module, code }]
|
|
|
|
}))
|
|
|
|
bundle.modules = Object.fromEntries(minifiedEntries)
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
visualizer({
|
|
|
|
...ctx.nuxt.options.build.analyze as any,
|
|
|
|
// @ts-ignore
|
|
|
|
filename: ctx.nuxt.options.build.analyze.filename.replace('{name}', 'client'),
|
|
|
|
title: 'Client bundle stats',
|
|
|
|
gzipSize: true
|
|
|
|
})
|
|
|
|
]
|
|
|
|
}
|