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

Addon-docs: Fix MDX compilation with @vitejs/plugin-react-swc and plugins #26837

Conversation

JReinhold
Copy link
Contributor

@JReinhold JReinhold commented Apr 14, 2024

Closes #24857

What I did

Moved the MDX Vite plugin to the start of the plugins-array instead of the end.

The MDX plugin must run before any React plugins to work. In 99 % of the cases it is because we have enforce: 'pre' defined in the plugin. However when:

  1. Using @vitejs/plugin-react-swc
  2. And have defined plugins for that Vite plugin with eg. react({ plugins: [] })

Our MDX plugin is runned after @vitejs/plugin-react-swc, causing build (but now dev) to crash.

This is because we get into this case in @vitejs/plugin-react-swc, where they also add enforce: 'pre', causing it to run before our MDX plugin. The fix is to add it to the beginning of the array instead of the end. Also see this condition in @vitejs/plugin-react-swc where they explicitly check for this case, except with the official MDX Vite plugin and not ours of course.

I can't come up with any downsides to this fix, as this shouldn't really change anything for most users AFAIK. I've tested it in basic situations with React+Vite without issues.

Side-note: I wonder if in the future we could just use the MDX Vite plugin directly instead of creating our own (extremely thin) wrapper around the low-level MDX compiler. 🤔 I can imagine it solving the problem better, ie. supporting source maps.

Checklist for Contributors

Testing

I think the only way to truly test this would be to have a sandbox with this scenario, but that's definitely not worth it.

The changes in this PR are covered in the following automated tests:

  • stories
  • unit tests
  • integration tests
  • end-to-end tests

Manual testing

  1. Create a new Vite project with npm create vite
  2. Choose "React" and "TypeScript + SWC"
  3. Init Storybook in the project
  4. Edit vite.config.ts, change the line plugins: [react()], to plugins: [react({ plugins: [] })],
  5. Build the Storybook
  6. See it fail with the latest version
  7. Install this canary of Storybook
  8. Build again
  9. See it succeeds
  10. Serve with npx http-serve storybook-static -c-1h
  11. See that the "Configure..." MDX page works

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/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-26837-sha-6fdb569c. Try it out in a new sandbox by running npx storybook@0.0.0-pr-26837-sha-6fdb569c sandbox or in an existing project with npx storybook@0.0.0-pr-26837-sha-6fdb569c upgrade.

More information
Published version 0.0.0-pr-26[8](https://github.com/storybookjs/storybook/actions/runs/8680911801/job/23802522844#step:10:8)37-sha-6fdb56[9](https://github.com/storybookjs/storybook/actions/runs/8680911801/job/23802522844#step:10:9)c
Triggered by @JReinhold
Repository storybookjs/storybook
Branch jeppe/24857-bug-mdx-compilation-breaks-when-using-vitejsplugin-react-swc-and-extending-its-plugins
Commit 6fdb569c
Datetime Sun Apr 14 19:39:24 UTC 2024 (1713123564)
Workflow run [8680911801](https://github.com/storybookjs/storybook/actions/runs/8680911801)

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 canary-release-pr.yml --field pr=26837

@JReinhold JReinhold added bug patch:yes Bugfix & documentation PR that need to be picked to main branch addon: docs builder-vite ci:normal labels Apr 14, 2024
@JReinhold JReinhold self-assigned this Apr 14, 2024
Copy link

nx-cloud bot commented Apr 14, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 6fdb569. 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 2 targets

Sent with 💌 from NxCloud.

Copy link
Contributor

@valentinpalkovic valentinpalkovic left a comment

Choose a reason for hiding this comment

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

Code-wise LGTM!

@JReinhold JReinhold merged commit d1b4753 into next Apr 15, 2024
66 of 73 checks passed
@JReinhold JReinhold deleted the jeppe/24857-bug-mdx-compilation-breaks-when-using-vitejsplugin-react-swc-and-extending-its-plugins branch April 15, 2024 18:44
storybook-bot pushed a commit that referenced this pull request Apr 15, 2024
…ation-breaks-when-using-vitejsplugin-react-swc-and-extending-its-plugins

Addon-docs: Fix MDX compilation when using `@vitejs/plugin-react-swc` with plugins
(cherry picked from commit d1b4753)
storybook-bot pushed a commit that referenced this pull request Apr 16, 2024
…ation-breaks-when-using-vitejsplugin-react-swc-and-extending-its-plugins

Addon-docs: Fix MDX compilation when using `@vitejs/plugin-react-swc` with plugins
(cherry picked from commit d1b4753)
storybook-bot pushed a commit that referenced this pull request Apr 19, 2024
…ation-breaks-when-using-vitejsplugin-react-swc-and-extending-its-plugins

Addon-docs: Fix MDX compilation when using `@vitejs/plugin-react-swc` with plugins
(cherry picked from commit d1b4753)
@shilman shilman changed the title Addon-docs: Fix MDX compilation when using @vitejs/plugin-react-swc with plugins Addon-docs: Fix MDX compilation with @vitejs/plugin-react-swc and plugins Apr 21, 2024
@github-actions github-actions bot added the patch:done Patch/release PRs already cherry-picked to main/release branch label Apr 22, 2024
@storybook-bot
Copy link
Contributor

Failed to publish canary version of this pull request, triggered by @JReinhold. See the failed workflow run at: https://github.com/storybookjs/storybook/actions/runs/8943682626

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addon: docs bug builder-vite ci:normal empathy patch:done Patch/release PRs already cherry-picked to main/release branch patch:yes Bugfix & documentation PR that need to be picked to main branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: MDX Compilation breaks when using @vitejs/plugin-react-swc and extending its plugins
3 participants