fix(builder-vite): disable Vite's publicDir to prevent double-copying static assets#34861
Conversation
… static assets
When a user configures `staticDirs` with a custom destination (e.g.
`{ from: '../public', to: '/foo' }`), Vite's own `publicDir` mechanism
was independently copying `../public` to the build root in addition to
wherever `staticDirs` directed it. This caused `../public/index.json`
to overwrite the Storybook-generated `index.json`, breaking builds.
Set `publicDir: false` in the shared Vite config so Storybook owns all
static asset copying via `staticDirs`. Export a `staticDirs` preset from
`@storybook/builder-vite` that mirrors the original Vite default behavior
(copy `../public` to `/`) when the directory exists and the user has not
already mapped it to a custom destination. This keeps existing projects
working without any config changes.
Fixes storybookjs#24627
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThe PR reconfigures how Storybook's Vite builder handles static files. Vite's default ChangesStatic directory handling with Vite
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Superseded by #33703 |
Summary
publicDirmechanism independently copies../publicto the build output root. When a user routes that same directory to a custom destination viastaticDirs, it ends up copied twice — once by Vite to/, once by Storybook to the configured path. If../public/index.jsonexists, it silently overwrites the Storybook-generatedindex.json, breaking story discovery.publicDir: falsein Storybook's shared Vite config (vite-config.ts) so Storybook owns all static file copying.staticDirspreset from@storybook/builder-vitethat re-adds../public → /when the directory exists and the user hasn't already mapped it elsewhere. Existing projects get identical behavior without any config changes.Changes
code/builders/builder-vite/src/vite-config.ts— addpublicDir: falsetosbConfigcode/builders/builder-vite/src/preset.ts— exportstaticDirsthat mirrors Vite's default public dir behavior via Storybook's own copy mechanismTest plan
public/index.jsonwith{}staticDirs: [{ from: '../public', to: '/foo' }]in.storybook/main.tsbuild-storybook—storybook-static/index.jsonshould contain valid story index data, not{}storybook-static/foo/should contain the public directory contentsstaticDirsconfig:../publicstill served at/(backwards compat)staticDirs: ['../public']: no duplicate copy, no regressionCloses #24627
Summary by CodeRabbit
staticDirsconfiguration, with Vite's public directory disabled to avoid conflicts.