Skip to content

Commit e7fc04b

Browse files
authored
[react-dom] Reorganize react-dom internals to match react (#25277)
* reorganize react-dom internals to match react * refactor and make forks work for flow and internal imports * flew too close to the sun * typo
1 parent fc16293 commit e7fc04b

File tree

11 files changed

+97
-33
lines changed

11 files changed

+97
-33
lines changed

packages/react-dom/index.classic.fb.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,16 @@
99

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

12-
import {__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED} from './src/client/ReactDOM';
12+
import Internals from './src/ReactDOMSharedInternals';
1313

1414
// For classic WWW builds, include a few internals that are already in use.
15-
Object.assign((__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: any), {
15+
Object.assign((Internals: any), {
1616
ReactBrowserEventEmitter: {
1717
isEnabled,
1818
},
1919
});
2020

2121
export {
22-
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
2322
createPortal,
2423
createRoot,
2524
hydrateRoot,
@@ -36,3 +35,5 @@ export {
3635
unstable_runWithPriority, // DO NOT USE: Temporarily exposed to migrate off of Scheduler.runWithPriority.
3736
version,
3837
} from './src/client/ReactDOM';
38+
39+
export {Internals as __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED};

packages/react-dom/index.experimental.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
* @flow
88
*/
99

10+
export {default as __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED} from './src/ReactDOMSharedInternals';
1011
export {
11-
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
1212
createPortal,
1313
createRoot,
1414
hydrateRoot,

packages/react-dom/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
// Export all exports so that they're available in tests.
1111
// We can't use export * from in Flow for some reason.
12+
export {default as __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED} from './src/ReactDOMSharedInternals';
1213
export {
13-
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
1414
createPortal,
1515
createRoot,
1616
hydrateRoot,

packages/react-dom/index.modern.fb.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
* @flow
88
*/
99

10+
export {default as __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED} from './src/ReactDOMSharedInternals';
1011
export {
11-
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
1212
createPortal,
1313
createRoot,
1414
hydrateRoot,

packages/react-dom/index.stable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
* @flow
88
*/
99

10+
export {default as __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED} from './src/ReactDOMSharedInternals';
1011
export {
11-
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
1212
createPortal,
1313
createRoot,
1414
hydrateRoot,
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import {batchedUpdates} from 'react-reconciler/src/ReactFiberReconciler';
11+
import {
12+
enqueueStateRestore,
13+
restoreStateIfNeeded,
14+
} from './events/ReactDOMControlledComponent';
15+
import {
16+
getInstanceFromNode,
17+
getNodeFromInstance,
18+
getFiberCurrentPropsFromNode,
19+
} from './client/ReactDOMComponentTree';
20+
21+
const Internals = {
22+
usingClientEntryPoint: false,
23+
// Keep in sync with ReactTestUtils.js.
24+
// This is an array for better minification.
25+
Events: [
26+
getInstanceFromNode,
27+
getNodeFromInstance,
28+
getFiberCurrentPropsFromNode,
29+
enqueueStateRestore,
30+
restoreStateIfNeeded,
31+
batchedUpdates,
32+
],
33+
};
34+
35+
export default Internals;

packages/react-dom/src/client/ReactDOM.js

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,7 @@ import {canUseDOM} from 'shared/ExecutionEnvironment';
5050
import ReactVersion from 'shared/ReactVersion';
5151
import {enableNewReconciler} from 'shared/ReactFeatureFlags';
5252

53-
import {
54-
getInstanceFromNode,
55-
getNodeFromInstance,
56-
getFiberCurrentPropsFromNode,
57-
getClosestInstanceFromNode,
58-
} from './ReactDOMComponentTree';
53+
import {getClosestInstanceFromNode} from './ReactDOMComponentTree';
5954
import {restoreControlledState} from './ReactDOMComponent';
6055
import {
6156
setAttemptSynchronousHydration,
@@ -66,11 +61,8 @@ import {
6661
setAttemptHydrationAtPriority,
6762
} from '../events/ReactDOMEventReplaying';
6863
import {setBatchingImplementation} from '../events/ReactDOMUpdateBatching';
69-
import {
70-
setRestoreImplementation,
71-
enqueueStateRestore,
72-
restoreStateIfNeeded,
73-
} from '../events/ReactDOMControlledComponent';
64+
import {setRestoreImplementation} from '../events/ReactDOMControlledComponent';
65+
import Internals from '../ReactDOMSharedInternals';
7466

7567
setAttemptSynchronousHydration(attemptSynchronousHydration);
7668
setAttemptDiscreteHydration(attemptDiscreteHydration);
@@ -133,20 +125,6 @@ function renderSubtreeIntoContainer(
133125
);
134126
}
135127

136-
const Internals = {
137-
usingClientEntryPoint: false,
138-
// Keep in sync with ReactTestUtils.js.
139-
// This is an array for better minification.
140-
Events: [
141-
getInstanceFromNode,
142-
getNodeFromInstance,
143-
getFiberCurrentPropsFromNode,
144-
enqueueStateRestore,
145-
restoreStateIfNeeded,
146-
batchedUpdates,
147-
],
148-
};
149-
150128
function createRoot(
151129
container: Element | Document | DocumentFragment,
152130
options?: CreateRootOptions,
@@ -201,7 +179,6 @@ export {
201179
createPortal,
202180
batchedUpdates as unstable_batchedUpdates,
203181
flushSync,
204-
Internals as __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
205182
ReactVersion as version,
206183
// Disabled behind disableLegacyReactDOMAPIs
207184
findDOMNode,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import * as ReactDOM from 'react-dom';
11+
12+
const ReactDOMSharedInternals =
13+
ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
14+
15+
export default ReactDOMSharedInternals;

scripts/jest/setupHostConfigs.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,10 @@ jest.mock('shared/ReactSharedInternals', () =>
148148
jest.requireActual('react/src/ReactSharedInternals')
149149
);
150150

151+
// Make it possible to import this module inside
152+
// the ReactDOM package itself.
153+
jest.mock('shared/ReactDOMSharedInternals', () =>
154+
jest.requireActual('react-dom/src/ReactDOMSharedInternals')
155+
);
156+
151157
jest.mock('scheduler', () => jest.requireActual('scheduler/unstable_mock'));

scripts/rollup/forks.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,33 @@ const forks = Object.freeze({
6767
return null;
6868
},
6969

70+
// Without this fork, importing `shared/ReactDOMSharedInternals` inside
71+
// the `react-dom` package itself would not work due to a cyclical dependency.
72+
'./packages/shared/ReactDOMSharedInternals.js': (
73+
bundleType,
74+
entry,
75+
dependencies
76+
) => {
77+
if (entry === 'react-dom') {
78+
return './packages/react-dom/src/ReactDOMSharedInternals.js';
79+
}
80+
if (
81+
!entry.startsWith('react-dom/') &&
82+
dependencies.indexOf('react-dom') === -1
83+
) {
84+
// React DOM internals are unavailable if we can't reference the package.
85+
// We return an error because we only want to throw if this module gets used.
86+
return new Error(
87+
'Cannot use a module that depends on ReactDOMSharedInternals ' +
88+
'from "' +
89+
entry +
90+
'" because it does not declare "react-dom" in the package ' +
91+
'dependencies or peerDependencies.'
92+
);
93+
}
94+
return null;
95+
},
96+
7097
// We have a few forks for different environments.
7198
'./packages/shared/ReactFeatureFlags.js': (bundleType, entry) => {
7299
switch (entry) {

scripts/shared/inlinedHostConfigs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module.exports = [
3636
'react-devtools-shell',
3737
'react-devtools-shared',
3838
'react-interactions',
39+
'shared/ReactDOMSharedInternals',
3940
],
4041
isFlowTyped: true,
4142
isServerSupported: true,
@@ -66,6 +67,7 @@ module.exports = [
6667
'react-devtools-core',
6768
'react-devtools-shell',
6869
'react-devtools-shared',
70+
'shared/ReactDOMSharedInternals',
6971
],
7072
isFlowTyped: true,
7173
isServerSupported: true,
@@ -85,6 +87,7 @@ module.exports = [
8587
'react-dom/src/server/ReactDOMLegacyServerNode.classic.fb.js',
8688
'react-dom/src/server/ReactDOMLegacyServerNodeStream.js', // file indirection to support partial forking of some methods in *Node
8789
'react-client/src/ReactFlightClientStream.js', // We can only type check this in streaming configurations.
90+
'shared/ReactDOMSharedInternals',
8891
],
8992
isFlowTyped: true,
9093
isServerSupported: true,

0 commit comments

Comments
 (0)