Skip to content

Commit

Permalink
[react-dom] move all client code to react-dom/client (#28271)
Browse files Browse the repository at this point in the history
This PR reorganizes the `react-dom` entrypoint to only pull in code that
is environment agnostic. Previously if you required anything from this
entrypoint in any environment the entire client reconciler was loaded.
In a prior release we added a server rendering stub which you could
alias in server environments to omit this unecessary code. After landing
this change this entrypoint should not load any environment specific
code.

While a few APIs are truly client (browser) only such as createRoot and
hydrateRoot many of the APIs you import from this package are only
useful in the browser but could concievably be imported in shared code
(components running in Fizz or shared components as part of an RSC app).
To avoid making these require opting into the client bundle we are
keeping them in the `react-dom` entrypoint and changing their
implementation so that in environments where they are not particularly
useful they do something benign and expected.

#### Removed APIs
The following APIs are being removed in the next major. Largely they
have all been deprecated already and are part of legacy rendering modes
where concurrent features of React are not available
* `render`
* `hydrate`
* `findDOMNode`
* `unmountComponentAtNode`
* `unstable_createEventHandle`
* `unstable_renderSubtreeIntoContainer`
* `unstable_runWithPrioirty`

#### moved Client APIs
These APIs were available on both `react-dom` (with a warning) and
`react-dom/client`. After this change they are only available on
`react-dom/client`
* `createRoot`
* `hydrateRoot`

#### retained APIs
These APIs still exist on the `react-dom` entrypoint but have normalized
behavior depending on which renderers are currently in scope
* `flushSync`: will execute the function (if provided) inside the
flushSync implemention of FlightServer, Fizz, and Fiber DOM renderers.
* `unstable_batchedUpdates`: This is a noop in concurrent mode because
it is now the only supported behavior because there is no legacy
rendering mode
* `createPortal`: This just produces an object. It can be called from
anywhere but since you will probably not have a handle on a DOM node to
pass to it it will likely warn in environments other than the browser
* preloading APIS such as `preload`: These methods will execute the
preload across all renderers currently in scope. Since we resolve the
Request object on the server using AsyncLocalStorage or the current
function stack in practice only one renderer should act upon the
preload.

In addition to these changes the server rendering stub now just rexports
everything from `react-dom`. In a future minor we will add a warning
when using the stub and in the next major we will remove the stub
altogether
  • Loading branch information
gnoff committed Apr 24, 2024
1 parent b039be6 commit cb15184
Show file tree
Hide file tree
Showing 51 changed files with 1,715 additions and 2,052 deletions.
2,368 changes: 1,204 additions & 1,164 deletions packages/react-devtools-shared/src/__tests__/TimelineProfiler-test.js

Large diffs are not rendered by default.

70 changes: 35 additions & 35 deletions packages/react-devtools-shared/src/__tests__/preprocessData-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -841,83 +841,83 @@ describe('Timeline profiler', () => {
{
"batchUID": 0,
"depth": 0,
"duration": 0.012,
"duration": 0.014,
"lanes": "0b0000000000000000000000000000101",
"timestamp": 0.006,
"timestamp": 0.008,
"type": "render-idle",
},
{
"batchUID": 0,
"depth": 0,
"duration": 0.003,
"lanes": "0b0000000000000000000000000000101",
"timestamp": 0.006,
"timestamp": 0.008,
"type": "render",
},
{
"batchUID": 0,
"depth": 0,
"duration": 0.008,
"duration": 0.010,
"lanes": "0b0000000000000000000000000000101",
"timestamp": 0.01,
"timestamp": 0.012,
"type": "commit",
},
{
"batchUID": 0,
"depth": 1,
"duration": 0.001,
"lanes": "0b0000000000000000000000000000101",
"timestamp": 0.016,
"timestamp": 0.02,
"type": "layout-effects",
},
{
"batchUID": 0,
"depth": 0,
"duration": 0.004,
"lanes": "0b0000000000000000000000000000101",
"timestamp": 0.019,
"timestamp": 0.023,
"type": "passive-effects",
},
],
1 => [
{
"batchUID": 1,
"depth": 0,
"duration": 0.012,
"duration": 0.014,
"lanes": "0b0000000000000000000000000000101",
"timestamp": 0.024,
"timestamp": 0.028,
"type": "render-idle",
},
{
"batchUID": 1,
"depth": 0,
"duration": 0.003,
"lanes": "0b0000000000000000000000000000101",
"timestamp": 0.024,
"timestamp": 0.028,
"type": "render",
},
{
"batchUID": 1,
"depth": 0,
"duration": 0.008,
"duration": 0.010,
"lanes": "0b0000000000000000000000000000101",
"timestamp": 0.028,
"timestamp": 0.032,
"type": "commit",
},
{
"batchUID": 1,
"depth": 1,
"duration": 0.001,
"lanes": "0b0000000000000000000000000000101",
"timestamp": 0.034,
"timestamp": 0.04,
"type": "layout-effects",
},
{
"batchUID": 1,
"depth": 0,
"duration": 0.003,
"lanes": "0b0000000000000000000000000000101",
"timestamp": 0.037,
"timestamp": 0.043,
"type": "passive-effects",
},
],
Expand All @@ -926,33 +926,33 @@ describe('Timeline profiler', () => {
{
"componentName": "App",
"duration": 0.001,
"timestamp": 0.007,
"timestamp": 0.009,
"type": "render",
"warning": null,
},
{
"componentName": "App",
"duration": 0.002,
"timestamp": 0.02,
"timestamp": 0.024,
"type": "passive-effect-mount",
"warning": null,
},
{
"componentName": "App",
"duration": 0.001,
"timestamp": 0.025,
"timestamp": 0.029,
"type": "render",
"warning": null,
},
{
"componentName": "App",
"duration": 0.001,
"timestamp": 0.038,
"timestamp": 0.044,
"type": "passive-effect-mount",
"warning": null,
},
],
"duration": 0.04,
"duration": 0.046,
"flamechart": [],
"internalModuleSourceToRanges": Map {
undefined => [
Expand Down Expand Up @@ -1015,81 +1015,81 @@ describe('Timeline profiler', () => {
{
"batchUID": 0,
"depth": 0,
"duration": 0.012,
"duration": 0.014,
"lanes": "0b0000000000000000000000000000101",
"timestamp": 0.006,
"timestamp": 0.008,
"type": "render-idle",
},
{
"batchUID": 0,
"depth": 0,
"duration": 0.003,
"lanes": "0b0000000000000000000000000000101",
"timestamp": 0.006,
"timestamp": 0.008,
"type": "render",
},
{
"batchUID": 0,
"depth": 0,
"duration": 0.008,
"duration": 0.010,
"lanes": "0b0000000000000000000000000000101",
"timestamp": 0.01,
"timestamp": 0.012,
"type": "commit",
},
{
"batchUID": 0,
"depth": 1,
"duration": 0.001,
"lanes": "0b0000000000000000000000000000101",
"timestamp": 0.016,
"timestamp": 0.02,
"type": "layout-effects",
},
{
"batchUID": 0,
"depth": 0,
"duration": 0.004,
"lanes": "0b0000000000000000000000000000101",
"timestamp": 0.019,
"timestamp": 0.023,
"type": "passive-effects",
},
{
"batchUID": 1,
"depth": 0,
"duration": 0.012,
"duration": 0.014,
"lanes": "0b0000000000000000000000000000101",
"timestamp": 0.024,
"timestamp": 0.028,
"type": "render-idle",
},
{
"batchUID": 1,
"depth": 0,
"duration": 0.003,
"lanes": "0b0000000000000000000000000000101",
"timestamp": 0.024,
"timestamp": 0.028,
"type": "render",
},
{
"batchUID": 1,
"depth": 0,
"duration": 0.008,
"duration": 0.010,
"lanes": "0b0000000000000000000000000000101",
"timestamp": 0.028,
"timestamp": 0.032,
"type": "commit",
},
{
"batchUID": 1,
"depth": 1,
"duration": 0.001,
"lanes": "0b0000000000000000000000000000101",
"timestamp": 0.034,
"timestamp": 0.04,
"type": "layout-effects",
},
{
"batchUID": 1,
"depth": 0,
"duration": 0.003,
"lanes": "0b0000000000000000000000000000101",
"timestamp": 0.037,
"timestamp": 0.043,
"type": "passive-effects",
},
],
Expand Down Expand Up @@ -1126,14 +1126,14 @@ describe('Timeline profiler', () => {
"schedulingEvents": [
{
"lanes": "0b0000000000000000000000000000101",
"timestamp": 0.005,
"timestamp": 0.007,
"type": "schedule-render",
"warning": null,
},
{
"componentName": "App",
"lanes": "0b0000000000000000000000000000101",
"timestamp": 0.021,
"timestamp": 0.025,
"type": "schedule-state-update",
"warning": null,
},
Expand Down
7 changes: 3 additions & 4 deletions packages/react-devtools-shell/src/e2e-regression/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
// This test harness mounts each test app as a separate root to test multi-root applications.

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import {createRoot} from 'react-dom/client';
import * as ReactDOMClient from 'react-dom/client';
import ListApp from '../e2e-apps/ListApp';

function mountApp(App: () => React$Node) {
const container = document.createElement('div');

((document.body: any): HTMLBodyElement).appendChild(container);

const root = createRoot(container);
const root = ReactDOMClient.createRoot(container);
root.render(<App />);
}
function mountTestApp() {
Expand All @@ -27,5 +26,5 @@ window.parent.REACT_DOM_APP = {
createTestNameSelector: name => `[data-testname="${name}"]`,
findAllNodes: (container, nodes) =>
container.querySelectorAll(nodes.join(' ')),
...ReactDOM,
...ReactDOMClient,
};
7 changes: 4 additions & 3 deletions packages/react-devtools-shell/src/e2e-regression/devtools.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import {createRoot} from 'react-dom/client';
import * as ReactDOMClient from 'react-dom/client';
import {
activate as activateBackend,
initialize as initializeBackend,
Expand Down Expand Up @@ -32,7 +32,7 @@ function init(appIframe, devtoolsContainer, appSource) {
const DevTools = createDevTools(contentWindow);

inject(contentDocument, appSource, () => {
createRoot(devtoolsContainer).render(
ReactDOMClient.createRoot(devtoolsContainer).render(
<DevTools
hookNamesModuleLoaderFunction={hookNamesModuleLoaderFunction}
showTabBar={true}
Expand All @@ -55,4 +55,5 @@ init(
);

// ReactDOM Test Selector APIs used by Playwright e2e tests
window.parent.REACT_DOM_DEVTOOLS = ReactDOM;
window.parent.REACT_DOM_DEVTOOLS =
'createTestNameSelector' in ReactDOMClient ? ReactDOMClient : ReactDOM;
7 changes: 3 additions & 4 deletions packages/react-devtools-shell/src/e2e/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
// This test harness mounts each test app as a separate root to test multi-root applications.

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import {createRoot} from 'react-dom/client';
import * as ReactDOMClient from 'react-dom/client';

const container = document.createElement('div');

Expand All @@ -14,8 +13,8 @@ const container = document.createElement('div');
// so that it can load things other than just ToDoList.
const App = require('../e2e-apps/ListApp').default;

const root = createRoot(container);
const root = ReactDOMClient.createRoot(container);
root.render(<App />);

// ReactDOM Test Selector APIs used by Playwright e2e tests
window.parent.REACT_DOM_APP = ReactDOM;
window.parent.REACT_DOM_APP = ReactDOMClient;
7 changes: 3 additions & 4 deletions packages/react-devtools-shell/src/e2e/devtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
*/

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import {createRoot} from 'react-dom/client';
import * as ReactDOMClient from 'react-dom/client';
import {
activate as activateBackend,
initialize as initializeBackend,
Expand Down Expand Up @@ -41,7 +40,7 @@ function init(appIframe, devtoolsContainer, appSource) {
const DevTools = createDevTools(contentWindow);

inject(contentDocument, appSource, () => {
createRoot(devtoolsContainer).render(
ReactDOMClient.createRoot(devtoolsContainer).render(
<DevTools
hookNamesModuleLoaderFunction={hookNamesModuleLoaderFunction}
showTabBar={true}
Expand All @@ -58,4 +57,4 @@ const devtoolsContainer = document.getElementById('devtools');
init(iframe, devtoolsContainer, 'dist/e2e-app.js');

// ReactDOM Test Selector APIs used by Playwright e2e tests
window.parent.REACT_DOM_DEVTOOLS = ReactDOM;
window.parent.REACT_DOM_DEVTOOLS = ReactDOMClient;
12 changes: 10 additions & 2 deletions packages/react-devtools-shell/webpack-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ const makeConfig = (entry, alias) => ({
},
});

const clientAsSeparateBuild = semver.gte(REACT_VERSION, '19.0.0');

const app = makeConfig(
{
'app-index': './src/app/index.js',
Expand All @@ -142,8 +144,14 @@ const app = makeConfig(
react: resolve(builtModulesDir, 'react'),
'react-debug-tools': resolve(builtModulesDir, 'react-debug-tools'),
'react-devtools-feature-flags': resolveFeatureFlags('shell'),
'react-dom/client': resolve(builtModulesDir, 'react-dom/client'),
'react-dom': resolve(builtModulesDir, 'react-dom/unstable_testing'),
'react-dom/client': resolve(
builtModulesDir,
clientAsSeparateBuild ? 'react-dom/unstable_testing' : 'react-dom/client',
),
'react-dom': resolve(
builtModulesDir,
clientAsSeparateBuild ? 'react-dom' : 'react-dom/unstable_testing',
),
'react-is': resolve(builtModulesDir, 'react-is'),
scheduler: resolve(builtModulesDir, 'scheduler'),
},
Expand Down
Loading

0 comments on commit cb15184

Please sign in to comment.