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 7fd1a59
Show file tree
Hide file tree
Showing 14 changed files with 63 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/compiler-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;
}
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
10 changes: 10 additions & 0 deletions packages/react/compiler-runtime.js
Original file line number Diff line number Diff line change
@@ -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';
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';
16 changes: 16 additions & 0 deletions packages/react/npm/compiler-runtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* 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
*/

'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');
}
5 changes: 5 additions & 0 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"README.md",
"index.js",
"cjs/",
"compiler-runtime.js",
"jsx-runtime.js",
"jsx-runtime.react-server.js",
"jsx-dev-runtime.js",
Expand All @@ -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": {
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
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
2 changes: 1 addition & 1 deletion packages/shared/forks/ReactFeatureFlags.test-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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 7fd1a59

Please sign in to comment.