Skip to content

Conversation

@GeoffKarnov
Copy link
Contributor

This commit addresses webpack module resolution errors that occur when bundling React Router's RSC features in React 18 environments, where React.use does not exist.

Problem:
Webpack's static analysis phase occurs before any runtime code executes. When webpack encounters React.use(getPayload()) in the source code, it attempts to validate that the use export exists in the React module during build time. In React 18, this export doesn't exist, causing the build error:

export 'use' (imported as 'React5') was not found in 'react' (possible exports: Children, Component, Fragment, Profiler, PureComponent, StrictMode, Suspense, __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, act, cloneElement, createContext, createElement, createFactory, createRef, forwardRef, isValidElement, lazy, memo, startTransition, unstable_act, useCallback, useContext, useDebugValue, useDeferredValue, useEffect, useId, useImperativeHandle, useInsertionEffect, useLayoutEffect, useMemo, useReducer, useRef, useState, useSyncExternalStore, useTransition, version)

Solution:
Implemented react18compatibleUse function that:

Moves the property resolution from build-time to runtime, bypassing webpack's static validation while maintaining the same functionality.

I tried many other alternate solutions but they all failed due to them being runtime solutions, but webpack's static analysis happens before runtime.

This solution:

  1. Hides property access from static analysis using dynamic property lookup with a string variable instead of direct React.use access
  2. Provides React 18/19 compatibility by detecting if React.use exists at runtime and falling back to an informative error function
  3. Maintains identical API to React.use, enabling seamless migration when React 18 support is eventually dropped
  4. Preserves type safety with proper generic typing matching React 19's Usable interface

Fixes #14020

This commit addresses webpack module resolution errors that occur when
bundling React Router's RSC features in React 18 environments, where
React.use does not exist.

**Problem:**
Webpack's static analysis phase occurs before any runtime code executes.
When webpack encounters `React.use(getPayload())` in the source code, it
attempts to validate that the `use` export exists in the React module
during build time. In React 18, this export doesn't exist, causing the
build error:

```
export 'use' (imported as 'React5') was not found in 'react' (possible exports: Children, Component, Fragment, Profiler, PureComponent, StrictMode, Suspense, __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, act, cloneElement, createContext, createElement, createFactory, createRef, forwardRef, isValidElement, lazy, memo, startTransition, unstable_act, useCallback, useContext, useDebugValue, useDeferredValue, useEffect, useId, useImperativeHandle, useInsertionEffect, useLayoutEffect, useMemo, useReducer, useRef, useState, useSyncExternalStore, useTransition, version)
```

**Solution:**
Implemented `react18compatibleUse` function that:

Moves the property resolution from build-time to runtime, bypassing
webpack's static validation while maintaining the same functionality.

I tried many other alternate solutions but they all failed due to them
being runtime solutions, but webpack's static analysis happens _before_
runtime.

This solution:
1. **Hides property access from static analysis** using dynamic property
   lookup with a string variable instead of direct `React.use` access
2. **Provides React 18/19 compatibility** by detecting if React.use exists
   at runtime and falling back to an informative error function
3. **Maintains identical API** to React.use, enabling seamless migration
   when React 18 support is eventually dropped
4. **Preserves type safety** with proper generic typing matching React 19's
   Usable<T> interface
@changeset-bot
Copy link

changeset-bot bot commented Aug 4, 2025

🦋 Changeset detected

Latest commit: ee71d02

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 11 packages
Name Type
react-router Patch
@react-router/architect Patch
@react-router/cloudflare Patch
@react-router/dev Patch
react-router-dom Patch
@react-router/express Patch
@react-router/node Patch
@react-router/serve Patch
@react-router/fs-routes Patch
@react-router/remix-routes-option-adapter Patch
create-react-router Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@remix-cla-bot
Copy link
Contributor

remix-cla-bot bot commented Aug 4, 2025

Hi @GeoffKarnov,

Welcome, and thank you for contributing to React Router!

Before we consider your pull request, we ask that you sign our Contributor License Agreement (CLA). We require this only once.

You may review the CLA and sign it by adding your name to contributors.yml.

Once the CLA is signed, the CLA Signed label will be added to the pull request.

If you have already signed the CLA and received this response in error, or if you have any questions, please contact us at [email protected].

Thanks!

- The Remix team

@GeoffKarnov
Copy link
Contributor Author

I'm not sure how to add tests for this to this repository, since the problem requires react 18 and webpack

I have verified it works by manually testing against the reproduction repository: timdorr/rr-treeshaking-issue@5fed997

@remix-cla-bot
Copy link
Contributor

remix-cla-bot bot commented Aug 4, 2025

Thank you for signing the Contributor License Agreement. Let's get this merged! 🥳

@timdorr timdorr requested a review from brophdawg11 August 4, 2025 17:58
@brophdawg11
Copy link
Contributor

Thanks! I made a small update to follow the pattern we used for this type of issue when supporting React 17/18 at the same time but this is definitely the way to go. I'm a bit surprised webpack is inspecting code that isn't actually imported by the app code but 🤷

@brophdawg11 brophdawg11 merged commit 59522ba into remix-run:dev Aug 4, 2025
5 checks passed
@GeoffKarnov
Copy link
Contributor Author

Great. Thanks!

@github-actions
Copy link
Contributor

github-actions bot commented Aug 5, 2025

🤖 Hello there,

We just published version 7.8.0-pre.0 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

@github-actions
Copy link
Contributor

github-actions bot commented Aug 7, 2025

🤖 Hello there,

We just published version 7.8.0 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

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