Skip to content

fix(builder-vite): avoid duplicate publicDir copies with staticDirs#34524

Open
harshit-d3v wants to merge 1 commit intostorybookjs:nextfrom
harshit-d3v:fix/vite-publicdir-staticdirs
Open

fix(builder-vite): avoid duplicate publicDir copies with staticDirs#34524
harshit-d3v wants to merge 1 commit intostorybookjs:nextfrom
harshit-d3v:fix/vite-publicdir-staticdirs

Conversation

@harshit-d3v
Copy link
Copy Markdown

@harshit-d3v harshit-d3v commented Apr 11, 2026

Summary

  • disable Vite's build-time publicDir copy when Storybook staticDirs already includes the same directory
  • prevent duplicate root-level asset copies during static builds for Vite-based Storybook projects
  • avoid cases where files such as public/index.json can overwrite Storybook's generated storybook-static/index.json
  • add regression tests covering default and custom publicDir values, plus build vs development behavior

Closes #24627.

Testing

  • yarn vitest run code/builders/builder-vite/src/vite-config.test.ts

Summary by CodeRabbit

  • Tests

    • Added test coverage for Vite builder's public directory handling with static directories in build and development modes.
  • Bug Fixes

    • Improved Vite builder's public directory configuration based on static directory mappings during production builds.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 11, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0034c793-ce8c-49cc-94b2-224a39da99fa

📥 Commits

Reviewing files that changed from the base of the PR and between aae6c95 and dd73cf4.

📒 Files selected for processing (2)
  • code/builders/builder-vite/src/vite-config.test.ts
  • code/builders/builder-vite/src/vite-config.ts

📝 Walkthrough

Walkthrough

These changes add logic to Storybook's Vite builder to conditionally disable Vite's publicDir during production builds when user-defined staticDirs already map to the same resolved directory. Tests verify this behavior across different build modes and configurations.

Changes

Cohort / File(s) Summary
Core Implementation
code/builders/builder-vite/src/vite-config.ts
Adds helper functions to resolve effective publicDir from Vite config and check if staticDirs mappings match it. Sets publicDir: false in build mode when static dir mapping aligns with resolved publicDir.
Test Additions
code/builders/builder-vite/src/vite-config.test.ts
Introduces test helpers (createOptions, createTempProject) and four test cases validating commonConfig behavior with publicDir disabled in build mode when staticDirs maps to the same directory, and enabled in other scenarios.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@valentinpalkovic
Copy link
Copy Markdown
Contributor

Hi @harshit-d3v,

Due to a recent high volume of unreviewed AI-generated PRs, we are requesting verification and proof that the implemented fix actually works. Please provide a simple GIF/Video or image of how the fix works, optimally with before-and-after comparisons.

Thank you for your understanding!

@harshit-d3v
Copy link
Copy Markdown
Author

harshit-d3v commented Apr 14, 2026

Screenshot 2026-04-14 161427

Hi @valentinpalkovic, thanks for flagging this.

I verified this locally and attached a simple before/after screenshot.

This PR fixes the case where Vite's publicDir and Storybook staticDirs point to the same directory during a static build. Before this change, both copy steps could run, which could duplicate root-level assets and potentially overwrite generated files like storybook-static/index.json.

After this fix, Storybook disables Vite's publicDir copy in build mode when staticDirs already maps to that same directory, so the assets are copied only once. Development behavior stays unchanged, and non-matching staticDirs still keep publicDir enabled.

I also verified the regression tests locally with yarn vitest run code/builders/builder-vite/src/vite-config.test.ts.

Result: 1 passed, 9 tests passed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Human verification

Development

Successfully merging this pull request may close these issues.

[Bug]: "../public" is always copied in the static build root even if I set it to go elsewhere

2 participants