-
Notifications
You must be signed in to change notification settings - Fork 47k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Export captureOwnerStacks() only in DEV "react" builds (#29923)
Only with the enableOwnerStacks flag (which is not on in www). This is a new DEV-only API to be able to implement what we do for console.error in user space. This API does not actually include the current stack that you'd get from `new Error().stack`. That you'd have to add yourself. This adds the ability to have conditional development exports because we plan on eventually having separate ESM builds that use the "development" or "production" export conditions. NOTE: This removes the export of `act` from `react` in prod (as opposed to a function that throws) - inline with what we do with other conditional exports.
- Loading branch information
1 parent
e684ca6
commit 6b4646c
Showing
14 changed files
with
380 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
packages/react-reconciler/src/__tests__/ReactOwnerStacks-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* 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. | ||
* | ||
* @emails react-core | ||
* @jest-environment node | ||
*/ | ||
'use strict'; | ||
|
||
let React; | ||
let ReactNoop; | ||
let act; | ||
|
||
describe('ReactOwnerStacks', () => { | ||
beforeEach(function () { | ||
jest.resetModules(); | ||
|
||
React = require('react'); | ||
ReactNoop = require('react-noop-renderer'); | ||
act = require('internal-test-utils').act; | ||
}); | ||
|
||
function normalizeCodeLocInfo(str) { | ||
return ( | ||
str && | ||
str.replace(/\n +(?:at|in) ([\S]+)[^\n]*/g, function (m, name) { | ||
return '\n in ' + name + ' (at **)'; | ||
}) | ||
); | ||
} | ||
|
||
// @gate __DEV__ && enableOwnerStacks | ||
it('can get the component owner stacks during rendering in dev', async () => { | ||
let stack; | ||
|
||
function Foo() { | ||
return <Bar />; | ||
} | ||
function Bar() { | ||
return ( | ||
<div> | ||
<Baz /> | ||
</div> | ||
); | ||
} | ||
function Baz() { | ||
stack = React.captureOwnerStack(); | ||
return <span>hi</span>; | ||
} | ||
|
||
await act(() => { | ||
ReactNoop.render( | ||
<div> | ||
<Foo /> | ||
</div>, | ||
); | ||
}); | ||
|
||
expect(normalizeCodeLocInfo(stack)).toBe( | ||
'\n in Bar (at **)' + '\n in Foo (at **)', | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* 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 | ||
*/ | ||
|
||
// Keep in sync with https://github.com/facebook/flow/blob/main/lib/react.js | ||
export type ComponentType<-P> = React$ComponentType<P>; | ||
export type AbstractComponent< | ||
-Config, | ||
+Instance = mixed, | ||
> = React$AbstractComponent<Config, Instance>; | ||
export type ElementType = React$ElementType; | ||
export type Element<+C> = React$Element<C>; | ||
export type Key = React$Key; | ||
export type Ref<C> = React$Ref<C>; | ||
export type Node = React$Node; | ||
export type Context<T> = React$Context<T>; | ||
export type Portal = React$Portal; | ||
export type ElementProps<C> = React$ElementProps<C>; | ||
export type ElementConfig<C> = React$ElementConfig<C>; | ||
export type ElementRef<C> = React$ElementRef<C>; | ||
export type Config<Props, DefaultProps> = React$Config<Props, DefaultProps>; | ||
export type ChildrenArray<+T> = $ReadOnlyArray<ChildrenArray<T>> | T; | ||
|
||
// Export all exports so that they're available in tests. | ||
// We can't use export * from in Flow for some reason. | ||
export { | ||
__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, | ||
Children, | ||
Component, | ||
Fragment, | ||
Profiler, | ||
PureComponent, | ||
StrictMode, | ||
Suspense, | ||
cloneElement, | ||
createContext, | ||
createElement, | ||
createRef, | ||
use, | ||
forwardRef, | ||
isValidElement, | ||
lazy, | ||
memo, | ||
cache, | ||
startTransition, | ||
unstable_DebugTracingMode, | ||
unstable_LegacyHidden, | ||
unstable_Activity, | ||
unstable_Scope, | ||
unstable_SuspenseList, | ||
unstable_TracingMarker, | ||
unstable_getCacheForType, | ||
unstable_useCacheRefresh, | ||
useId, | ||
useCallback, | ||
useContext, | ||
useDebugValue, | ||
useDeferredValue, | ||
useEffect, | ||
experimental_useEffectEvent, | ||
useImperativeHandle, | ||
useInsertionEffect, | ||
useLayoutEffect, | ||
useMemo, | ||
useOptimistic, | ||
useSyncExternalStore, | ||
useReducer, | ||
useRef, | ||
useState, | ||
useTransition, | ||
useActionState, | ||
version, | ||
act, // DEV-only | ||
captureOwnerStack, // DEV-only | ||
} from './src/ReactClient'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* 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 { | ||
__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, | ||
Children, | ||
Component, | ||
Fragment, | ||
Profiler, | ||
PureComponent, | ||
StrictMode, | ||
Suspense, | ||
cloneElement, | ||
createContext, | ||
createElement, | ||
createRef, | ||
use, | ||
forwardRef, | ||
isValidElement, | ||
lazy, | ||
memo, | ||
cache, | ||
startTransition, | ||
unstable_DebugTracingMode, | ||
unstable_Activity, | ||
unstable_postpone, | ||
unstable_getCacheForType, | ||
unstable_SuspenseList, | ||
unstable_useCacheRefresh, | ||
useId, | ||
useCallback, | ||
useContext, | ||
useDebugValue, | ||
useDeferredValue, | ||
useEffect, | ||
experimental_useEffectEvent, | ||
useImperativeHandle, | ||
useInsertionEffect, | ||
useLayoutEffect, | ||
useMemo, | ||
useOptimistic, | ||
useReducer, | ||
useRef, | ||
useState, | ||
useSyncExternalStore, | ||
useTransition, | ||
useActionState, | ||
version, | ||
act, // DEV-only | ||
captureOwnerStack, // DEV-only | ||
} from './src/ReactClient'; | ||
|
||
import {useOptimistic} from './src/ReactClient'; | ||
|
||
export function experimental_useOptimistic<S, A>( | ||
passthrough: S, | ||
reducer: ?(S, A) => S, | ||
): [S, (A) => void] { | ||
if (__DEV__) { | ||
console.error( | ||
'useOptimistic is now in canary. Remove the experimental_ prefix. ' + | ||
'The prefixed alias will be removed in an upcoming release.', | ||
); | ||
} | ||
return useOptimistic(passthrough, reducer); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and its 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 {enableOwnerStacks} from 'shared/ReactFeatureFlags'; | ||
import ReactSharedInternals from 'shared/ReactSharedInternals'; | ||
|
||
export function captureOwnerStack(): null | string { | ||
if (!enableOwnerStacks || !__DEV__) { | ||
return null; | ||
} | ||
const getCurrentStack = ReactSharedInternals.getCurrentStack; | ||
if (getCurrentStack === null) { | ||
return null; | ||
} | ||
// The current stack will be the owner stack if enableOwnerStacks is true | ||
// which it is always here. Otherwise it's the parent stack. | ||
return getCurrentStack(null); | ||
} |
85 changes: 85 additions & 0 deletions
85
packages/react/src/ReactServer.experimental.development.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* 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 {default as __SERVER_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE} from './ReactSharedInternalsServer'; | ||
|
||
import {forEach, map, count, toArray, only} from './ReactChildren'; | ||
import { | ||
REACT_FRAGMENT_TYPE, | ||
REACT_PROFILER_TYPE, | ||
REACT_STRICT_MODE_TYPE, | ||
REACT_SUSPENSE_TYPE, | ||
REACT_DEBUG_TRACING_MODE_TYPE, | ||
} from 'shared/ReactSymbols'; | ||
import { | ||
cloneElement, | ||
createElement, | ||
isValidElement, | ||
} from './jsx/ReactJSXElement'; | ||
import {createRef} from './ReactCreateRef'; | ||
import { | ||
use, | ||
useId, | ||
useCallback, | ||
useDebugValue, | ||
useMemo, | ||
useActionState, | ||
getCacheForType, | ||
} from './ReactHooks'; | ||
import {forwardRef} from './ReactForwardRef'; | ||
import {lazy} from './ReactLazy'; | ||
import {memo} from './ReactMemo'; | ||
import {cache} from './ReactCacheServer'; | ||
import {startTransition} from './ReactStartTransition'; | ||
import {postpone} from './ReactPostpone'; | ||
import {captureOwnerStack} from './ReactOwnerStack'; | ||
import version from 'shared/ReactVersion'; | ||
|
||
const Children = { | ||
map, | ||
forEach, | ||
count, | ||
toArray, | ||
only, | ||
}; | ||
|
||
// These are server-only | ||
export { | ||
taintUniqueValue as experimental_taintUniqueValue, | ||
taintObjectReference as experimental_taintObjectReference, | ||
} from './ReactTaint'; | ||
|
||
export { | ||
Children, | ||
REACT_FRAGMENT_TYPE as Fragment, | ||
REACT_PROFILER_TYPE as Profiler, | ||
REACT_STRICT_MODE_TYPE as StrictMode, | ||
REACT_SUSPENSE_TYPE as Suspense, | ||
cloneElement, | ||
createElement, | ||
createRef, | ||
use, | ||
forwardRef, | ||
isValidElement, | ||
lazy, | ||
memo, | ||
cache, | ||
startTransition, | ||
REACT_DEBUG_TRACING_MODE_TYPE as unstable_DebugTracingMode, | ||
REACT_SUSPENSE_TYPE as unstable_SuspenseList, | ||
getCacheForType as unstable_getCacheForType, | ||
postpone as unstable_postpone, | ||
useId, | ||
useCallback, | ||
useDebugValue, | ||
useMemo, | ||
useActionState, | ||
version, | ||
captureOwnerStack, // DEV-only | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.