Skip to content

Commit

Permalink
Move client only exports to react-dom/client
Browse files Browse the repository at this point in the history
This change updates the entrypoints for `react-dom` to only include exports which make sense in every runtime (Flight, Fizz, and Fiber). The main benefit to doing this is we stop including the entire client build when importing anything from `react-dom`. The server-rendering-stub was added as a manual way of doing this prior to the next major and now that stub simply reexports from `react-dom`. In a future major we will remove the stub altogether.

This change affects the OSS channels but does not update how the meta entrypoints are organized
  • Loading branch information
gnoff committed Apr 24, 2024
1 parent 6f18664 commit 4ac97cc
Show file tree
Hide file tree
Showing 44 changed files with 453 additions and 836 deletions.
42 changes: 42 additions & 0 deletions packages/react-dom-bindings/src/client/ReactDOMContainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright (c) Meta Platforms, Inc. and 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 {disableCommentsAsDOMContainers} from 'shared/ReactFeatureFlags';

import {
ELEMENT_NODE,
COMMENT_NODE,
DOCUMENT_NODE,
DOCUMENT_FRAGMENT_NODE,
} from './HTMLNodeType';

export function isValidContainer(node: any): boolean {
return !!(
node &&
(node.nodeType === ELEMENT_NODE ||
node.nodeType === DOCUMENT_NODE ||
node.nodeType === DOCUMENT_FRAGMENT_NODE ||
(!disableCommentsAsDOMContainers &&
node.nodeType === COMMENT_NODE &&
(node: any).nodeValue === ' react-mount-point-unstable '))
);
}

// TODO: Remove this function which also includes comment nodes.
// We only use it in places that are currently more relaxed.
export function isValidContainerLegacy(node: any): boolean {
return !!(
node &&
(node.nodeType === ELEMENT_NODE ||
node.nodeType === DOCUMENT_NODE ||
node.nodeType === DOCUMENT_FRAGMENT_NODE ||
(node.nodeType === COMMENT_NODE &&
(node: any).nodeValue === ' react-mount-point-unstable '))
);
}
48 changes: 1 addition & 47 deletions packages/react-dom/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,4 @@
* @flow
*/

'use strict';

import type {ReactNodeList} from 'shared/ReactTypes';
import type {
RootType,
HydrateRootOptions,
CreateRootOptions,
} from './src/client/ReactDOMRoot';

import {
createRoot as createRootImpl,
hydrateRoot as hydrateRootImpl,
__DOM_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE as Internals,
} from './';

export function createRoot(
container: Element | Document | DocumentFragment,
options?: CreateRootOptions,
): RootType {
if (__DEV__) {
Internals.usingClientEntryPoint = true;
}
try {
return createRootImpl(container, options);
} finally {
if (__DEV__) {
Internals.usingClientEntryPoint = false;
}
}
}

export function hydrateRoot(
container: Document | Element,
children: ReactNodeList,
options?: HydrateRootOptions,
): RootType {
if (__DEV__) {
Internals.usingClientEntryPoint = true;
}
try {
return hydrateRootImpl(container, children, options);
} finally {
if (__DEV__) {
Internals.usingClientEntryPoint = false;
}
}
}
export {createRoot, hydrateRoot, version} from './src/client/ReactDOMClient';
28 changes: 0 additions & 28 deletions packages/react-dom/index.experimental.js

This file was deleted.

16 changes: 5 additions & 11 deletions packages/react-dom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,19 @@
* @flow
*/

// Export all exports so that they're available in tests.
// We can't use export * from in Flow for some reason.
export {default as __DOM_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE} from './src/ReactDOMSharedInternals';
export {
createPortal,
createRoot,
hydrateRoot,
flushSync,
unstable_batchedUpdates,
unstable_createEventHandle,
unstable_runWithPriority, // DO NOT USE: Temporarily exposed to migrate off of Scheduler.runWithPriority.
useFormStatus,
useFormState,
requestFormReset,
prefetchDNS,
preconnect,
preload,
preloadModule,
preinit,
preinitModule,
requestFormReset,
unstable_batchedUpdates,
useFormState,
useFormStatus,
version,
} from './src/client/ReactDOM';
} from './src/shared/ReactDOM';
27 changes: 0 additions & 27 deletions packages/react-dom/index.stable.js

This file was deleted.

53 changes: 33 additions & 20 deletions packages/react-dom/npm/client.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
'use strict';

var m = require('react-dom');
function checkDCE() {
/* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
if (
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ === 'undefined' ||
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.checkDCE !== 'function'
) {
return;
}
if (process.env.NODE_ENV !== 'production') {
// This branch is unreachable because this function is only called
// in production, but the condition is true only in development.
// Therefore if the branch is still here, dead code elimination wasn't
// properly applied.
// Don't change the message. React DevTools relies on it. Also make sure
// this message doesn't occur elsewhere in this function, or it will cause
// a false positive.
throw new Error('^_^');
}
try {
// Verify that the code above has been dead code eliminated (DCE'd).
__REACT_DEVTOOLS_GLOBAL_HOOK__.checkDCE(checkDCE);
} catch (err) {
// DevTools shouldn't crash React, no matter what.
// We should still report in case we break this code.
console.error(err);
}
}

if (process.env.NODE_ENV === 'production') {
exports.createRoot = m.createRoot;
exports.hydrateRoot = m.hydrateRoot;
// DCE check should happen before ReactDOM bundle executes so that
// DevTools can report bad minification during injection.
checkDCE();
module.exports = require('./cjs/react-dom-client.production.js');
} else {
var i = m.__DOM_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE;
exports.createRoot = function (c, o) {
i.usingClientEntryPoint = true;
try {
return m.createRoot(c, o);
} finally {
i.usingClientEntryPoint = false;
}
};
exports.hydrateRoot = function (c, h, o) {
i.usingClientEntryPoint = true;
try {
return m.hydrateRoot(c, h, o);
} finally {
i.usingClientEntryPoint = false;
}
};
module.exports = require('./cjs/react-dom-client.development.js');
}
4 changes: 2 additions & 2 deletions packages/react-dom/npm/profiling.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ if (process.env.NODE_ENV === 'production') {
// DCE check should happen before ReactDOM bundle executes so that
// DevTools can report bad minification during injection.
checkDCE();
module.exports = require('./cjs/react-dom.profiling.js');
module.exports = require('./cjs/react-dom-profiling.profiling.js');
} else {
module.exports = require('./cjs/react-dom.development.js');
module.exports = require('./cjs/react-dom-profiling.development.js');
}
7 changes: 0 additions & 7 deletions packages/react-dom/npm/server-rendering-stub.js

This file was deleted.

2 changes: 0 additions & 2 deletions packages/react-dom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"profiling.js",
"profiling.react-server.js",
"react-dom.react-server.js",
"server-rendering-stub.js",
"server.browser.js",
"server.bun.js",
"server.edge.js",
Expand Down Expand Up @@ -107,7 +106,6 @@
"react-server": "./static.react-server.js",
"default": "./static.node.js"
},
"./server-rendering-stub": "./server-rendering-stub.js",
"./profiling": {
"react-server": "./profiling.react-server.js",
"default": "./profiling.js"
Expand Down
12 changes: 12 additions & 0 deletions packages/react-dom/profiling.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

// This entrypoint should track the /client entrypoint
export * from './client';
export * from './index';
61 changes: 0 additions & 61 deletions packages/react-dom/server-rendering-stub.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import {isEnabled} from 'react-dom-bindings/src/events/ReactDOMEventListener';

import Internals from './src/ReactDOMSharedInternalsFB';
import Internals from './ReactDOMSharedInternalsFB';

// For classic WWW builds, include a few internals that are already in use.
Object.assign((Internals: any), {
Expand All @@ -18,6 +18,8 @@ Object.assign((Internals: any), {
},
});

export {Internals as __DOM_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE};

export {
createPortal,
flushSync,
Expand All @@ -33,7 +35,7 @@ export {
preinit,
preinitModule,
version,
} from './src/client/ReactDOMFB';
} from './client/ReactDOMClientFB';

export {
createRoot,
Expand All @@ -43,6 +45,4 @@ export {
findDOMNode,
unstable_renderSubtreeIntoContainer,
unmountComponentAtNode,
} from './src/client/ReactDOMRootFB';

export {Internals as __DOM_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE};
} from './client/ReactDOMRootFB';
Loading

0 comments on commit 4ac97cc

Please sign in to comment.