Build: Support big metafiles in bundle analyzer#32109
Conversation
| 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); |
There was a problem hiding this comment.
the diff is annoying because the file was moved, this is the important new part
|
View your CI Pipeline Execution ↗ for commit 0825667
☁️ Nx Cloud last updated this comment at |
There was a problem hiding this comment.
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 |
| /* | ||
| 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); | ||
| } |
There was a problem hiding this comment.
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
Package BenchmarksCommit: No significant changes detected, all good. 👏 |
…ame, so that it matches {projectName} in NX config and is correctly calculated as part of the nx output cache
| features: { | ||
| developmentModeForBuild: true, | ||
| }, | ||
| staticDirs: [{ from: './bench/bundle-analyzer', to: '/bundle-analyzer' }], |
Works on #31787
Replaces #31644 (which it takes inspiration from)
What I did
This PR improves the metafile analysis:
postbuildscripts never being executedChecklist for Contributors
Testing
The changes in this PR are covered in the following automated tests:
Manual testing
This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!
Documentation
MIGRATION.MD
Checklist for Maintainers
When this PR is ready for testing, make sure to add
ci:normal,ci:mergedorci:dailyGH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found incode/lib/cli-storybook/src/sandbox-templates.tsMake 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/coreteam 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:
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 visualizationsEnhanced Story Configuration: Refactors the benchmark stories to use a
beforeEachhook instead of loaders, setting metafile data onglobalThisfor iframe communication rather than base64-encoding into URLsBuild System Improvements: Removes problematic postbuild scripts from multiple packages (
cli-storybook,create-storybook,core) that were never executing properly, streamlining the build processConfiguration Updates: Modifies Vite builder to use configurable
options.configDirinstead of hardcoded.storybook, and updates Storybook's main config to serve the bundle analyzer UI as static filesThe 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
code/core/build-config.ts,code/lib/cli-storybook/build-config.ts, andcode/lib/create-storybook/build-config.tsneed more attention due to removed executable permission handling