Skip to content

Commit c771016

Browse files
authored
Rename The Secret Export of Server Internals (#28786)
We have a different set of dispatchers that Flight uses. This also includes the `jsx-runtime` which must also be aliased to use the right version. To ensure the right versions are used together we rename the export of the SharedInternals from 'react' and alias it in relevant bundles.
1 parent d50323e commit c771016

File tree

8 files changed

+62
-5
lines changed

8 files changed

+62
-5
lines changed

packages/react-client/src/__tests__/ReactFlight-test.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ describe('ReactFlight', () => {
5757
ReactNoopFlightServer = require('react-noop-renderer/flight-server');
5858
// This stores the state so we need to preserve it
5959
const flightModules = require('react-noop-renderer/flight-modules');
60-
__unmockReact();
6160
jest.resetModules();
61+
__unmockReact();
6262
jest.mock('react-noop-renderer/flight-modules', () => flightModules);
6363
React = require('react');
6464
startTransition = React.startTransition;
@@ -2161,7 +2161,9 @@ describe('ReactFlight', () => {
21612161
jest.mock('react', () => require('react/react.react-server'));
21622162
ReactServer = require('react');
21632163
ReactNoopFlightServer = require('react-noop-renderer/flight-server');
2164-
transport = ReactNoopFlightServer.render({root: <ServerComponent />});
2164+
transport = ReactNoopFlightServer.render({
2165+
root: ReactServer.createElement(ServerComponent),
2166+
});
21652167
}).toErrorDev('err');
21662168

21672169
expect(mockConsoleLog).toHaveBeenCalledTimes(1);

packages/react-reconciler/src/__tests__/ReactCache-test.js

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ describe('ReactCache', () => {
2525
ReactNoopFlightClient = require('react-noop-renderer/flight-client');
2626

2727
cache = React.cache;
28+
29+
jest.resetModules();
30+
__unmockReact();
2831
});
2932

3033
// @gate enableCache

packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMReply-test.js

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ describe('ReactFlightDOMReply', () => {
3535
React = require('react');
3636
ReactServerDOMServer = require('react-server-dom-webpack/server.browser');
3737
jest.resetModules();
38+
__unmockReact();
3839
ReactServerDOMClient = require('react-server-dom-webpack/client');
3940
});
4041

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and 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 React from 'react';
11+
12+
const ReactSharedInternalsServer =
13+
// $FlowFixMe: It's defined in the one we resolve to.
14+
React.__SECRET_SERVER_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
15+
16+
if (!ReactSharedInternalsServer) {
17+
throw new Error(
18+
'The "react" package in this environment is not configured correctly. ' +
19+
'The "react-server" condition must be enabled in any environment that ' +
20+
'runs React Server Components.',
21+
);
22+
}
23+
24+
export default ReactSharedInternalsServer;

packages/react/src/ReactServer.experimental.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// Patch fetch
1111
import './ReactFetch';
1212

13-
export {default as __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED} from './ReactSharedInternalsServer';
13+
export {default as __SECRET_SERVER_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED} from './ReactSharedInternalsServer';
1414

1515
import {forEach, map, count, toArray, only} from './ReactChildren';
1616
import {

packages/react/src/ReactServer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// Patch fetch
1111
import './ReactFetch';
1212

13-
export {default as __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED} from './ReactSharedInternalsServer';
13+
export {default as __SECRET_SERVER_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED} from './ReactSharedInternalsServer';
1414

1515
import {forEach, map, count, toArray, only} from './ReactChildren';
1616
import {

scripts/rollup/bundles.js

+22
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ const bundles = [
9898
moduleType: ISOMORPHIC,
9999
entry: 'react/src/ReactServer.js',
100100
name: 'react.react-server',
101+
condition: 'react-server',
101102
global: 'React',
102103
minifyWithProdErrorCodes: true,
103104
wrapWithModuleBoundaries: false,
@@ -109,6 +110,7 @@ const bundles = [
109110
bundleTypes: __EXPERIMENTAL__ ? [FB_WWW_DEV, FB_WWW_PROD] : [],
110111
moduleType: ISOMORPHIC,
111112
entry: 'react/src/ReactServerFB.js',
113+
condition: 'react-server',
112114
global: 'ReactServer',
113115
minifyWithProdErrorCodes: true,
114116
wrapWithModuleBoundaries: false,
@@ -140,6 +142,7 @@ const bundles = [
140142
moduleType: ISOMORPHIC,
141143
entry: 'react/src/jsx/ReactJSXServer.js',
142144
name: 'react-jsx-runtime.react-server',
145+
condition: 'react-server',
143146
global: 'JSXRuntime',
144147
minifyWithProdErrorCodes: false,
145148
wrapWithModuleBoundaries: false,
@@ -194,6 +197,7 @@ const bundles = [
194197
moduleType: RENDERER,
195198
entry: 'react-dom/src/ReactDOMServer.js',
196199
name: 'react-dom.react-server',
200+
condition: 'react-server',
197201
global: 'ReactDOM',
198202
minifyWithProdErrorCodes: false,
199203
wrapWithModuleBoundaries: false,
@@ -289,6 +293,7 @@ const bundles = [
289293
bundleTypes: __EXPERIMENTAL__ ? [FB_WWW_DEV, FB_WWW_PROD] : [],
290294
moduleType: RENDERER,
291295
entry: 'react-server-dom-fb/src/ReactDOMServerFB.js',
296+
condition: 'react-server',
292297
global: 'ReactDOMServerStreaming',
293298
minifyWithProdErrorCodes: false,
294299
wrapWithModuleBoundaries: false,
@@ -350,6 +355,7 @@ const bundles = [
350355
bundleTypes: [NODE_DEV, NODE_PROD, UMD_DEV, UMD_PROD],
351356
moduleType: RENDERER,
352357
entry: 'react-server-dom-webpack/server.browser',
358+
condition: 'react-server',
353359
global: 'ReactServerDOMServer',
354360
minifyWithProdErrorCodes: false,
355361
wrapWithModuleBoundaries: false,
@@ -359,6 +365,7 @@ const bundles = [
359365
bundleTypes: [NODE_DEV, NODE_PROD],
360366
moduleType: RENDERER,
361367
entry: 'react-server-dom-webpack/server.node',
368+
condition: 'react-server',
362369
global: 'ReactServerDOMServer',
363370
minifyWithProdErrorCodes: false,
364371
wrapWithModuleBoundaries: false,
@@ -368,6 +375,7 @@ const bundles = [
368375
bundleTypes: [NODE_DEV, NODE_PROD],
369376
moduleType: RENDERER,
370377
entry: 'react-server-dom-webpack/server.node.unbundled',
378+
condition: 'react-server',
371379
global: 'ReactServerDOMServer',
372380
minifyWithProdErrorCodes: false,
373381
wrapWithModuleBoundaries: false,
@@ -377,6 +385,7 @@ const bundles = [
377385
bundleTypes: [NODE_DEV, NODE_PROD],
378386
moduleType: RENDERER,
379387
entry: 'react-server-dom-webpack/server.edge',
388+
condition: 'react-server',
380389
global: 'ReactServerDOMServer',
381390
minifyWithProdErrorCodes: false,
382391
wrapWithModuleBoundaries: false,
@@ -437,6 +446,7 @@ const bundles = [
437446
bundleTypes: [ESM_PROD],
438447
moduleType: RENDERER_UTILS,
439448
entry: 'react-server-dom-webpack/node-loader',
449+
condition: 'react-server',
440450
global: 'ReactServerWebpackNodeLoader',
441451
minifyWithProdErrorCodes: false,
442452
wrapWithModuleBoundaries: false,
@@ -449,6 +459,7 @@ const bundles = [
449459
moduleType: RENDERER_UTILS,
450460
entry: 'react-server-dom-webpack/src/ReactFlightWebpackNodeRegister',
451461
name: 'react-server-dom-webpack-node-register',
462+
condition: 'react-server',
452463
global: 'ReactFlightWebpackNodeRegister',
453464
minifyWithProdErrorCodes: false,
454465
wrapWithModuleBoundaries: false,
@@ -460,6 +471,7 @@ const bundles = [
460471
bundleTypes: [NODE_DEV, NODE_PROD, UMD_DEV, UMD_PROD],
461472
moduleType: RENDERER,
462473
entry: 'react-server-dom-turbopack/server.browser',
474+
condition: 'react-server',
463475
global: 'ReactServerDOMServer',
464476
minifyWithProdErrorCodes: false,
465477
wrapWithModuleBoundaries: false,
@@ -469,6 +481,7 @@ const bundles = [
469481
bundleTypes: [NODE_DEV, NODE_PROD],
470482
moduleType: RENDERER,
471483
entry: 'react-server-dom-turbopack/server.node',
484+
condition: 'react-server',
472485
global: 'ReactServerDOMServer',
473486
minifyWithProdErrorCodes: false,
474487
wrapWithModuleBoundaries: false,
@@ -478,6 +491,7 @@ const bundles = [
478491
bundleTypes: [NODE_DEV, NODE_PROD],
479492
moduleType: RENDERER,
480493
entry: 'react-server-dom-turbopack/server.node.unbundled',
494+
condition: 'react-server',
481495
global: 'ReactServerDOMServer',
482496
minifyWithProdErrorCodes: false,
483497
wrapWithModuleBoundaries: false,
@@ -487,6 +501,7 @@ const bundles = [
487501
bundleTypes: [NODE_DEV, NODE_PROD],
488502
moduleType: RENDERER,
489503
entry: 'react-server-dom-turbopack/server.edge',
504+
condition: 'react-server',
490505
global: 'ReactServerDOMServer',
491506
minifyWithProdErrorCodes: false,
492507
wrapWithModuleBoundaries: false,
@@ -540,6 +555,7 @@ const bundles = [
540555
bundleTypes: [ESM_PROD],
541556
moduleType: RENDERER_UTILS,
542557
entry: 'react-server-dom-turbopack/node-loader',
558+
condition: 'react-server',
543559
global: 'ReactServerTurbopackNodeLoader',
544560
minifyWithProdErrorCodes: false,
545561
wrapWithModuleBoundaries: false,
@@ -552,6 +568,7 @@ const bundles = [
552568
moduleType: RENDERER_UTILS,
553569
entry: 'react-server-dom-turbopack/src/ReactFlightTurbopackNodeRegister',
554570
name: 'react-server-dom-turbopack-node-register',
571+
condition: 'react-server',
555572
global: 'ReactFlightWebpackNodeRegister',
556573
minifyWithProdErrorCodes: false,
557574
wrapWithModuleBoundaries: false,
@@ -563,6 +580,7 @@ const bundles = [
563580
bundleTypes: [NODE_DEV, NODE_PROD],
564581
moduleType: RENDERER,
565582
entry: 'react-server-dom-esm/server.node',
583+
condition: 'react-server',
566584
minifyWithProdErrorCodes: false,
567585
wrapWithModuleBoundaries: false,
568586
externals: ['react', 'util', 'crypto', 'async_hooks', 'react-dom'],
@@ -591,6 +609,7 @@ const bundles = [
591609
bundleTypes: [ESM_PROD],
592610
moduleType: RENDERER_UTILS,
593611
entry: 'react-server-dom-esm/node-loader',
612+
condition: 'react-server',
594613
global: 'ReactServerESMNodeLoader',
595614
minifyWithProdErrorCodes: false,
596615
wrapWithModuleBoundaries: false,
@@ -602,6 +621,7 @@ const bundles = [
602621
bundleTypes: __EXPERIMENTAL__ ? [FB_WWW_DEV, FB_WWW_PROD] : [],
603622
moduleType: RENDERER,
604623
entry: 'react-server-dom-fb/src/ReactFlightDOMServerFB.js',
624+
condition: 'react-server',
605625
global: 'ReactFlightDOMServer',
606626
minifyWithProdErrorCodes: false,
607627
wrapWithModuleBoundaries: false,
@@ -796,6 +816,7 @@ const bundles = [
796816
bundleTypes: [NODE_DEV, NODE_PROD],
797817
moduleType: RENDERER,
798818
entry: 'react-noop-renderer/flight-server',
819+
condition: 'react-server',
799820
global: 'ReactNoopFlightServer',
800821
minifyWithProdErrorCodes: false,
801822
wrapWithModuleBoundaries: false,
@@ -850,6 +871,7 @@ const bundles = [
850871
bundleTypes: [NODE_DEV, NODE_PROD],
851872
moduleType: RECONCILER,
852873
entry: 'react-server/flight',
874+
condition: 'react-server',
853875
global: 'ReactFlightServer',
854876
minifyWithProdErrorCodes: false,
855877
wrapWithModuleBoundaries: false,

scripts/rollup/forks.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ const forks = Object.freeze({
5858
'./packages/shared/ReactSharedInternals.js': (
5959
bundleType,
6060
entry,
61-
dependencies
61+
dependencies,
62+
_moduleType,
63+
bundle
6264
) => {
6365
if (entry === 'react') {
6466
return './packages/react/src/ReactSharedInternalsClient.js';
@@ -69,6 +71,9 @@ const forks = Object.freeze({
6971
) {
7072
return './packages/react/src/ReactSharedInternalsServer.js';
7173
}
74+
if (bundle.condition === 'react-server') {
75+
return './packages/react-server/src/ReactSharedInternalsServer.js';
76+
}
7277
if (!entry.startsWith('react/') && dependencies.indexOf('react') === -1) {
7378
// React internals are unavailable if we can't reference the package.
7479
// We return an error because we only want to throw if this module gets used.

0 commit comments

Comments
 (0)