From 4508873393058e86bed308b56e49ec883ece59d1 Mon Sep 17 00:00:00 2001 From: Jan Kassens Date: Tue, 30 Apr 2024 12:00:22 -0400 Subject: [PATCH] Move useMemoCache hook to react/compiler-runtime (#28954) Move useMemoCache hook to react/compiler-runtime For Meta-internal purposes, we keep the export on `react` itself to reduce churn. --- .../ReactHooksInspectionIntegration-test.js | 2 +- .../src/app/InspectableElements/UseMemoCache.js | 3 ++- .../src/__tests__/useMemoCache-test.js | 2 +- packages/react/compiler-runtime.js | 10 ++++++++++ packages/react/index.classic.fb.js | 9 ++++++++- packages/react/index.experimental.js | 1 - packages/react/index.js | 1 - packages/react/index.modern.fb.js | 8 +++++++- packages/react/npm/compiler-runtime.js | 14 ++++++++++++++ packages/react/package.json | 5 +++++ packages/react/src/ReactClient.js | 2 -- packages/shared/ReactFeatureFlags.js | 2 +- .../forks/ReactFeatureFlags.test-renderer.js | 2 +- scripts/rollup/bundles.js | 11 +++++++++++ 14 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 packages/react/compiler-runtime.js create mode 100644 packages/react/npm/compiler-runtime.js diff --git a/packages/react-debug-tools/src/__tests__/ReactHooksInspectionIntegration-test.js b/packages/react-debug-tools/src/__tests__/ReactHooksInspectionIntegration-test.js index 2adcce742e87e..b0d18c3b5d5ac 100644 --- a/packages/react-debug-tools/src/__tests__/ReactHooksInspectionIntegration-test.js +++ b/packages/react-debug-tools/src/__tests__/ReactHooksInspectionIntegration-test.js @@ -37,7 +37,7 @@ describe('ReactHooksInspectionIntegration', () => { ReactDOM = require('react-dom'); act = require('internal-test-utils').act; ReactDebugTools = require('react-debug-tools'); - useMemoCache = React.unstable_useMemoCache; + useMemoCache = require('react/compiler-runtime').c; }); it('should inspect the current state of useState hooks', async () => { diff --git a/packages/react-devtools-shell/src/app/InspectableElements/UseMemoCache.js b/packages/react-devtools-shell/src/app/InspectableElements/UseMemoCache.js index d3b94e9aec2f8..1a3568c40dc0d 100644 --- a/packages/react-devtools-shell/src/app/InspectableElements/UseMemoCache.js +++ b/packages/react-devtools-shell/src/app/InspectableElements/UseMemoCache.js @@ -1,7 +1,8 @@ import * as React from 'react'; +import {c as useMemoCache} from 'react/compiler-runtime'; export default function UseMemoCache(): React.Node { - React.unstable_useMemoCache(1); + useMemoCache(1); return null; } diff --git a/packages/react-reconciler/src/__tests__/useMemoCache-test.js b/packages/react-reconciler/src/__tests__/useMemoCache-test.js index b2eed6e7d0e6a..280a4458894f0 100644 --- a/packages/react-reconciler/src/__tests__/useMemoCache-test.js +++ b/packages/react-reconciler/src/__tests__/useMemoCache-test.js @@ -27,8 +27,8 @@ describe('useMemoCache()', () => { Scheduler = require('scheduler'); act = require('internal-test-utils').act; assertLog = require('internal-test-utils').assertLog; + useMemoCache = require('react/compiler-runtime').c; useState = React.useState; - useMemoCache = React.unstable_useMemoCache; MemoCacheSentinel = Symbol.for('react.memo_cache_sentinel'); class _ErrorBoundary extends React.Component { diff --git a/packages/react/compiler-runtime.js b/packages/react/compiler-runtime.js new file mode 100644 index 0000000000000..7ff68cd6fa023 --- /dev/null +++ b/packages/react/compiler-runtime.js @@ -0,0 +1,10 @@ +/** + * 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 + */ + +export {useMemoCache as c} from './src/ReactHooks'; diff --git a/packages/react/index.classic.fb.js b/packages/react/index.classic.fb.js index f38e4ec4a4597..c1bfe5b0e4713 100644 --- a/packages/react/index.classic.fb.js +++ b/packages/react/index.classic.fb.js @@ -36,7 +36,6 @@ export { unstable_SuspenseList, unstable_getCacheForType, unstable_useCacheRefresh, - unstable_useMemoCache, useId, useCallback, useContext, @@ -57,4 +56,12 @@ export { useActionState, version, } from './src/ReactClient'; + export {jsx, jsxs, jsxDEV} from './src/jsx/ReactJSX'; + +// export for backwards compatibility during upgrade +export {useMemoCache as unstable_useMemoCache} from './src/ReactHooks'; + +// export to match the name of the OSS function typically exported from +// react/compiler-runtime +export {useMemoCache as c} from './src/ReactHooks'; diff --git a/packages/react/index.experimental.js b/packages/react/index.experimental.js index 6c594578bfeec..efac1257849be 100644 --- a/packages/react/index.experimental.js +++ b/packages/react/index.experimental.js @@ -34,7 +34,6 @@ export { unstable_getCacheForType, unstable_SuspenseList, unstable_useCacheRefresh, - unstable_useMemoCache, useId, useCallback, useContext, diff --git a/packages/react/index.js b/packages/react/index.js index 6dcbe3a83ef1f..8808d259583ee 100644 --- a/packages/react/index.js +++ b/packages/react/index.js @@ -57,7 +57,6 @@ export { unstable_TracingMarker, unstable_getCacheForType, unstable_useCacheRefresh, - unstable_useMemoCache, useId, useCallback, useContext, diff --git a/packages/react/index.modern.fb.js b/packages/react/index.modern.fb.js index fc2bf6dfeaa2a..d5fa4463762d7 100644 --- a/packages/react/index.modern.fb.js +++ b/packages/react/index.modern.fb.js @@ -36,7 +36,6 @@ export { unstable_TracingMarker, unstable_getCacheForType, unstable_useCacheRefresh, - unstable_useMemoCache, useId, useCallback, useContext, @@ -58,3 +57,10 @@ export { version, } from './src/ReactClient'; export {jsx, jsxs, jsxDEV} from './src/jsx/ReactJSX'; + +// export for backwards compatibility during upgrade +export {useMemoCache as unstable_useMemoCache} from './src/ReactHooks'; + +// export to match the name of the OSS function typically exported from +// react/compiler-runtime +export {useMemoCache as c} from './src/ReactHooks'; diff --git a/packages/react/npm/compiler-runtime.js b/packages/react/npm/compiler-runtime.js new file mode 100644 index 0000000000000..ab6aabb0a98a3 --- /dev/null +++ b/packages/react/npm/compiler-runtime.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. + */ + +'use strict'; + +if (process.env.NODE_ENV === 'production') { + module.exports = require('./cjs/react-compiler-runtime.production.js'); +} else { + module.exports = require('./cjs/react-compiler-runtime.development.js'); +} diff --git a/packages/react/package.json b/packages/react/package.json index d19f37f1135f2..a61385ae9db69 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -13,6 +13,7 @@ "README.md", "index.js", "cjs/", + "compiler-runtime.js", "jsx-runtime.js", "jsx-runtime.react-server.js", "jsx-dev-runtime.js", @@ -34,6 +35,10 @@ "react-server": "./jsx-dev-runtime.react-server.js", "default": "./jsx-dev-runtime.js" }, + "./compiler-runtime": { + "react-server": "./compiler-runtime.js", + "default": "./compiler-runtime.js" + }, "./src/*": "./src/*" }, "repository": { diff --git a/packages/react/src/ReactClient.js b/packages/react/src/ReactClient.js index 759112f3e8b8b..4d5ec0d3ea5f8 100644 --- a/packages/react/src/ReactClient.js +++ b/packages/react/src/ReactClient.js @@ -55,7 +55,6 @@ import { useId, useCacheRefresh, use, - useMemoCache, useOptimistic, useActionState, } from './ReactHooks'; @@ -117,7 +116,6 @@ export { getCacheForType as unstable_getCacheForType, useCacheRefresh as unstable_useCacheRefresh, use, - useMemoCache as unstable_useMemoCache, // enableScopeAPI REACT_SCOPE_TYPE as unstable_Scope, // enableTransitionTracing diff --git a/packages/shared/ReactFeatureFlags.js b/packages/shared/ReactFeatureFlags.js index 690cf0b0220ee..bfb48e65df4a1 100644 --- a/packages/shared/ReactFeatureFlags.js +++ b/packages/shared/ReactFeatureFlags.js @@ -102,7 +102,7 @@ export const enableSuspenseAvoidThisFallbackFizz = false; export const enableCPUSuspense = __EXPERIMENTAL__; -// Enables unstable_useMemoCache hook, intended as a compilation target for +// Enables useMemoCache hook, intended as a compilation target for // auto-memoization. export const enableUseMemoCacheHook = __EXPERIMENTAL__; // Test this at Meta before enabling. diff --git a/packages/shared/forks/ReactFeatureFlags.test-renderer.js b/packages/shared/forks/ReactFeatureFlags.test-renderer.js index 120d506f2b445..e80af511bb71b 100644 --- a/packages/shared/forks/ReactFeatureFlags.test-renderer.js +++ b/packages/shared/forks/ReactFeatureFlags.test-renderer.js @@ -36,7 +36,7 @@ export const disableTextareaChildren = false; export const enableSuspenseAvoidThisFallback = false; export const enableSuspenseAvoidThisFallbackFizz = false; export const enableCPUSuspense = false; -export const enableUseMemoCacheHook = true; +export const enableUseMemoCacheHook = __EXPERIMENTAL__; export const enableNoCloningMemoCache = false; export const enableUseEffectEventHook = false; export const favorSafetyOverHydrationPerf = true; diff --git a/scripts/rollup/bundles.js b/scripts/rollup/bundles.js index 428f2c61a109a..f6dc4438fabe8 100644 --- a/scripts/rollup/bundles.js +++ b/scripts/rollup/bundles.js @@ -115,6 +115,17 @@ const bundles = [ externals: ['react', 'ReactNativeInternalFeatureFlags'], }, + /******* Compiler Runtime *******/ + { + bundleTypes: [NODE_DEV, NODE_PROD, NODE_PROFILING], + moduleType: ISOMORPHIC, + entry: 'react/compiler-runtime', + global: 'CompilerRuntime', + minifyWithProdErrorCodes: true, + wrapWithModuleBoundaries: false, + externals: ['react'], + }, + /******* React JSX Runtime React Server *******/ { bundleTypes: [NODE_DEV, NODE_PROD],