Bundle Size Limits

Set up bundle size validation and limits

Bundle Size Limits

This example shows how to enforce bundle size budgets with Packem's validation.bundleLimit option. You can set a single limit for the whole build, per-file (or per-glob) limits, and decide whether exceeding a limit should fail the build or only warn.

Overview

This example demonstrates:

  • Setting a total bundle size budget
  • Setting per-file and per-glob size limits
  • Mixing byte numbers and human-readable strings (e.g. "1MB")
  • Controlling whether limit violations fail the build (allowFail)

Configuration

// packem.config.ts
import { defineConfig } from '@visulima/packem/config'
import transformer from '@visulima/packem/transformer/esbuild'

export default defineConfig({
    transformer,
    validation: {
        bundleLimit: {
            // Total size budget across all build entries.
            // Accepts a number of bytes or a string with a unit.
            limit: "1MB",

            // Per-file / per-glob limits. The key is matched against the
            // output path (relative to outDir) or as a glob pattern.
            limits: {
                "**/*.mjs": "500KB",
                "index.cjs": 1048576, // 1MB in bytes
            },

            // When true, the build still succeeds and a warning is emitted
            // instead of failing when a limit is exceeded.
            // @default false
            allowFail: false,
        },
    },
})

Size units

The limit and each value in limits accept either:

  • A number of bytes, e.g. 1048576
  • A string with a unit suffix: "B", "KB", "MB", "GB", or "TB", e.g. "500KB", "1MB"
bundleLimit: {
    limit: 1048576,   // exact byte count
    // or
    limit: "1MB",     // parsed to bytes for you
}

How limits keys are matched

Each key in limits is matched against your build output. Packem first strips a leading outDir segment, then checks whether an output entry's path ends with the key, falling back to glob matching:

  • "index.cjs" matches the emitted index.cjs entry
  • "**/*.mjs" matches every emitted .mjs file via glob

If a key matches no output entry, it is skipped (a debug message is logged) rather than failing the build.

Failing vs. warning

By default a violation is reported and the build is treated as failed. Set allowFail: true to downgrade violations to warnings so the build still completes:

validation: {
    bundleLimit: {
        limit: "250KB",
        allowFail: true, // warn, but do not fail the build
    },
}

Running the build

packem build

If the total output or a matched file exceeds its limit, Packem reports the actual size against the limit, for example:

Total file size exceeds the limit: 1.4 MB / 1.00 MB
File size exceeds the limit: dist/index.cjs (1.2 MB / 1.00 MB)
Support

Contribute to our work and keep us going

Community is the heart of open source. The success of our packages wouldn't be possible without the incredible contributions of users, testers, and developers who collaborate with us every day.Want to get involved? Here are some tips on how you can make a meaningful impact on our open source projects.

Ready to help us out?

Be sure to check out the package's contribution guidelines first. They'll walk you through the process on how to properly submit an issue or pull request to our repositories.

Submit a pull request

Found something to improve? Fork the repo, make your changes, and open a PR. We review every contribution and provide feedback to help you get merged.

Good first issues

Simple issues suited for people new to open source development, and often a good place to start working on a package.
View good first issues