Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ViteBuilder: Allow viteFinal to modify the configuration before getOptimizeDeps triggers resolveConfig #29790

Open
wants to merge 3 commits into
base: next
Choose a base branch
from

Conversation

cr7pt0gr4ph7
Copy link

@cr7pt0gr4ph7 cr7pt0gr4ph7 commented Dec 3, 2024

What I did

The getOptimizeDeps(config, ...) functions internally uses vite.resolveConfig(config).createResolver(), which causes problems if some plugin throws an error because the configuration is not valid. This PR defers the getOptimizeDeps invocation until after viteFinal had a chance to fix up the configuration.

The specific issue I encountered was that the Vike framework, which builds upon Vite, does not like it if base is set to "./", while Storybook forces base: "./" and currently invokes getOptimizeDeps before the user has a chance to rectify the issues through viteFinal.

This change should only be observable to consumers in two cases:

  1. getOptimizedDeps failed due to a plugin throwing an error during initialization (so it was broken anyway) because of an invalid configuration.
  2. viteFinal is used to remove dependencies that were added by getOptimizedDeps. This is not possible anymore.
    • An alternative that would also allow this (if deemed necessary) would be to execute viteFinal twice, once to produce the preliminary resolved configuration used by getOptimizedDeps, and the second time on the unresolved preliminary configuration + optimizeDeps to produce the final configuration.

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: All existing tests using vite-builder to build Vite applications will implicitly test the affected code as well.

Manual testing

  1. Run a sandbox for template: yarn task --task dev --start-from=install --template react-vite/default-ts
  2. Open Storybook in your browser
  3. Access any story (e.g. Example/Button/Docs).

Documentation

  • Add or update documentation reflecting your changes: The documentation for viteFinal already aligns with the behaviour implemented here.
  • 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 PR does not have a canary release associated. You can request a canary release of this pull request by mentioning the @storybookjs/core team 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

Reorders Vite server initialization to ensure framework-specific configuration changes are applied before dependency optimization occurs, preventing plugin initialization errors.

  • Modified execution order in code/builders/builder-vite/src/vite-server.ts to defer getOptimizeDeps until after viteFinal runs
  • Fixes compatibility with Vike framework which requires base configuration different from Storybook's default
  • Prevents plugin errors that could occur when resolveConfig is called before configuration is fully prepared
  • Only impacts cases where plugins throw initialization errors or when viteFinal removes dependencies

💡 (1/5) You can manually trigger the bot by mentioning @greptileai in a comment!

@cr7pt0gr4ph7 cr7pt0gr4ph7 force-pushed the fix-vite-final-optimize-deps-order branch from 2a8c9b8 to 88a9c55 Compare December 3, 2024 13:04
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

LGTM

1 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings | Greptile

@cr7pt0gr4ph7 cr7pt0gr4ph7 force-pushed the fix-vite-final-optimize-deps-order branch from 88a9c55 to 5f48f08 Compare December 3, 2024 13:04
@cr7pt0gr4ph7 cr7pt0gr4ph7 force-pushed the fix-vite-final-optimize-deps-order branch from 5f48f08 to ce9f6b1 Compare December 3, 2024 13:05
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

LGTM

1 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings | Greptile

Copy link

nx-cloud bot commented Dec 9, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 15a62ef. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 1 target

Sent with 💌 from NxCloud.

@jonniebigodes
Copy link
Contributor

@cr7pt0gr4ph7, thank you for putting together this pull request and helping us improve Storybook. We appreciate it 🙏 !

@IanVS do you mind looking into this as well when you're able?

Thanks in advance

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.

3 participants