Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move useMemoCache hook to react/compiler-runtime #28954

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 as useMemoCache} from 'react/compiler-runtime';

export default function UseMemoCache(): React.Node {
React.unstable_useMemoCache(1);
useMemoCache(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';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @acdlite and @gnoff may have suggested the idea of moving the impl as well into it's own package and dynamically injecting it into the dispatcher iirc. This feels okay to me as a first step though

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that's possible as useMemoCache adds an extra field to the fiber.

I think what we can look into is how this should look in the server build. We might iterate on that as the server build should probably have a noop use memo cache or not even use the transform.

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';
14 changes: 14 additions & 0 deletions packages/react/npm/compiler-runtime.js
Original file line number Diff line number Diff line change
@@ -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');
}
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__;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to match as a bundle test is using the test renderer and asserting this isn't available in non-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