Skip to content

Commit

Permalink
[react-dom] Reorganize react-dom internals to match react (#25277)
Browse files Browse the repository at this point in the history
* reorganize react-dom internals to match react

* refactor and make forks work for flow and internal imports

* flew too close to the sun

* typo
  • Loading branch information
gnoff authored Sep 15, 2022
1 parent fc16293 commit e7fc04b
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 33 deletions.
7 changes: 4 additions & 3 deletions packages/react-dom/index.classic.fb.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@

import {isEnabled} from './src/events/ReactDOMEventListener';

import {__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED} from './src/client/ReactDOM';
import Internals from './src/ReactDOMSharedInternals';

// For classic WWW builds, include a few internals that are already in use.
Object.assign((__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: any), {
Object.assign((Internals: any), {
ReactBrowserEventEmitter: {
isEnabled,
},
});

export {
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
createPortal,
createRoot,
hydrateRoot,
Expand All @@ -36,3 +35,5 @@ export {
unstable_runWithPriority, // DO NOT USE: Temporarily exposed to migrate off of Scheduler.runWithPriority.
version,
} from './src/client/ReactDOM';

export {Internals as __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED};
2 changes: 1 addition & 1 deletion packages/react-dom/index.experimental.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* @flow
*/

export {default as __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED} from './src/ReactDOMSharedInternals';
export {
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
createPortal,
createRoot,
hydrateRoot,
Expand Down
2 changes: 1 addition & 1 deletion packages/react-dom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

// Export all exports so that they're available in tests.
// We can't use export * from in Flow for some reason.
export {default as __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED} from './src/ReactDOMSharedInternals';
export {
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
createPortal,
createRoot,
hydrateRoot,
Expand Down
2 changes: 1 addition & 1 deletion packages/react-dom/index.modern.fb.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* @flow
*/

export {default as __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED} from './src/ReactDOMSharedInternals';
export {
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
createPortal,
createRoot,
hydrateRoot,
Expand Down
2 changes: 1 addition & 1 deletion packages/react-dom/index.stable.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* @flow
*/

export {default as __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED} from './src/ReactDOMSharedInternals';
export {
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
createPortal,
createRoot,
hydrateRoot,
Expand Down
35 changes: 35 additions & 0 deletions packages/react-dom/src/ReactDOMSharedInternals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (c) Facebook, 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 {batchedUpdates} from 'react-reconciler/src/ReactFiberReconciler';
import {
enqueueStateRestore,
restoreStateIfNeeded,
} from './events/ReactDOMControlledComponent';
import {
getInstanceFromNode,
getNodeFromInstance,
getFiberCurrentPropsFromNode,
} from './client/ReactDOMComponentTree';

const Internals = {
usingClientEntryPoint: false,
// Keep in sync with ReactTestUtils.js.
// This is an array for better minification.
Events: [
getInstanceFromNode,
getNodeFromInstance,
getFiberCurrentPropsFromNode,
enqueueStateRestore,
restoreStateIfNeeded,
batchedUpdates,
],
};

export default Internals;
29 changes: 3 additions & 26 deletions packages/react-dom/src/client/ReactDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,7 @@ import {canUseDOM} from 'shared/ExecutionEnvironment';
import ReactVersion from 'shared/ReactVersion';
import {enableNewReconciler} from 'shared/ReactFeatureFlags';

import {
getInstanceFromNode,
getNodeFromInstance,
getFiberCurrentPropsFromNode,
getClosestInstanceFromNode,
} from './ReactDOMComponentTree';
import {getClosestInstanceFromNode} from './ReactDOMComponentTree';
import {restoreControlledState} from './ReactDOMComponent';
import {
setAttemptSynchronousHydration,
Expand All @@ -66,11 +61,8 @@ import {
setAttemptHydrationAtPriority,
} from '../events/ReactDOMEventReplaying';
import {setBatchingImplementation} from '../events/ReactDOMUpdateBatching';
import {
setRestoreImplementation,
enqueueStateRestore,
restoreStateIfNeeded,
} from '../events/ReactDOMControlledComponent';
import {setRestoreImplementation} from '../events/ReactDOMControlledComponent';
import Internals from '../ReactDOMSharedInternals';

setAttemptSynchronousHydration(attemptSynchronousHydration);
setAttemptDiscreteHydration(attemptDiscreteHydration);
Expand Down Expand Up @@ -133,20 +125,6 @@ function renderSubtreeIntoContainer(
);
}

const Internals = {
usingClientEntryPoint: false,
// Keep in sync with ReactTestUtils.js.
// This is an array for better minification.
Events: [
getInstanceFromNode,
getNodeFromInstance,
getFiberCurrentPropsFromNode,
enqueueStateRestore,
restoreStateIfNeeded,
batchedUpdates,
],
};

function createRoot(
container: Element | Document | DocumentFragment,
options?: CreateRootOptions,
Expand Down Expand Up @@ -201,7 +179,6 @@ export {
createPortal,
batchedUpdates as unstable_batchedUpdates,
flushSync,
Internals as __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
ReactVersion as version,
// Disabled behind disableLegacyReactDOMAPIs
findDOMNode,
Expand Down
15 changes: 15 additions & 0 deletions packages/shared/ReactDOMSharedInternals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Copyright (c) Facebook, 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 * as ReactDOM from 'react-dom';

const ReactDOMSharedInternals =
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;

export default ReactDOMSharedInternals;
6 changes: 6 additions & 0 deletions scripts/jest/setupHostConfigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,10 @@ jest.mock('shared/ReactSharedInternals', () =>
jest.requireActual('react/src/ReactSharedInternals')
);

// Make it possible to import this module inside
// the ReactDOM package itself.
jest.mock('shared/ReactDOMSharedInternals', () =>
jest.requireActual('react-dom/src/ReactDOMSharedInternals')
);

jest.mock('scheduler', () => jest.requireActual('scheduler/unstable_mock'));
27 changes: 27 additions & 0 deletions scripts/rollup/forks.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,33 @@ const forks = Object.freeze({
return null;
},

// Without this fork, importing `shared/ReactDOMSharedInternals` inside
// the `react-dom` package itself would not work due to a cyclical dependency.
'./packages/shared/ReactDOMSharedInternals.js': (
bundleType,
entry,
dependencies
) => {
if (entry === 'react-dom') {
return './packages/react-dom/src/ReactDOMSharedInternals.js';
}
if (
!entry.startsWith('react-dom/') &&
dependencies.indexOf('react-dom') === -1
) {
// React DOM internals are unavailable if we can't reference the package.
// We return an error because we only want to throw if this module gets used.
return new Error(
'Cannot use a module that depends on ReactDOMSharedInternals ' +
'from "' +
entry +
'" because it does not declare "react-dom" in the package ' +
'dependencies or peerDependencies.'
);
}
return null;
},

// We have a few forks for different environments.
'./packages/shared/ReactFeatureFlags.js': (bundleType, entry) => {
switch (entry) {
Expand Down
3 changes: 3 additions & 0 deletions scripts/shared/inlinedHostConfigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = [
'react-devtools-shell',
'react-devtools-shared',
'react-interactions',
'shared/ReactDOMSharedInternals',
],
isFlowTyped: true,
isServerSupported: true,
Expand Down Expand Up @@ -66,6 +67,7 @@ module.exports = [
'react-devtools-core',
'react-devtools-shell',
'react-devtools-shared',
'shared/ReactDOMSharedInternals',
],
isFlowTyped: true,
isServerSupported: true,
Expand All @@ -85,6 +87,7 @@ module.exports = [
'react-dom/src/server/ReactDOMLegacyServerNode.classic.fb.js',
'react-dom/src/server/ReactDOMLegacyServerNodeStream.js', // file indirection to support partial forking of some methods in *Node
'react-client/src/ReactFlightClientStream.js', // We can only type check this in streaming configurations.
'shared/ReactDOMSharedInternals',
],
isFlowTyped: true,
isServerSupported: true,
Expand Down

0 comments on commit e7fc04b

Please sign in to comment.