[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
Josh Story committed
Sep 15, 2022 at 15:31 UTC
e7fc04b2970197cb953833aa15597cbc9fe38f2e
11 files changed
+97
-33
packages/react-dom/index.classic.fb.js
+4
-3
@@ -9,17 +9,16 @@
9
10
import {isEnabled} from './src/events/ReactDOMEventListener';
11
12
-import {__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED} from './src/client/ReactDOM';
12
+import Internals from './src/ReactDOMSharedInternals';
13
14
// 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), {
16
ReactBrowserEventEmitter: {
17
isEnabled,
18
},
19
});
20
21
export {
22
- __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
22
createPortal,
23
createRoot,
24
hydrateRoot,
@@ -36,3 +35,5 @@ export {
35
unstable_runWithPriority, // DO NOT USE: Temporarily exposed to migrate off of Scheduler.runWithPriority.
36
version,
37
} 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
+1
-1
@@ -7,8 +7,8 @@
7
* @flow
8
*/
9
10
+export {default as __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED} from './src/ReactDOMSharedInternals';
11
export {
11
- __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
12
createPortal,
13
createRoot,
14
hydrateRoot,
packages/react-dom/index.js
+1
-1
@@ -9,8 +9,8 @@
9
10
// Export all exports so that they're available in tests.
11
// 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';
13
export {
13
- __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
14
createPortal,
15
createRoot,
16
hydrateRoot,
packages/react-dom/index.modern.fb.js
+1
-1
@@ -7,8 +7,8 @@
7
* @flow
8
*/
9
10
+export {default as __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED} from './src/ReactDOMSharedInternals';
11
export {
11
- __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
12
createPortal,
13
createRoot,
14
hydrateRoot,
packages/react-dom/index.stable.js
+1
-1
@@ -7,8 +7,8 @@
7
* @flow
8
*/
9
10
+export {default as __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED} from './src/ReactDOMSharedInternals';
11
export {
11
- __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
12
createPortal,
13
createRoot,
14
hydrateRoot,
packages/react-dom/src/ReactDOMSharedInternals.js
new
+35
@@ -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
+3
-26
@@ -50,12 +50,7 @@ import {canUseDOM} from 'shared/ExecutionEnvironment';
50
import ReactVersion from 'shared/ReactVersion';
51
import {enableNewReconciler} from 'shared/ReactFeatureFlags';
52
53
-import {
54
- getInstanceFromNode,
55
- getNodeFromInstance,
56
- getFiberCurrentPropsFromNode,
57
- getClosestInstanceFromNode,
58
-} from './ReactDOMComponentTree';
53
+import {getClosestInstanceFromNode} from './ReactDOMComponentTree';
54
import {restoreControlledState} from './ReactDOMComponent';
55
import {
56
setAttemptSynchronousHydration,
@@ -66,11 +61,8 @@ import {
61
setAttemptHydrationAtPriority,
62
} from '../events/ReactDOMEventReplaying';
63
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';
66
67
setAttemptSynchronousHydration(attemptSynchronousHydration);
68
setAttemptDiscreteHydration(attemptDiscreteHydration);
@@ -133,20 +125,6 @@ function renderSubtreeIntoContainer(
125
);
126
}
127
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
-
128
function createRoot(
129
container: Element | Document | DocumentFragment,
130
options?: CreateRootOptions,
@@ -201,7 +179,6 @@ export {
179
createPortal,
180
batchedUpdates as unstable_batchedUpdates,
181
flushSync,
204
- Internals as __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
182
ReactVersion as version,
183
// Disabled behind disableLegacyReactDOMAPIs
184
findDOMNode,
packages/shared/ReactDOMSharedInternals.js
new
+15
@@ -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
+6
@@ -148,4 +148,10 @@ jest.mock('shared/ReactSharedInternals', () =>
148
jest.requireActual('react/src/ReactSharedInternals')
149
);
150
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
+
157
jest.mock('scheduler', () => jest.requireActual('scheduler/unstable_mock'));
scripts/rollup/forks.js
+27
@@ -67,6 +67,33 @@ const forks = Object.freeze({
67
return null;
68
},
69
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
+
97
// We have a few forks for different environments.
98
'./packages/shared/ReactFeatureFlags.js': (bundleType, entry) => {
99
switch (entry) {
scripts/shared/inlinedHostConfigs.js
+3
@@ -36,6 +36,7 @@ module.exports = [
36
'react-devtools-shell',
37
'react-devtools-shared',
38
'react-interactions',
39
+ 'shared/ReactDOMSharedInternals',
40
],
41
isFlowTyped: true,
42
isServerSupported: true,
@@ -66,6 +67,7 @@ module.exports = [
67
'react-devtools-core',
68
'react-devtools-shell',
69
'react-devtools-shared',
70
+ 'shared/ReactDOMSharedInternals',
71
],
72
isFlowTyped: true,
73
isServerSupported: true,
@@ -85,6 +87,7 @@ module.exports = [
87
'react-dom/src/server/ReactDOMLegacyServerNode.classic.fb.js',
88
'react-dom/src/server/ReactDOMLegacyServerNodeStream.js', // file indirection to support partial forking of some methods in *Node
89
'react-client/src/ReactFlightClientStream.js', // We can only type check this in streaming configurations.
90
+ 'shared/ReactDOMSharedInternals',
91
],
92
isFlowTyped: true,
93
isServerSupported: true,