Skip to content

Build: Support big metafiles in bundle analyzer#32109

Merged
ndelangen merged 10 commits into
sb10/esm-onlyfrom
sonda-analyzer
Jul 24, 2025
Merged

Build: Support big metafiles in bundle analyzer#32109
ndelangen merged 10 commits into
sb10/esm-onlyfrom
sonda-analyzer

Conversation

@JReinhold
Copy link
Copy Markdown
Contributor

@JReinhold JReinhold commented Jul 23, 2025

Works on #31787

Replaces #31644 (which it takes inspiration from)

What I did

This PR improves the metafile analysis:

  1. Refactor the bundle generation script
  2. Fix postbuild scripts never being executed
  3. Change logic so that metafiles are also continously generated in watch mode
  4. Refactor the metafile analysis stories, to have a local version of the UI instead of relying on the one hosted at https://esbuild.github.io/analyze . This means that there is now no limit to the size of metafiles we can analyze, as the metafile is read directly from the iframe instead of via query strings

Checklist for Contributors

Testing

The changes in this PR are covered in the following automated tests:

  • stories
  • unit tests
  • integration tests
  • end-to-end tests

Manual testing

This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!

Documentation

  • Add or update documentation reflecting your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Checklist for Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli-storybook/src/sandbox-templates.ts

  • Make sure this PR contains one of the labels below:

    Available labels
    • bug: Internal changes that fixes incorrect behavior.
    • maintenance: User-facing maintenance tasks.
    • dependencies: Upgrading (sometimes downgrading) dependencies.
    • build: Internal-facing build tooling & test updates. Will not show up in release changelog.
    • cleanup: Minor cleanup style change. Will not show up in release changelog.
    • documentation: Documentation only changes. Will not show up in release changelog.
    • feature request: Introducing a new feature.
    • BREAKING CHANGE: Changes that break compatibility in some way with current major version.
    • other: Changes that don't fit in the above categories.

🦋 Canary release

This PR does not have a canary release associated. You can request a canary release of this pull request by mentioning the @storybookjs/core team here.

core team members can create a canary release here or locally with gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=<PR_NUMBER>

Greptile Summary

This PR fundamentally transforms Storybook's bundle analyzer to handle large metafiles by replacing the external esbuild analyzer dependency with a local implementation. The key architectural change moves from using URL query strings (which have size limitations) to direct iframe communication via globalThis.metafile.

The implementation includes:

  1. Local Bundle Analyzer: Creates a complete local copy of esbuild's analyzer UI (code/.storybook/bench/bundle-analyzer/) with HTML, CSS, and JavaScript files that provide treemap, sunburst, and flame chart visualizations

  2. Enhanced Story Configuration: Refactors the benchmark stories to use a beforeEach hook instead of loaders, setting metafile data on globalThis for iframe communication rather than base64-encoding into URLs

  3. Build System Improvements: Removes problematic postbuild scripts from multiple packages (cli-storybook, create-storybook, core) that were never executing properly, streamlining the build process

  4. Configuration Updates: Modifies Vite builder to use configurable options.configDir instead of hardcoded .storybook, and updates Storybook's main config to serve the bundle analyzer UI as static files

The solution elegantly sidesteps the 2MB URL limitation that prevented analysis of large metafiles, enabling developers to analyze bundle statistics of any size within Storybook's development environment. This integrates with the existing metafile generation system while providing a more robust, self-contained solution.

Confidence score: 3/5

  • This PR introduces significant architectural changes that should work well but may have edge cases or integration issues
  • The removal of postbuild scripts that set executable permissions could break CLI functionality if not handled elsewhere
  • Files code/core/build-config.ts, code/lib/cli-storybook/build-config.ts, and code/lib/create-storybook/build-config.ts need more attention due to removed executable permission handling

@JReinhold JReinhold self-assigned this Jul 23, 2025
@JReinhold JReinhold added build Internal-facing build tooling & test updates ci:normal Run our default set of CI jobs (choose this for most PRs). labels Jul 23, 2025
@JReinhold JReinhold requested review from Copilot and ndelangen July 23, 2025 09:24
@JReinhold JReinhold marked this pull request as ready for review July 23, 2025 09:24
Comment on lines +44 to +46
const metafile = await allMetafiles[args.metafile]();
// this is read by the bundle-analyzer iframe via parent.metafile, in bundle-analyzer/index.js
globalThis.metafile = JSON.stringify(metafile);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the diff is annoying because the file was moved, this is the important new part

@nx-cloud
Copy link
Copy Markdown

nx-cloud Bot commented Jul 23, 2025

View your CI Pipeline Execution ↗ for commit 0825667

Command Status Duration Result
nx run-many -t build --parallel=3 ✅ Succeeded 48s View ↗

☁️ Nx Cloud last updated this comment at 2025-07-23 19:10:44 UTC

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves the metafile analysis functionality by refactoring the bundle generation script and implementing a local version of the esbuild analyzer UI. The changes enable support for large metafiles by eliminating the URL size limitations of the previous iframe-based approach that relied on the hosted esbuild analyzer.

  • Refactored bundle generation to fix postbuild script execution and generate metafiles in watch mode
  • Implemented local esbuild analyzer UI to replace external dependency on esbuild.github.io
  • Updated Storybook configuration to serve the local analyzer and reorganized story structure

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
scripts/build/utils/generate-bundle.ts Major refactoring to fix postbuild execution and add metafile generation plugin
scripts/build/utils/entry-utils.ts Added EntryType export and refactored type definitions
code/lib/create-storybook/build-config.ts Removed chmod postbuild configuration
code/lib/cli-storybook/build-config.ts Removed chmod postbuild configuration
code/core/build-config.ts Removed chmod postbuild configuration
code/builders/builder-vite/src/vite-config.ts Updated to use configDir instead of hardcoded '.storybook'
code/.storybook/main.ts Updated stories path and added staticDirs configuration
code/.storybook/bench/bundle-analyzer/index.js Added local esbuild analyzer implementation
code/.storybook/bench/bundle-analyzer/index.html Added HTML structure for local analyzer
code/.storybook/bench/bundle-analyzer/index.css Added styles for local analyzer
code/.storybook/bench/bench.stories.tsx New story file with updated metafile loading approach
code/.storybook/bench.stories.tsx Removed old bench stories file

Comment thread scripts/build/utils/generate-bundle.ts
Comment thread scripts/build/utils/generate-bundle.ts
Comment thread code/.storybook/bench/bundle-analyzer/index.js
Comment thread code/.storybook/bench/bench.stories.tsx
Comment thread code/.storybook/bench/bench.stories.tsx
Copy link
Copy Markdown
Contributor

@greptile-apps greptile-apps Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 files reviewed, 1 comment

Edit Code Review Bot Settings | Greptile

Comment thread code/.storybook/bench/bench.stories.tsx Outdated
Comment on lines +2066 to +2072
/*
Additional code for Storybook to load the metafile from the parent window.
The story sets this global variable to the metafile in a beforeEach hook.
*/
if (parent.metafile) {
et(parent.metafile);
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the part that loads the metafile in the analyzer. an iframe can access its parent's scope as long as it's same-origin

@storybook-app-bot
Copy link
Copy Markdown

storybook-app-bot Bot commented Jul 23, 2025

Package Benchmarks

Commit: 0825667, ran on 23 July 2025 at 18:56:34 UTC

No significant changes detected, all good. 👏

Comment thread code/.storybook/main.ts
features: {
developmentModeForBuild: true,
},
staticDirs: [{ from: './bench/bundle-analyzer', to: '/bundle-analyzer' }],
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

smart!

@ndelangen ndelangen merged commit dc7e86b into sb10/esm-only Jul 24, 2025
51 of 53 checks passed
@ndelangen ndelangen deleted the sonda-analyzer branch July 24, 2025 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build Internal-facing build tooling & test updates ci:normal Run our default set of CI jobs (choose this for most PRs).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants