BundleMon

repository·main·Indexed 41 days ago

https://github.com/lironer/bundlemon

A tool for monitoring bundle sizes on every commit to prevent bundle bloat. It allows developers to set size limits and receive alerts via CI integrations and GitHub comments. BundleMon provides a CLI for CI/CD pipelines, a self-hosted platform via Docker (bundlemon-platform v1.0.1), and a service for tracking historical bundle size records and managing project IDs.

Tokens
19.5K
Snippets
70
Records
85
Agent score
51%

What's inside BundleMon

  1. Use the BundleMon platform Docker image

    main
    The bundlemon-platform Docker image is a consolidated distribution that contains both the BundleMon service and the BundleMon website (UI). Use this image if you want to deploy the entire BundleMon stack (backend and frontend) within a single containerized environment.
  2. Data stored in the BundleMon free hosted service

    main

    When using the free hosted service, BundleMon stores specific metadata for each record (commit). This data is used to track bundle changes and provide feedback (e.g., via GitHub).

    Stored information includes:

    • Project ID: Stored in a separate collection. For GitHub projects, this includes the owner and repository names.
    • Sub project name: If specified in your configuration.
    • Branch name: The name of the branch being analyzed.
    • Commit SHA: The unique identifier for the commit.
    • Base branch name: The target branch (for pull requests).
    • Pull request number: The ID of the PR, if applicable.
    • Commit message: Only stored if includeCommitMessage is enabled.
    • Files / Groups: Detailed breakdown including:
      • pattern: The glob pattern used.
      • friendlyName: A custom name if provided.
      • path: The specific file path.
      • size: The size of the file/group.
      • compression: The type of compression applied.
      • size limits: Any limits configured.
  3. Use `<hash>` in file paths for dynamic filenames

    main

    When build tools generate files with unique hashes (e.g., main.a1b2c3d4.js), use the <hash> placeholder in your configuration. This allows BundleMon to track the same logical file across different builds even when the filename changes.

    "bundlemon": {
      "baseDir": "./build",
      "files": [
        {
          "path": "*.<hash>.chunk.js"
        },
        {
          "path": "*.<hash>.js"
        }
      ]
    }
  4. Configure GitHub integration for BundleMon

    main

    To allow BundleMon to post commit statuses or PR comments on GitHub, you must provide a GitHub access token via the BUNDLEMON_GITHUB_TOKEN environment variable in your CI configuration.

    Token Requirements:

    • Create a Personal Access Token (PAT) without any specific scopes.
    • The token owner must have write permissions to the repository where outputs will be posted.
    • The token is used only to verify the username that created it and is not stored by the BundleMon service.
    # Set this in your CI environment variables
    BUNDLEMON_GITHUB_TOKEN=your_github_access_token
  5. Generate a secret session key

    main

    The SECRET_SESSION_KEY is used for securely signing session cookies. While the service auto-generates a key on startup, it is recommended to set a persistent key manually. You can generate a key using the provided script.

    yarn install
    
    # prints the secret key
    node apps/service/scripts/generateSecretKey.js
  6. Set up BundleMon Project ID and API Key

    main

    To save history and compare against base branches, BundleMon requires a project ID.

    1. GitHub Actions: If using the official GitHub App, BundleMon detects this automatically.
    2. Other CI/Manual: Create a project at app.bundlemon.dev and set the following environment variables in your CI:
      • BUNDLEMON_PROJECT_ID
      • BUNDLEMON_PROJECT_APIKEY
  7. Configure custom output implementation

    main

    The custom output plugin allows you to use your own JavaScript implementation to process or output BundleMon results. You must provide a path to a JavaScript file that exports a function. This function receives the generated report as its parameter and can be either synchronous or asynchronous.

    // Configuration
    "reportOutput": [
      [
        "custom",
        {
          "path": "custom-output.js"
        }
      ]
    ]
  8. Configure GitHub report output

    main

    The github output plugin allows BundleMon to create GitHub check runs, post commit statuses, and add detailed comments to Pull Requests. To use it, you must first Install the BundleMon GitHub App.

    In your configuration, reportOutput accepts either a simple string for default settings or an array containing the plugin name and an options object to override defaults.

    // Use default options
    "reportOutput": ["github"]
    
    // Override default options
    "reportOutput": [
      [
        "github",
        {
          "checkRun": false,
          "commitStatus": true,
          "prComment": true
        }
      ]
    ]
  9. Configure MongoDB for BundleMon

    main

    BundleMon requires a MongoDB instance to store data. You can use any of the following:

    • A MongoDB instance running in Docker.
    • A local MongoDB installation on your machine.
    • A managed service like MongoDB Atlas.

    Ensure you provide the connection string to the platform using the MONGO_URL environment variable.

  10. Configure JSON report output

    main

    The json output plugin saves the raw BundleMon results into a JSON file. You can use the default filename or specify a custom one via the fileName option.

    // Use default options (bundlemon-results.json)
    "reportOutput": ["json"]
    
    // Override default options
    "reportOutput": [
      [
        "json",
        {
          "fileName": "fantastic-file-name.json"
        }
      ]
    ]
  11. Set up GitHub integration for BundleMon

    main

    To enable the self-hosted BundleMon service to interact with GitHub, you must create a GitHub App with specific permissions and provide its credentials via environment variables.

    Required GitHub App Permissions

    • Metadata: Read
    • Pull requests: Read & write
    • Checks: Read & write
    • Commit statuses: Read & write

    GitHub App Configuration

    After creating the app in GitHub, provide the following environment variables. Note that for the GITHUB_APP_PRIVATE_KEY, you must replace new lines with \n to ensure it is a valid single-line string.

    | ------------------------ | ------------------------ |
    | GITHUB_APP_ID            | GitHub App ID            |
    | GITHUB_APP_PRIVATE_KEY   | GitHub App private key   |
    | GITHUB_APP_CLIENT_ID     | GitHub App client ID     |
    | GITHUB_APP_CLIENT_SECRET | GitHub App client secret |