Builder-Vite: Use preview annotations as entry points for optimizeDeps#33875
Conversation
|
View your CI Pipeline Execution ↗ for commit ca461fa
☁️ Nx Cloud last updated this comment at |
|
Failed to publish canary version of this pull request, triggered by @yannbf. See the failed workflow run at: https://github.com/storybookjs/storybook/actions/runs/22188806376 |
|
@copilot this mostly worked but there are parts which you didn't catch. When running Storybook and watching its output you can see an optimization message for How can we avoid missing these, and are there perhaps more entries which are required here? |
These two packages were missed because they appear in contexts Vite's dep scanner can't statically analyze:
The systematic rule to catch these: check virtual module code generators ( After auditing all virtual module generators and dynamic imports, these two are the only cases that need explicit The fixes from commit |
|
Great @copilot but how do we make sure we don't regress? Is there a way to automatically catch cases which are not covered? Can you write a test that would have caught this? |
Added two regression tests in commit 1. Systematic test in Generates all virtual module code ( 2. Focused regression test in Asserts This covers the two identified patterns that Vite cannot discover statically: imports in virtual module string templates, and |
Package BenchmarksCommit: The following packages have significant changes to their size or dependencies:
|
| Before | After | Difference | |
|---|---|---|---|
| Dependency count | 20 | 20 | 0 |
| Self size | 131 KB | 131 KB | 0 B |
| Dependency size | 3.40 MB | 3.42 MB | 🚨 +20 KB 🚨 |
| Bundle Size Analyzer | Link | Link |
…ove INCLUDE_CANDIDATES Co-authored-by: yannbf <1671563+yannbf@users.noreply.github.com>
…timizeDeps coverage Co-authored-by: yannbf <1671563+yannbf@users.noreply.github.com>
9e00f92 to
e96bd64
Compare
NOTE: This has been reviewed, tested and edited by my beloved humans @yannbf and @valentinpalkovic. GitHub should give them a prize, that's all I'll say!
Vite's dep optimizer was only scanning story files as entry points, missing preview annotation files (user's
preview.ts, addon/framework/renderer previews). This caused runtime CJS dependency discovery, triggering hard reloads in Storybook and flaky test reruns in Vitest.The real fix is to give Vite the complete set of entry points upfront so it can crawl all transitive dependencies before serving.
Changes
storybook-optimize-deps-plugin.tsplugin now figures out all of the preview annotations and adds them as entries in the vite config.storybook/internal/preview/runtimebecause it's only imported inside a string template incodegen-modern-iframe-script.ts(the virtual iframe entry) and therefore can't be crawled ahead of time.react-dom/test-utilsbecause its dynamically imported inact-compat.ts, which Vite's static analyzer does not pre-bundle.The fix applies to both
@storybook/builder-viteand@storybook/addon-vitestsincestorybookOptimizeDepsPluginis part of the sharedviteCorePlugins.Manual testing
lodashin the .storybook/preview fileOriginal prompt
This section details on the original issue you should resolve
<issue_title>[Bug]: Revamp Vite and Vitest dependency optimization</issue_title>
<issue_description>### Describe the bug
Problem
We currently have specific code which handles dependency optimization in the Vite builder as can be seen here. That code is outdated and might not be necessary.
When the Vite dependency optimization kicks in, it needs to hard reload the browser. This causes two things:
Storybook already adds stories as
entriesto Vite's config, which makes it scan most files to do dependency optimization automatically (great). However, currently Storybook does not add other necessary files as entries, such as the preview annotation files. For instance, if a.storybook/preview.tsxfile has imports like:Then such dependencies might need to be optimized (if they're CJS for instance) and therefore this will trigger the warnings. This would not have been the case had Storybook added the preview file as an entry to Vite. The same issue also occurs from packages, such as renderers, frameworks or addons. Such packages can use a concept of a
preview annotationorpreset, which adds annotations to Storybook (such as decorators, loaders, etc). These annotations can also end up making imports of dependencies that would cause the same problem.This is a quite critical change that requires a lot of checking, but worth investigating. This solution will close many issues, including #33067, #32049, #31119 as well as provide a much better experience for end users.
The workaround
When this behavior occurs, users see, both in Storybook's CLI and in Vitest's CLI some warnings like this:
So to solve this issue, users need to add a
viteFinalconfig in the Storybook main file and specify their own dependencies in itsoptimizeDeps.includefield:Potential solution
Vite automatically handles dependency optimization based on the entrypoints passed into the
entries, so theoretically we don't need to handle dependency optimization ourselves, as long as we specify the correct entries.Vitest has similar challenges and solves it by doing this, it basically adds all test files + setup files (plus some internal utilities) as entries, and Vite will figure out what needs to be optimized. In Storybook, the scenario is similar, we need to list the stories files as entries, as well as the preview entries (user's preview file, plus addon/renderer/frameworks preview annotations which come from presets or preview files).
</issue_description>
<agent_instructions>There was a recent change in the repo to centralize core vite plugins and that is reused between storybook proper and the Vitest integration. The solution you come up with should work for them both then.
Also, this issue is not necessarily a problem for CSF4 (known as csf factories) users, it's mostly a problem for CSF3 and below.
The main goal of this task should be to improve and simplify the dependency optimization logic, which will make the INCLUDE_CANDIDATES in code/...
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.
Summary by CodeRabbit
New Features
Chores
Refactor