[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
This commit is contained in:
Josh Story
2022-09-15 15:31:31 -07:00
committed by GitHub
parent 3d3b229d50
commit f6df9d7d45
11 changed files with 97 additions and 33 deletions
+4 -3
View File
@@ -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,
@@ -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};
+1 -1
View File
@@ -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,
+1 -1
View File
@@ -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,
+1 -1
View File
@@ -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,
+1 -1
View File
@@ -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,
+35
View File
@@ -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;
+3 -26
View File
@@ -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,
@@ -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);
@@ -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,
@@ -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,
@@ -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
View File
@@ -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
View File
@@ -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) {
+3
View File
@@ -36,6 +36,7 @@ module.exports = [
'react-devtools-shell',
'react-devtools-shared',
'react-interactions',
'shared/ReactDOMSharedInternals',
],
isFlowTyped: true,
isServerSupported: true,
@@ -66,6 +67,7 @@ module.exports = [
'react-devtools-core',
'react-devtools-shell',
'react-devtools-shared',
'shared/ReactDOMSharedInternals',
],
isFlowTyped: true,
isServerSupported: true,
@@ -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,