mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 21:55:11 +00:00
refactor(vite): move analyzePlugin to a seperate file (#1710)
This commit is contained in:
parent
85d173d653
commit
4da062a120
@ -5,9 +5,8 @@ import vuePlugin from '@vitejs/plugin-vue'
|
||||
import viteJsxPlugin from '@vitejs/plugin-vue-jsx'
|
||||
import type { Connect } from 'vite'
|
||||
|
||||
import { visualizer } from 'rollup-plugin-visualizer'
|
||||
import { transform } from 'esbuild'
|
||||
import { cacheDirPlugin } from './plugins/cache-dir'
|
||||
import { analyzePlugin } from './plugins/analyze'
|
||||
import { wpfs } from './utils/wpfs'
|
||||
import type { ViteBuildContext, ViteOptions } from './vite'
|
||||
import { writeManifest } from './manifest'
|
||||
@ -49,29 +48,7 @@ export async function buildClient (ctx: ViteBuildContext) {
|
||||
|
||||
// Add analyze plugin if needed
|
||||
if (ctx.nuxt.options.build.analyze) {
|
||||
clientConfig.plugins.push({
|
||||
name: 'nuxt-analyze-minify',
|
||||
async generateBundle (_opts, outputBundle) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
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
|
||||
}
|
||||
})
|
||||
clientConfig.plugins.push(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
|
||||
}))
|
||||
clientConfig.plugins.push(...analyzePlugin(ctx))
|
||||
}
|
||||
|
||||
await ctx.nuxt.callHook('vite:extendConfig', clientConfig, { isClient: true, isServer: false })
|
||||
|
32
packages/vite/src/plugins/analyze.ts
Normal file
32
packages/vite/src/plugins/analyze.ts
Normal file
@ -0,0 +1,32 @@
|
||||
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 [
|
||||
{
|
||||
name: 'nuxt-analyze-minify',
|
||||
async generateBundle (_opts, outputBundle) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
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
|
||||
})
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue
Block a user