mirror of
https://github.com/hanzoai/react.git
synced 2026-09-24 17:02:27 +00:00
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.
This commit is contained in:
committed by
GitHub
parent
401e22ba48
commit
ea74b893bc
+1
-1
@@ -44,7 +44,7 @@ export function getCurrentParentStackInDev(): string {
|
||||
return '';
|
||||
}
|
||||
|
||||
function getCurrentFiberStackInDev(stack: Error): string {
|
||||
function getCurrentFiberStackInDev(stack: null | Error): string {
|
||||
if (__DEV__) {
|
||||
if (current === null) {
|
||||
return '';
|
||||
|
||||
@@ -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 **)',
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -61,6 +61,7 @@ import {
|
||||
disableLegacyMode,
|
||||
} from 'shared/ReactFeatureFlags';
|
||||
|
||||
// $FlowFixMe[prop-missing]: This is only in the development export.
|
||||
const act = React.act;
|
||||
|
||||
// TODO: Remove from public bundle
|
||||
|
||||
@@ -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';
|
||||
@@ -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);
|
||||
}
|
||||
@@ -9,7 +9,6 @@
|
||||
|
||||
export {
|
||||
__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,
|
||||
act,
|
||||
Children,
|
||||
Component,
|
||||
Fragment,
|
||||
|
||||
@@ -30,7 +30,6 @@ export type ChildrenArray<+T> = $ReadOnlyArray<ChildrenArray<T>> | T;
|
||||
// We can't use export * from in Flow for some reason.
|
||||
export {
|
||||
__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,
|
||||
act,
|
||||
Children,
|
||||
Component,
|
||||
Fragment,
|
||||
|
||||
@@ -61,6 +61,7 @@ import {
|
||||
import ReactSharedInternals from './ReactSharedInternalsClient';
|
||||
import {startTransition} from './ReactStartTransition';
|
||||
import {act} from './ReactAct';
|
||||
import {captureOwnerStack} from './ReactOwnerStack';
|
||||
|
||||
const Children = {
|
||||
map,
|
||||
@@ -121,5 +122,6 @@ export {
|
||||
// enableTransitionTracing
|
||||
REACT_TRACING_MARKER_TYPE as unstable_TracingMarker,
|
||||
useId,
|
||||
act,
|
||||
act, // DEV-only
|
||||
captureOwnerStack, // DEV-only
|
||||
};
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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
|
||||
};
|
||||
@@ -35,7 +35,7 @@ export type SharedStateClient = {
|
||||
thrownErrors: Array<mixed>,
|
||||
|
||||
// ReactDebugCurrentFrame
|
||||
getCurrentStack: null | ((stack: Error) => string),
|
||||
getCurrentStack: null | ((stack: null | Error) => string),
|
||||
};
|
||||
|
||||
export type RendererTask = boolean => RendererTask | null;
|
||||
@@ -56,7 +56,7 @@ if (__DEV__) {
|
||||
// Stack implementation injected by the current renderer.
|
||||
ReactSharedInternals.getCurrentStack = (null:
|
||||
| null
|
||||
| ((stack: Error) => string));
|
||||
| ((stack: null | Error) => string));
|
||||
}
|
||||
|
||||
export default ReactSharedInternals;
|
||||
|
||||
@@ -38,7 +38,7 @@ export type SharedStateServer = {
|
||||
// DEV-only
|
||||
|
||||
// ReactDebugCurrentFrame
|
||||
getCurrentStack: null | ((stack: Error) => string),
|
||||
getCurrentStack: null | ((stack: null | Error) => string),
|
||||
};
|
||||
|
||||
export type RendererTask = boolean => RendererTask | null;
|
||||
@@ -60,7 +60,7 @@ if (__DEV__) {
|
||||
// Stack implementation injected by the current renderer.
|
||||
ReactSharedInternals.getCurrentStack = (null:
|
||||
| null
|
||||
| ((stack: Error) => string));
|
||||
| ((stack: null | Error) => string));
|
||||
}
|
||||
|
||||
export default ReactSharedInternals;
|
||||
|
||||
@@ -12,6 +12,8 @@ function resolveEntryFork(resolvedEntry, isFBBundle) {
|
||||
// .stable.js
|
||||
// .experimental.js
|
||||
// .js
|
||||
// or any of those plus .development.js
|
||||
|
||||
if (isFBBundle) {
|
||||
// FB builds for react-dom need to alias both react-dom and react-dom/client to the same
|
||||
// entrypoint since there is only a single build for them.
|
||||
@@ -41,6 +43,10 @@ function resolveEntryFork(resolvedEntry, isFBBundle) {
|
||||
}
|
||||
|
||||
resolvedEntry = nodePath.join(resolvedEntry, '..', entrypoint);
|
||||
const developmentEntry = resolvedEntry.replace('.js', '.development.js');
|
||||
if (fs.existsSync(developmentEntry)) {
|
||||
return developmentEntry;
|
||||
}
|
||||
if (fs.existsSync(resolvedEntry)) {
|
||||
return resolvedEntry;
|
||||
}
|
||||
@@ -53,6 +59,10 @@ function resolveEntryFork(resolvedEntry, isFBBundle) {
|
||||
'.js',
|
||||
__EXPERIMENTAL__ ? '.modern.fb.js' : '.classic.fb.js'
|
||||
);
|
||||
const devFBEntry = resolvedFBEntry.replace('.js', '.development.js');
|
||||
if (fs.existsSync(devFBEntry)) {
|
||||
return devFBEntry;
|
||||
}
|
||||
if (fs.existsSync(resolvedFBEntry)) {
|
||||
return resolvedFBEntry;
|
||||
}
|
||||
@@ -66,9 +76,17 @@ function resolveEntryFork(resolvedEntry, isFBBundle) {
|
||||
'.js',
|
||||
__EXPERIMENTAL__ ? '.experimental.js' : '.stable.js'
|
||||
);
|
||||
const devForkedEntry = resolvedForkedEntry.replace('.js', '.development.js');
|
||||
if (fs.existsSync(devForkedEntry)) {
|
||||
return devForkedEntry;
|
||||
}
|
||||
if (fs.existsSync(resolvedForkedEntry)) {
|
||||
return resolvedForkedEntry;
|
||||
}
|
||||
const plainDevEntry = resolvedEntry.replace('.js', '.development.js');
|
||||
if (fs.existsSync(plainDevEntry)) {
|
||||
return plainDevEntry;
|
||||
}
|
||||
// Just use the plain .js one.
|
||||
return resolvedEntry;
|
||||
}
|
||||
@@ -77,7 +95,8 @@ function mockReact() {
|
||||
jest.mock('react', () => {
|
||||
const resolvedEntryPoint = resolveEntryFork(
|
||||
require.resolve('react'),
|
||||
global.__WWW__ || global.__XPLAT__
|
||||
global.__WWW__ || global.__XPLAT__,
|
||||
global.__DEV__
|
||||
);
|
||||
return jest.requireActual(resolvedEntryPoint);
|
||||
});
|
||||
@@ -100,7 +119,8 @@ jest.mock('react/react.react-server', () => {
|
||||
});
|
||||
const resolvedEntryPoint = resolveEntryFork(
|
||||
require.resolve('react/src/ReactServer'),
|
||||
global.__WWW__ || global.__XPLAT__
|
||||
global.__WWW__ || global.__XPLAT__,
|
||||
global.__DEV__
|
||||
);
|
||||
return jest.requireActual(resolvedEntryPoint);
|
||||
});
|
||||
@@ -198,7 +218,8 @@ inlinedHostConfigs.forEach(rendererInfo => {
|
||||
mockAllConfigs(rendererInfo);
|
||||
const resolvedEntryPoint = resolveEntryFork(
|
||||
require.resolve(entryPoint),
|
||||
global.__WWW__ || global.__XPLAT__
|
||||
global.__WWW__ || global.__XPLAT__,
|
||||
global.__DEV__
|
||||
);
|
||||
return jest.requireActual(resolvedEntryPoint);
|
||||
});
|
||||
|
||||
+21
-1
@@ -567,16 +567,31 @@ function resolveEntryFork(resolvedEntry, isFBBundle) {
|
||||
// .stable.js
|
||||
// .experimental.js
|
||||
// .js
|
||||
// or any of those plus .development.js
|
||||
|
||||
if (isFBBundle) {
|
||||
const resolvedFBEntry = resolvedEntry.replace(
|
||||
'.js',
|
||||
__EXPERIMENTAL__ ? '.modern.fb.js' : '.classic.fb.js'
|
||||
);
|
||||
const developmentFBEntry = resolvedFBEntry.replace(
|
||||
'.js',
|
||||
'.development.js'
|
||||
);
|
||||
if (fs.existsSync(developmentFBEntry)) {
|
||||
return developmentFBEntry;
|
||||
}
|
||||
if (fs.existsSync(resolvedFBEntry)) {
|
||||
return resolvedFBEntry;
|
||||
}
|
||||
const resolvedGenericFBEntry = resolvedEntry.replace('.js', '.fb.js');
|
||||
const developmentGenericFBEntry = resolvedGenericFBEntry.replace(
|
||||
'.js',
|
||||
'.development.js'
|
||||
);
|
||||
if (fs.existsSync(developmentGenericFBEntry)) {
|
||||
return developmentGenericFBEntry;
|
||||
}
|
||||
if (fs.existsSync(resolvedGenericFBEntry)) {
|
||||
return resolvedGenericFBEntry;
|
||||
}
|
||||
@@ -586,6 +601,10 @@ function resolveEntryFork(resolvedEntry, isFBBundle) {
|
||||
'.js',
|
||||
__EXPERIMENTAL__ ? '.experimental.js' : '.stable.js'
|
||||
);
|
||||
const devForkedEntry = resolvedForkedEntry.replace('.js', '.development.js');
|
||||
if (fs.existsSync(devForkedEntry)) {
|
||||
return devForkedEntry;
|
||||
}
|
||||
if (fs.existsSync(resolvedForkedEntry)) {
|
||||
return resolvedForkedEntry;
|
||||
}
|
||||
@@ -604,7 +623,8 @@ async function createBundle(bundle, bundleType) {
|
||||
|
||||
let resolvedEntry = resolveEntryFork(
|
||||
require.resolve(bundle.entry),
|
||||
isFBWWWBundle || isFBRNBundle
|
||||
isFBWWWBundle || isFBRNBundle,
|
||||
!isProductionBundleType(bundleType)
|
||||
);
|
||||
|
||||
const peerGlobals = Modules.getPeerGlobals(bundle.externals, bundleType);
|
||||
|
||||
Reference in New Issue
Block a user