Skip to content

Commit

Permalink
Move useMemoCache hook to react/compiler-runtime
Browse files Browse the repository at this point in the history
For Meta-internal purposes, we keep the export on `react` itself to reduce churn.
  • Loading branch information
kassens committed Apr 29, 2024
1 parent 190cc99 commit 290712f
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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/jsx-runtime').c;
});

it('should inspect the current state of useState hooks', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as React from 'react';
import {c} from 'react/compiler-runtime';

export default function UseMemoCache(): React.Node {
React.unstable_useMemoCache(1);
c(1);

return null;
}
2 changes: 1 addition & 1 deletion packages/react-reconciler/src/ReactInternalTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ export type Dispatcher = {
): T,
useId(): string,
useCacheRefresh?: () => <T>(?() => T, ?T) => void,
useMemoCache?: (size: number) => Array<any>,
useMemoCache: (size: number) => Array<any>,
useHostTransitionStatus?: () => TransitionStatus,
useOptimistic?: <S, A>(
passthrough: S,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 8 additions & 1 deletion packages/react/index.classic.fb.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export {
unstable_SuspenseList,
unstable_getCacheForType,
unstable_useCacheRefresh,
unstable_useMemoCache,
useId,
useCallback,
useContext,
Expand All @@ -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';
1 change: 0 additions & 1 deletion packages/react/index.experimental.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export {
unstable_getCacheForType,
unstable_SuspenseList,
unstable_useCacheRefresh,
unstable_useMemoCache,
useId,
useCallback,
useContext,
Expand Down
1 change: 0 additions & 1 deletion packages/react/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export {
unstable_TracingMarker,
unstable_getCacheForType,
unstable_useCacheRefresh,
unstable_useMemoCache,
useId,
useCallback,
useContext,
Expand Down
8 changes: 7 additions & 1 deletion packages/react/index.modern.fb.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export {
unstable_TracingMarker,
unstable_getCacheForType,
unstable_useCacheRefresh,
unstable_useMemoCache,
useId,
useCallback,
useContext,
Expand All @@ -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';
4 changes: 4 additions & 0 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,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": {
Expand Down
2 changes: 0 additions & 2 deletions packages/react/src/ReactClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ import {
useId,
useCacheRefresh,
use,
useMemoCache,
useOptimistic,
useActionState,
} from './ReactHooks';
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions packages/react/src/compiler-runtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* 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.
*/

export {useMemoCache as c} from './src/ReactHooks';
2 changes: 1 addition & 1 deletion packages/shared/ReactFeatureFlags.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions scripts/rollup/bundles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down

0 comments on commit 290712f

Please sign in to comment.