Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 8, 2025

Closes #913

Ecosystem ci run

copilot

Summary

This PR enables @vitejs/plugin-rsc to automatically detect and use react-server-dom-webpack from the user's project dependencies when available, while maintaining backward compatibility with the vendored version.

Motivation

Previously, the plugin always used a bundled (vendored) version of react-server-dom-webpack. This meant:

  • Users couldn't stay up-to-date with the latest React Server Components runtime without waiting for plugin updates
  • No flexibility to use specific versions for compatibility or testing
  • Potential version conflicts between the vendored and user's React versions

Changes

1. Dynamic Package Detection

Added getReactServerDomPackageName() function that:

  • Attempts to resolve react-server-dom-webpack from the user's project root
  • Falls back to the vendored version (@vitejs/plugin-rsc/vendor/react-server-dom) if not found
  • Uses Node's createRequire to check from the project's package.json location
function getReactServerDomPackageName(root: string): string {
  try {
    const userRequire = createRequire(path.join(root, 'package.json'))
    userRequire.resolve('react-server-dom-webpack/package.json')
    return 'react-server-dom-webpack'
  } catch {
    return '@vitejs/plugin-rsc/vendor/react-server-dom'
  }
}

2. Import Aliasing

Created rsc:react-server-dom-alias Vite plugin that:

  • Intercepts imports of @vitejs/plugin-rsc/vendor/react-server-dom/*
  • Redirects to user's react-server-dom-webpack when detected
  • Preserves subpath imports (e.g., /client.browser, /server.edge)

3. Configuration Updates

Modified Vite environment configs to:

  • Dynamically determine the package name at configuration time
  • Update optimizeDeps.include for all environments (client, ssr, rsc)
  • Use the appropriate package based on detection result

4. Package Metadata

  • Added peerDependenciesMeta marking react-server-dom-webpack as optional
  • This prevents npm/pnpm warnings when users don't install their own version

5. Documentation

Updated README with:

  • Clear explanation of the feature and its benefits
  • Installation instructions for users who want to use their own version
  • Note about backward compatibility

Usage

Users can now optionally install react-server-dom-webpack:

npm install react-server-dom-webpack
# or
pnpm add react-server-dom-webpack

The plugin will automatically detect and use it. No configuration needed.

If not installed, the plugin continues to work with the vendored version (zero breaking changes).

Benefits

  • Up-to-date: Users can stay current with React Server Components runtime
  • Flexible: Allows using specific versions for compatibility needs
  • Backward Compatible: No breaking changes; vendored version works by default
  • Zero Config: Automatic detection; no user configuration required
  • Minimal Changes: Surgical implementation focused only on the necessary changes

Testing

  • ✅ Plugin builds successfully without errors
  • ✅ Resolution logic verified for both scenarios (with/without user package)
  • ✅ Example project builds successfully using vendored version
  • ✅ Vendored files correctly included in distribution
  • ✅ Backward compatibility maintained

[!WARNING]

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/repos/vitejs/vite-plugin-react/issues/913
    • Triggering command: curl -s REDACTED (http block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

fix @vitejs/vite-plugin-react/issues/913


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits October 8, 2025 06:16
- Add getReactServerDomPackageName() helper to detect user's package
- Create alias plugin to redirect vendored imports to user's package
- Update config to use dynamic package name in optimizeDeps

Co-authored-by: hi-ogawa <4232207+hi-ogawa@users.noreply.github.com>
…-dom-webpack

- Add peerDependenciesMeta to mark react-server-dom-webpack as optional
- Add documentation section explaining the dynamic resolution feature

Co-authored-by: hi-ogawa <4232207+hi-ogawa@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix issue with Vite plugin React causing build errors fix(plugin-rsc): dynamically use user's react-server-dom-webpack when available Oct 8, 2025
Copilot AI requested a review from hi-ogawa October 8, 2025 06:25
Copilot finished work on behalf of hi-ogawa October 8, 2025 06:25
@hi-ogawa hi-ogawa changed the title fix(plugin-rsc): dynamically use user's react-server-dom-webpack when available fix(plugin-rsc): dynamically use user's react-server-dom-webpack when available Oct 8, 2025
@hi-ogawa hi-ogawa changed the title fix(plugin-rsc): dynamically use user's react-server-dom-webpack when available fix(rsc): dynamically use user's react-server-dom-webpack when available Oct 8, 2025
@hi-ogawa hi-ogawa changed the title fix(rsc): dynamically use user's react-server-dom-webpack when available fix(rsc): use locally installed react-server-dom-webpack when available Oct 8, 2025
@hi-ogawa hi-ogawa changed the title fix(rsc): use locally installed react-server-dom-webpack when available feat(rsc): use locally installed react-server-dom-webpack when available Oct 8, 2025
@hi-ogawa hi-ogawa added the trigger: preview Trigger pkg.pr.new label Oct 8, 2025
@pkg-pr-new
Copy link

pkg-pr-new bot commented Oct 8, 2025

Open in StackBlitz

npm i https://pkg.pr.new/@vitejs/plugin-react@915
npm i https://pkg.pr.new/@vitejs/plugin-react-oxc@915
npm i https://pkg.pr.new/@vitejs/plugin-rsc@915
npm i https://pkg.pr.new/@vitejs/plugin-react-swc@915

commit: 0de8d8d

@hi-ogawa hi-ogawa marked this pull request as ready for review October 8, 2025 08:11
@hi-ogawa
Copy link
Contributor

hi-ogawa commented Oct 8, 2025

@dai-shi This has been implemented and Waku CI is passing. https://github.com/vitejs/vite-ecosystem-ci/actions/runs/18338297753

@dai-shi
Copy link

dai-shi commented Oct 8, 2025

Thaaanks!

@hi-ogawa hi-ogawa merged commit 3506cfa into main Oct 9, 2025
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

trigger: preview Trigger pkg.pr.new

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dynamically switch to react-server-dom-webpack when available in user project package.json instead of bundled @vitejs/plugin-rsc/vendor/react-server-dom

3 participants