MIT License

技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

项目背景与痛点

在现代 Web 开发中,项目打包成了开发流程中的一个重要环节。而打包后生成的文件大小直接影响到项目的加载时间和用户体验。因此,对于开发者来说,能够精准地分析打包结果,从而优化项目性能成为了一个亟待解决的问题。

vite-bundle-analyzer 是一个针对 Vite 项目设计的打包分析工具,它能够清晰地展示出打包后的文件大小和组成结构。相对于其他打包分析工具,它更直接聚焦在 Vite 这一新兴的开发框架上,提供了一个更为便捷的解决方案。

核心技术揭秘

vite-bundle-analyzer 的实现主要是基于 Webpack 的 BundleAnalyzerPlugin。虽然 Vite 本身并不使用 Webpack,但是 Vite 在构建阶段却保留了将代码打包到静态资源的能力。这意味着我们可以借用 Webpack 的插件来实现对项目打包过程的分析。

下面是使用 vite-bundle-analyzer 进行打包分析的原理框架图。从项目初始化到输出分析报告的各个步骤都可以在下面的 Mermaid 图中体现。

graph TD A[用户配置] --> B[启动 Vite] B --> C[构建项目] C --> D[执行分析插件] D --> E[生成分析报告] E --> F[用户查看报告]

从原理上分析,vite-bundle-analyzer 插件会在 Vite 的构建阶段被触发,插件通过监听打包过程中的资源生成事件,记录下每个资源的大小、依赖关系等重要信息,并最终以图表的形式呈现给用户。

功能亮点与差异

相比于其他打包分析工具,vite-bundle-analyzer 的最大亮点在于对 Vite 项目的高度匹配性和易用性。它紧密契合 Vite 的模块处理机制和构建流程,可以提供更精准、更快速的报告生成。

此外,插件支持自定义配置,允许用户根据自身需求调整分析的粒度和范围。从实际使用角度出发,它可以为开发者提供决策支持,帮助识别和优化“膨胀”的代码模块。

应用场景与落地建议

在生产环境中,vite-bundle-analyzer 特别适合在代码评审阶段使用,帮助团队成员快速定位臃肿的文件。它适用于大型、复杂的 Vite 项目,特别是那些需要精细控制资源大小以提升加载性能的场景。

建议在每次版本迭代前使用该插件进行打包分析,不仅可以保证代码质量,还能防止意外引入的额外资源使整体项目变大。

综合评价

vite-bundle-analyzer 提供了对 Vite 项目独到的打包分析能力,其高度集成了 Vite 的插件机制,带来了极大的便利性和实用性。优点在于它的高度适配性和各种自定义配置选项带来的灵活性。然而,不足之处在于它对于非 Vite 项目的支持度不够,这意味着大多数情况下,它只能作为专门服务于 Vite 生态的工具。

vite-bundle-analyzer 是一个值得推荐的工具,特别是给那些正在寻找 Vite 项目打包优化解决方案的专业开发者。

关于

GitHub 原始描述(英文)

bundle analyzer for vite

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

简要分析

热度分
503
价值分
286
活跃状态
活跃

语言占比

HTML
JavaScript
TypeScript
Zig

Release

README

vite-bundle-analyzer

Awesome

Vite Bundle Analyzer

A bundle analyzer for Vite, Rollup, and Rolldown that visualizes bundle size with an interactive treemap.

Warning 警告

Vite enables minification by default. The parsed size may appear larger than actual size because bundle info from Rollup isn’t compressed. Rolldown support is experimental.

Installation

npm install vite-bundle-analyzer -D
# or
yarn add vite-bundle-analyzer -D
# or
pnpm add vite-bundle-analyzer -D

Usage

Plugins only work during the build phase.

Vite

import { defineConfig } from 'vite'
import { analyzer } from 'vite-bundle-analyzer'

export default defineConfig({
  plugins: [
    analyzer()
  ]
})

Rollup

import { adapter } from 'vite-bundle-analyzer'
import { analyzer } from 'vite-bundle-analyzer'

export default {
  plugins: [
    adapter(analyzer())
  ]
}

Rolldown Plugin (Experimental)

import { unstableRolldownAdapter } from 'vite-bundle-analyzer'
import { analyzer } from 'vite-bundle-analyzer'

export default {
  plugins: [
    unstableRolldownAdapter(analyzer())
  ]
}

Configuration

Option Type Default Description
analyzerMode 'server' | 'static' | 'json' | function 'server' Analysis output mode
fileName string | function 'stats' Output filename (without extension)
reportTitle string 'Vite Bundle Analyzer' Report page title
gzipOptions Record<string, any> {} Gzip compression options (see zlib module)
brotliOptions Record<string, any> {} Brotli compression options
analyzerPort number | 'auto' 8888 Server port
openAnalyzer boolean true Auto-open browser (server/static mode only)
defaultSizes 'stat' | 'gzip' | 'brotli' 'stat' Default size metric
summary boolean true Show summary in console
enabled boolean true Enable/disable plugin
include string | RegExp | Array<string | RegExp> [] Include patterns
exclude string | RegExp | Array<string | RegExp> [] Exclude patterns
pathFormatter (path: string, defaultWd: string) => string undefined Formatting Paths

CLI

npx vite-bundle-analyzer

# With options
npx vite-bundle-analyzer --help

# If you're using rolldown-vite pls using the following command

npx vite-bundle-analyzer -e=rolldown-vite

Common Issues

Empty Chunk Sizes

If chunk sizes appear empty, check for plugins that break source maps (e.g., @vitejs/plugin-legacy). The analyzer relies on source maps for accurate size calculation.

Vite-based Frameworks

For frameworks built on Vite (VitePress, Remix, Qwik), use analyzerMode: 'server' for accurate results due to multiple build instances.

Static Mode Without Server

When analyzerMode: 'static' and openAnalyzer: false, no development server is created.

API Integration

import { SSE, createServer, injectHTMLTag, openBrowser, renderView } from 'vite-bundle-analyzer'

// Create custom server
const server = createServer()
server.get('/', async (c) => {
  const html = await renderView(data, { title: 'Custom Analyzer', mode: 'stat' })
  c.res.writeHead(200, { 'Content-Type': 'text/html' })
  c.res.write(html)
  c.res.end()
})
server.listen(3000)

Debug

# Vite
vite build --debug

# Environment variable
ANALYZE_DEBUG=true npm run build

Contributors

Kanno Aleksandr Mitskevich mengdaoshizhongxinyang

Sponsors

Credits

  • Geist UI
  • The friend who proposed this idea.
  • All contributors and sponsors

License

MIT

评论

暂无评论