@fluidframework/bundle-size-tools

中文文档
0.65.0发布于 2026/04/21MITTypeScriptCommonJS近 30 天下载 6143

Utility for analyzing bundle size regressions

原始使用文档
文档目录11 节
  1. Features of bundle-size-tools
  2. Limitations
  3. Webpack stats files
  4. Baseline builds
  5. Bundle Comparisons
  6. Default Stats Processors
  7. Bundles all generate compressed stats files
  8. Main CI Generates Baseline stats files
  9. Pull Request Buddy Builds generate comparison stats files
  10. Build runs main CI completes to update pull requests pendin…
  11. Bundle metrics considered

@fluidframework/bundle-size-tools

This package contains utility for analyzing bundle sizes to help catch code size regressions at pull request time. This library is meant to be fully extensible to meet the unique needs of any repository, but is currently only being used in a few repositories, so additional work may be necessary in order to make the library more useful for others.

Features of bundle-size-tools

  • Accurate comparisons that enables you to see exactly how many bytes a PR adds to relevant bundles
  • APIs for integrating with Azure devops workflows
  • The ability to write comments on pull requests that call out bundle size regressions
  • Extension points so that teams can write their own custom bundle reports
  • Support for monorepos that build many packages with multiple interesting bundles
  • The ability to define custom bundle metrics in a bundle's webpack config

Limitations

  • Bundle-size-tools currently only has APIs for working with Azure DevOps. Additional work will have to be done to support other source control providers like GitHub. Other tools can be used in conjunction with bundle-size-tools to support hybrid environments.
  • Bundle-size-tools was designed to work with webpack; other bundlers are not supported.

How Bundle-size-tools works

This section highlights some of the assumptions that bundle-size-tools makes and defines the pieces required to onboard a new repository.

Webpack stats files

Bundle-size-tools uses webpack stats files as the foundation for bundle comparisons. To use bundle-size-tools, your build pipeline is expected to produce webpack stats files. Since webpack stats files tend to be very large, we recommend using the @mixer/webpack-bundle-compare webpack plugin to generate the plugins in gzipped mspack format to reduce the size of the stats files on disk.

Our recommended approach is to add this to the plugin section of your webpack configs that produce application bundles:

new BundleComparisonPlugin({
    // File to create, relative to the webpack build output path:
    file: resolve(process.cwd(), "bundleAnalysis/bundleStats.msp.gz"),
});

Baseline builds

In order to provide accurate metrics for bundle size regressions, Bundle-size-tools must have a baseline to compare against. For example, when submitting a pull request, bundle-size-tools should only report changes to bundle sizes that are directly related to the current pull request. If the pull request was targeting the main branch of the repository, we would consider the baseline to be the main commit the PR branch was based off. To use bundle-size-tools, you'll need a mechanism to get the webpack stats files for the baseline builds, such as generating webpack stats file for every commit to main using an automated build or providing a way to find a matching stats file from a different commit.

Bundle Comparisons

Bundle-size-tool's comparisons are implemented using WebpackStatsProcessors, which are functions with the following signature:

export type WebpackStatsProcessor = (
    stats: Webpack.StatsCompilation,
    config: BundleBuddyConfig | undefined,
) => BundleMetricSet | undefined;

A WebpackStatsProcessor takes a webpack stats object in as input and outputs a BundleMetricSet. In essence, WebpackStatsProcessors take in the complex stats object and outputs a simple list of metrics. There is also support for passing in a bundle-specific BundleBuddyConfig object that enables analysis on specific chunks in the bundle. The BundleBuddyConfig is provided in the bundle's webpack config using the BundleBuddyConfigWebpackPlugin.

export type BundleMetricSet = Map<string, BundleMetric>;

export interface BundleMetric {
    parsedSize: number;
}

Consumers of bundle-size-tools must configure one or more WebpackStatsProcessors for their projects. It is also possible to write and use custom WebpackStatsProcessors.

Bundle-size-tools runs the same set of WebpackStatsProcessors on both the baseline commit and pull request commit. It then compares the metrics produces by the baseline commit and pull request commit and reports these differences in a comment in the pull request.

Default Stats Processors

Bundle-size-tools provides the following basic set of WebpackStatsProcessors:

  • entryStatsProcessor - reports the size of the chunks generated for each of the webpack entry points specified in the webpack config
  • totalSizeProcessor - reports the total size of all chunks in the webpack bundle.
  • bundleBuddyConfigProcessor - enables analysis of a specific set of chunks for a given bundle. It is expected that a BundleBuddyConfig would be specified via the BundleBuddyConfigWebpackPlugin in the bundles webpack config

Sample Workflow

This is the workflow the fluidframework repository uses for Bundle buddy.

Assumptions

  • Monorepo that produces one or more packages.
  • Packages in the repository only produce one bundle that is going to be analyzed by bundle-size-tools.
  • CI builds run and store artifacts in Azure DevOps.

Bundles all generate compressed stats files

Every single bundle in the repository is configured with the @mixer/webpack-bundle-compare webpack plugin to generate a compressed stats file named bundleStats.msp.gz in a bundleAnalysis folder at the root of each package. As an example, say our repository has packages package1, package2, and package3 all under a packages folder, our build process will generate the following files:

  • packages/package1/bundleAnalysis/bundle1/bundleStats.msp.gz
  • packages/package1/bundleAnalysis/bundle2/bundleStats.msp.gz
  • packages/package2/bundleAnalysis/bundleStats.msp.gz
  • packages/package3/bundleAnalysis/bundleStats.msp.gz

Main CI Generates Baseline stats files

There is a continuous integration build that runs on every commit to main and generates the bundle stats files. It is important that this CI definition does not have the batch option set, it should run for every single commit to main to ensure we have accurate baselines for every commit to main. It is also important to ensure the stats files used as baselines are generated from running webpack with the mode set to production and not development to get the best representation of what our users will download.

The build process then runs a script that copies all the bundle stats files in the repository and copies them into a single bundleAnalysis folder in a temporary directory.

  • /bundleAnalysis/package1/bundle1/bundleStats.msp.gz
  • /bundleAnalysis/package1/bundle2/bundleStats.msp.gz
  • /bundleAnalysis/package2/bundleStats.msp.gz
  • /bundleAnalysis/package3/bundleStats.msp.gz

The build process then uploads this folder as an Azure DevOps build artifact names bundle-analysis-reports.

Pull Request Buddy Builds generate comparison stats files

The buddy build that runs for every pull request runs the same process as the main build to generate the bundleAnalysis folder and bundle-analysis-reports build artifact.

The buddy build will then determine the main commit that this PR was branched off of, using that commit as the "baseline" commit. If the main CI build has already created the bundle-analysis-reports artifact, the pull request buddy build will run bundle buddy and report the result as a comment in the pull request.

If the artifacts are not available for the baseline commit, the buddy build adds an Azure Devops build tag with the format bundle-size-tools-pending-<commitHash> so that a later process can add the bundle analysis comment to this PR when the baseline artifacts are ready.

Build runs main CI completes to update pull requests pending baseline stats

There is a separate build pipeline that runs after each successful main build and looks for all buddy builds with the bundle-size-tools-pending-<commitHash> corresponding to the <commitHash> of the main build. This build process will then run bundle-size-tools on all these PRs and post a comment.

Bundle metrics considered

The repository is configured to run the entryStatsProcessor and totalSizeProcessor for all bundles, meaning that bundle buddy will report changes to total bundle size (sum of all chunks) and the size of each entry chunk.

The repository also has a few bundles that we wish to analyze the size of some critical scenarios that are behind a dynamic import, meaning they are not entry chunks. This leverages the bundleBuddyConfigProcessor, which uses a config file to specify the names of chunks to analyze. These config files are specified using the BundleBuddyConfigWebpackPlugin and these config files are uploaded alongside the bundleStats.msp.gz file in the bundle-analysis-reports artifact.

版本与发布记录

@fluidframework/bundle-size-tools 共发布 174 个版本,首次发布于 2020/09/26;过去 12 个月发布了 5 个版本。

版本发布日期距今
0.65.0latest2026/04/216 个月前
0.64.02026/03/148 个月前
0.63.02026/01/1410 个月前
0.62.02025/12/1910 个月前
0.61.02025/12/0511 个月前
0.60.02025/11/071 年前

其他发布通道:next → 0.29.0-227332,dev → 0.56.0-332685。

关于 @fluidframework/bundle-size-tools 的常见问题

@fluidframework/bundle-size-tools 是什么?

Utility for analyzing bundle size regressions

如何安装 @fluidframework/bundle-size-tools?

在项目目录运行 npm install @fluidframework/bundle-size-tools,或使用 pnpm add / yarn add / bun add。

@fluidframework/bundle-size-tools 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fluidframework/bundle-size-tools 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@fluidframework/bundle-size-tools 可以商用吗?

@fluidframework/bundle-size-tools 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fluidframework/bundle-size-tools 的下载量有多少?

根据 npm 官方下载统计,@fluidframework/bundle-size-tools 在 2026/10/15 – 2026/11/13 这 30 天内被下载 6,143 次,日均约 205 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fluidframework/bundle-size-tools 还在维护吗?

最新版本 0.65.0 发布于 2026/04/21(6 个月前);至今共发布 174 个版本,其中过去 12 个月发布了 5 个;首次发布于 2020/09/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。