From b78a7f2f35e554a8647c3262d7f392e68d06febc Mon Sep 17 00:00:00 2001 From: lauren Date: Mon, 7 Oct 2024 16:25:04 -0400 Subject: [PATCH] [rcr] Re-export useMemoCache in top level React namespace (#31139) In order to support using the compiler on versions of React prior to 19, we need the ability to statically import `c` (aka useMemoCache) or fallback to a polyfill supplied by `react-compiler-runtime` (note: this is a separate npm package, not to be confused with `react/compiler-runtime`, which is currently a part of react). To do this we first need to re-export `useMemoCache` under the top level React namespace again, which is additive and thus non-breaking. Doing so allows `react-compiler-runtime` to statically either re-export `React.__COMPILER_RUNTIME.c` or supply a polyfill, without the need for a dynamic import which is finicky to support due to returning a promise. In later PRs I will remove `react/compiler-runtime` and update the compiler to emit imports to `react-compiler-runtime` instead. --- packages/react/index.development.js | 1 + packages/react/index.experimental.development.js | 1 + packages/react/index.experimental.js | 1 + packages/react/index.fb.js | 1 + packages/react/index.js | 1 + packages/react/src/ReactClient.js | 2 ++ packages/react/src/ReactCompilerRuntime.js | 14 ++++++++++++++ 7 files changed, 21 insertions(+) create mode 100644 packages/react/src/ReactCompilerRuntime.js diff --git a/packages/react/index.development.js b/packages/react/index.development.js index c94c460b97416..bf08db7a58418 100644 --- a/packages/react/index.development.js +++ b/packages/react/index.development.js @@ -30,6 +30,7 @@ export type ChildrenArray<+T> = $ReadOnlyArray> | T; // We can't use export * from in Flow for some reason. export { __CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, + __COMPILER_RUNTIME, Children, Component, Fragment, diff --git a/packages/react/index.experimental.development.js b/packages/react/index.experimental.development.js index 4ffcdea0d242e..676b9eea4d9a9 100644 --- a/packages/react/index.experimental.development.js +++ b/packages/react/index.experimental.development.js @@ -9,6 +9,7 @@ export { __CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, + __COMPILER_RUNTIME, Children, Component, Fragment, diff --git a/packages/react/index.experimental.js b/packages/react/index.experimental.js index 6f2ab835135c0..ae98e3b91f19c 100644 --- a/packages/react/index.experimental.js +++ b/packages/react/index.experimental.js @@ -9,6 +9,7 @@ export { __CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, + __COMPILER_RUNTIME, Children, Component, Fragment, diff --git a/packages/react/index.fb.js b/packages/react/index.fb.js index 1b87e4b2e582f..7999655f30e4b 100644 --- a/packages/react/index.fb.js +++ b/packages/react/index.fb.js @@ -9,6 +9,7 @@ export { __CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, + __COMPILER_RUNTIME, act, cache, Children, diff --git a/packages/react/index.js b/packages/react/index.js index 19f256fd73b5b..0c048c4e9c297 100644 --- a/packages/react/index.js +++ b/packages/react/index.js @@ -31,6 +31,7 @@ export type ChildrenArray<+T> = $ReadOnlyArray> | T; // We can't use export * from in Flow for some reason. export { __CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, + __COMPILER_RUNTIME, Children, Component, Fragment, diff --git a/packages/react/src/ReactClient.js b/packages/react/src/ReactClient.js index 318d8e648d9d5..ed064fe8031c0 100644 --- a/packages/react/src/ReactClient.js +++ b/packages/react/src/ReactClient.js @@ -63,6 +63,7 @@ import ReactSharedInternals from './ReactSharedInternalsClient'; import {startTransition} from './ReactStartTransition'; import {act} from './ReactAct'; import {captureOwnerStack} from './ReactOwnerStack'; +import ReactCompilerRuntime from './ReactCompilerRuntime'; const Children = { map, @@ -109,6 +110,7 @@ export { isValidElement, ReactVersion as version, ReactSharedInternals as __CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, + ReactCompilerRuntime as __COMPILER_RUNTIME, // Concurrent Mode useTransition, startTransition, diff --git a/packages/react/src/ReactCompilerRuntime.js b/packages/react/src/ReactCompilerRuntime.js new file mode 100644 index 0000000000000..a19d05596863e --- /dev/null +++ b/packages/react/src/ReactCompilerRuntime.js @@ -0,0 +1,14 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + */ + +import {useMemoCache} from './ReactHooks'; + +export default { + c: useMemoCache, +};