Skip to content

Commit

Permalink
Stop exporting dev-only methods production builds
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Jan 23, 2025
1 parent 028c8e6 commit 5f9f622
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 40 deletions.
8 changes: 0 additions & 8 deletions packages/react-dom/src/__tests__/ReactTestUtilsAct-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -779,13 +779,5 @@ function runActTests(render, unmount, rerender) {
});
}
});
describe('throw in prod mode', () => {
// @gate !__DEV__
it('warns if you try to use act() in prod mode', () => {
expect(() => act(() => {})).toThrow(
'act(...) is not supported in production builds of React',
);
});
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ describe('isomorphic act()', () => {
return text;
}

// @gate !__DEV__
it('is not exported in production builds', () => {
expect(React).not.toHaveProperty('act');
});

// @gate __DEV__
it('bypasses queueMicrotask', async () => {
const root = ReactNoop.createRoot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ describe('ReactOwnerStacks', () => {
);
}

// @gate !__DEV__ || !enableOwnerStacks
it('is not exported in production buids', () => {
expect(React).not.toHaveProperty('captureOwnerStack');
});

// @gate __DEV__ && enableOwnerStacks
it('can get the component owner stacks during rendering in dev', async () => {
let stack;
Expand Down
79 changes: 79 additions & 0 deletions packages/react/index.fb.development.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* 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
*/
import {enableOwnerStacks} from 'shared/ReactFeatureFlags';
import {captureOwnerStack as captureOwnerStackImpl} from './src/ReactClient';

export {
__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,
__COMPILER_RUNTIME,
cache,
Children,
cloneElement,
Component,
createContext,
createElement,
createRef,
experimental_useEffectEvent,
experimental_useResourceEffect,
forwardRef,
Fragment,
isValidElement,
lazy,
memo,
Profiler,
PureComponent,
startTransition,
StrictMode,
Suspense,
unstable_Activity,
unstable_getCacheForType,
unstable_LegacyHidden,
unstable_Scope,
unstable_SuspenseList,
unstable_TracingMarker,
unstable_useCacheRefresh,
use,
useActionState,
useCallback,
useContext,
useDebugValue,
useDeferredValue,
useEffect,
useId,
useImperativeHandle,
useInsertionEffect,
useLayoutEffect,
useMemo,
useOptimistic,
useReducer,
useRef,
useState,
useSyncExternalStore,
useTransition,
version,
act, // DEV-only
} 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';

// Only export captureOwnerStack if enabled.
let captureOwnerStack: ?() => null | string;
if (enableOwnerStacks) {
captureOwnerStack = captureOwnerStackImpl;
}

// DEV-only
export {captureOwnerStack};
11 changes: 0 additions & 11 deletions packages/react/index.fb.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@
* @flow
*/

import {enableOwnerStacks} from 'shared/ReactFeatureFlags';
import {captureOwnerStack as captureOwnerStackImpl} from './src/ReactClient';
export {
__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,
__COMPILER_RUNTIME,
act,
cache,
Children,
cloneElement,
Expand Down Expand Up @@ -68,11 +65,3 @@ 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';

// Only export captureOwnerStack in development.
let captureOwnerStack: ?() => null | string;
if (__DEV__ && enableOwnerStacks) {
captureOwnerStack = captureOwnerStackImpl;
}

export {captureOwnerStack};
2 changes: 0 additions & 2 deletions packages/react/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ 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,
__COMPILER_RUNTIME,
Expand Down
51 changes: 51 additions & 0 deletions packages/react/index.stable.development.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* 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,
__COMPILER_RUNTIME,
Children,
Component,
Fragment,
Profiler,
PureComponent,
StrictMode,
Suspense,
cloneElement,
createContext,
createElement,
createRef,
use,
forwardRef,
isValidElement,
lazy,
memo,
cache,
unstable_useCacheRefresh,
startTransition,
useId,
useCallback,
useContext,
useDebugValue,
useDeferredValue,
useEffect,
useImperativeHandle,
useInsertionEffect,
useLayoutEffect,
useMemo,
useReducer,
useOptimistic,
useRef,
useState,
useSyncExternalStore,
useTransition,
useActionState,
version,
act, // DEV-only
} from './src/ReactClient';
1 change: 0 additions & 1 deletion packages/react/index.stable.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
export {
__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,
__COMPILER_RUNTIME,
act,
Children,
Component,
Fragment,
Expand Down
19 changes: 13 additions & 6 deletions scripts/jest/setupHostConfigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ function resolveEntryFork(resolvedEntry, isFBBundle) {
}

resolvedEntry = nodePath.join(resolvedEntry, '..', entrypoint);
const developmentEntry = resolvedEntry.replace('.js', '.development.js');
if (fs.existsSync(developmentEntry)) {
return developmentEntry;
const devEntry = resolvedEntry.replace('.js', '.development.js');
if (__DEV__ && fs.existsSync(devEntry)) {
return devEntry;
}
if (fs.existsSync(resolvedEntry)) {
return resolvedEntry;
Expand All @@ -60,13 +60,20 @@ function resolveEntryFork(resolvedEntry, isFBBundle) {
__EXPERIMENTAL__ ? '.modern.fb.js' : '.classic.fb.js'
);
const devFBEntry = resolvedFBEntry.replace('.js', '.development.js');
if (fs.existsSync(devFBEntry)) {
if (__DEV__ && fs.existsSync(devFBEntry)) {
return devFBEntry;
}
if (fs.existsSync(resolvedFBEntry)) {
return resolvedFBEntry;
}
const resolvedGenericFBEntry = resolvedEntry.replace('.js', '.fb.js');
const devGenericFBEntry = resolvedGenericFBEntry.replace(
'.js',
'.development.js'
);
if (__DEV__ && fs.existsSync(devGenericFBEntry)) {
return devGenericFBEntry;
}
if (fs.existsSync(resolvedGenericFBEntry)) {
return resolvedGenericFBEntry;
}
Expand All @@ -77,14 +84,14 @@ function resolveEntryFork(resolvedEntry, isFBBundle) {
__EXPERIMENTAL__ ? '.experimental.js' : '.stable.js'
);
const devForkedEntry = resolvedForkedEntry.replace('.js', '.development.js');
if (fs.existsSync(devForkedEntry)) {
if (__DEV__ && fs.existsSync(devForkedEntry)) {
return devForkedEntry;
}
if (fs.existsSync(resolvedForkedEntry)) {
return resolvedForkedEntry;
}
const plainDevEntry = resolvedEntry.replace('.js', '.development.js');
if (fs.existsSync(plainDevEntry)) {
if (__DEV__ && fs.existsSync(plainDevEntry)) {
return plainDevEntry;
}
// Just use the plain .js one.
Expand Down
21 changes: 9 additions & 12 deletions scripts/rollup/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ function shouldSkipBundle(bundle, bundleType) {
return false;
}

function resolveEntryFork(resolvedEntry, isFBBundle) {
function resolveEntryFork(resolvedEntry, isFBBundle, isDev) {
// Pick which entry point fork to use:
// .modern.fb.js
// .classic.fb.js
Expand All @@ -586,23 +586,20 @@ function resolveEntryFork(resolvedEntry, isFBBundle) {
'.js',
__EXPERIMENTAL__ ? '.modern.fb.js' : '.classic.fb.js'
);
const developmentFBEntry = resolvedFBEntry.replace(
'.js',
'.development.js'
);
if (fs.existsSync(developmentFBEntry)) {
return developmentFBEntry;
const devFBEntry = resolvedFBEntry.replace('.js', '.development.js');
if (isDev && fs.existsSync(devFBEntry)) {
return devFBEntry;
}
if (fs.existsSync(resolvedFBEntry)) {
return resolvedFBEntry;
}
const resolvedGenericFBEntry = resolvedEntry.replace('.js', '.fb.js');
const developmentGenericFBEntry = resolvedGenericFBEntry.replace(
const devGenericFBEntry = resolvedGenericFBEntry.replace(
'.js',
'.development.js'
);
if (fs.existsSync(developmentGenericFBEntry)) {
return developmentGenericFBEntry;
if (isDev && fs.existsSync(devGenericFBEntry)) {
return devGenericFBEntry;
}
if (fs.existsSync(resolvedGenericFBEntry)) {
return resolvedGenericFBEntry;
Expand All @@ -614,7 +611,7 @@ function resolveEntryFork(resolvedEntry, isFBBundle) {
__EXPERIMENTAL__ ? '.experimental.js' : '.stable.js'
);
const devForkedEntry = resolvedForkedEntry.replace('.js', '.development.js');
if (fs.existsSync(devForkedEntry)) {
if (isDev && fs.existsSync(devForkedEntry)) {
return devForkedEntry;
}
if (fs.existsSync(resolvedForkedEntry)) {
Expand All @@ -633,7 +630,7 @@ async function createBundle(bundle, bundleType) {

const {isFBWWWBundle, isFBRNBundle} = getBundleTypeFlags(bundleType);

let resolvedEntry = resolveEntryFork(
const resolvedEntry = resolveEntryFork(
require.resolve(bundle.entry),
isFBWWWBundle || isFBRNBundle,
!isProductionBundleType(bundleType)
Expand Down

0 comments on commit 5f9f622

Please sign in to comment.