Skip to content

Vite: Add mock entries to optimizeDeps.entries#34167

Merged
valentinpalkovic merged 1 commit into
nextfrom
valentin/support-optimize-deps-for-mocks
Mar 17, 2026
Merged

Vite: Add mock entries to optimizeDeps.entries#34167
valentinpalkovic merged 1 commit into
nextfrom
valentin/support-optimize-deps-for-mocks

Conversation

@valentinpalkovic
Copy link
Copy Markdown
Contributor

@valentinpalkovic valentinpalkovic commented Mar 16, 2026

Closes #

What I did

Added support in the Vite optimize-deps plugin to include manual sb.mock(...) redirect targets (files in __mocks__) in optimizeDeps.include, so dependencies imported by those mock files are pre-bundled correctly.

Specifically:

  • Reused existing mock extraction/resolution utilities (extractMockCalls, findMockRedirect) in the optimize-deps plugin.
  • Added a helper that derives include entries from redirectPath only, escapes glob-sensitive characters, and deduplicates entries.
  • Merged derived mock entries into optimizeDeps.include while preserving existing include ordering.
  • Added focused unit tests for include derivation behavior.

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

Caution

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

No additional manual testing was performed. This change is covered by focused unit tests and validation commands (nx compile, nx check, targeted vitest, and lint).

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 pull request has been released as version 0.0.0-pr-34167-sha-d83eb160. Try it out in a new sandbox by running npx storybook@0.0.0-pr-34167-sha-d83eb160 sandbox or in an existing project with npx storybook@0.0.0-pr-34167-sha-d83eb160 upgrade.

More information
Published version 0.0.0-pr-34167-sha-d83eb160
Triggered by @valentinpalkovic
Repository storybookjs/storybook
Branch valentin/support-optimize-deps-for-mocks
Commit d83eb160
Datetime Mon Mar 16 22:08:39 UTC 2026 (1773698919)
Workflow run 23168229627

To request a new release of this pull request, mention the @storybookjs/core team.

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

Summary by CodeRabbit

  • Refactor

    • Enhanced mock redirect processing in Storybook's Vite dependency optimization for improved build handling.
  • Tests

    • Added comprehensive test suite for mock redirect functionality, validating deduplication, escaping, and null-path filtering.

@nx-cloud
Copy link
Copy Markdown

nx-cloud Bot commented Mar 16, 2026

View your CI Pipeline Execution ↗ for commit d83eb16

Command Status Duration Result
nx run-many -t compile -c production --parallel=1 ✅ Succeeded 5m 9s View ↗

☁️ Nx Cloud last updated this comment at 2026-03-16 22:13:23 UTC

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 16, 2026

📝 Walkthrough

Walkthrough

A new utility function getMockRedirectIncludeEntries is introduced to convert mock redirect paths into escaped glob entries for Vite optimization. The function is integrated into the Storybook Vite plugin to include mock redirects in the optimize-deps configuration during development, with comprehensive test coverage added.

Changes

Cohort / File(s) Summary
Mock Redirect Optimization
code/builders/builder-vite/src/plugins/storybook-optimize-deps-plugin.ts
Added exported getMockRedirectIncludeEntries utility function that filters null entries, escapes special glob characters, and deduplicates redirect paths. Integrated into the plugin to extract mock redirects from preview/config files and merge them into optimizeDeps.entries alongside existing entries.
Mock Redirect Tests
code/builders/builder-vite/src/plugins/storybook-optimize-deps-plugin.test.ts
Added test suite for getMockRedirectIncludeEntries validating non-null filtering, glob character escaping, and path deduplication behavior.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
📝 Coding Plan
  • Generate coding plan for human review comments

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

Tip

CodeRabbit can use TruffleHog to scan for secrets in your code with verification capabilities.

Add a TruffleHog config file (e.g. trufflehog-config.yml, trufflehog.yml) to your project to customize detectors and scanning behavior. The tool runs only when a config file is present.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
code/builders/builder-vite/src/plugins/storybook-optimize-deps-plugin.ts (1)

60-73: Consider whether disableTelemetry should follow options configuration.

The disableTelemetry: true is hardcoded here. If this is intentional (e.g., to avoid telemetry during dep optimization which runs frequently), a brief comment explaining the rationale would be helpful for future maintainers.

💡 Suggested documentation
       const mockRedirectIncludeEntries = previewOrConfigFile
         ? getMockRedirectIncludeEntries(
             extractMockCalls(
               {
                 previewConfigPath: previewOrConfigFile,
+                // Telemetry is disabled here to avoid excessive telemetry events during
+                // frequent dep optimization passes in development.
                 coreOptions: { disableTelemetry: true },
                 configDir: options.configDir,
               },
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@code/builders/builder-vite/src/plugins/storybook-optimize-deps-plugin.ts`
around lines 60 - 73, The code hardcodes disableTelemetry: true in the
extractMockCalls call (within mockRedirectIncludeEntries creation). Replace the
literal with a value derived from the passed-in options (e.g., disableTelemetry:
options.disableTelemetry ?? true) so telemetry follows the caller's
configuration, or if the intent is to always disableTelemetry here, add a short
clarifying comment above the getMockRedirectIncludeEntries/extractMockCalls call
explaining why telemetry must be forced off during dep optimization.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@code/builders/builder-vite/src/plugins/storybook-optimize-deps-plugin.ts`:
- Around line 60-73: The code hardcodes disableTelemetry: true in the
extractMockCalls call (within mockRedirectIncludeEntries creation). Replace the
literal with a value derived from the passed-in options (e.g., disableTelemetry:
options.disableTelemetry ?? true) so telemetry follows the caller's
configuration, or if the intent is to always disableTelemetry here, add a short
clarifying comment above the getMockRedirectIncludeEntries/extractMockCalls call
explaining why telemetry must be forced off during dep optimization.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1da82216-1fc7-45b2-aa7c-e2e37c68ad18

📥 Commits

Reviewing files that changed from the base of the PR and between 546aece and d83eb16.

📒 Files selected for processing (2)
  • code/builders/builder-vite/src/plugins/storybook-optimize-deps-plugin.test.ts
  • code/builders/builder-vite/src/plugins/storybook-optimize-deps-plugin.ts

@storybook-app-bot
Copy link
Copy Markdown

Package Benchmarks

Commit: d83eb16, ran on 16 March 2026 at 22:11:33 UTC

The following packages have significant changes to their size or dependencies:

@storybook/nextjs

Before After Difference
Dependency count 534 534 0
Self size 650 KB 650 KB 🎉 -10 B 🎉
Dependency size 59.93 MB 59.95 MB 🚨 +19 KB 🚨
Bundle Size Analyzer Link Link

@storybook/preset-create-react-app

Before After Difference
Dependency count 68 68 0
Self size 32 KB 32 KB 🎉 -4 B 🎉
Dependency size 6.00 MB 6.01 MB 🚨 +10 KB 🚨
Bundle Size Analyzer Link Link

@valentinpalkovic valentinpalkovic merged commit 08fc2ea into next Mar 17, 2026
135 of 143 checks passed
@valentinpalkovic valentinpalkovic deleted the valentin/support-optimize-deps-for-mocks branch March 17, 2026 17:25
@github-actions github-actions Bot mentioned this pull request Mar 17, 2026
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants