From 80db2143257a79c91f07d41cebea1a30a22af658 Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Mon, 8 Apr 2024 19:14:18 -0400 Subject: [PATCH] Rename internals for the react-server condition --- .../src/__tests__/ReactFlight-test.js | 6 +++-- .../src/ReactSharedInternalsServer.js | 24 +++++++++++++++++++ .../react/src/ReactServer.experimental.js | 2 +- packages/react/src/ReactServer.js | 2 +- scripts/rollup/bundles.js | 22 +++++++++++++++++ scripts/rollup/forks.js | 7 +++++- 6 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 packages/react-server/src/ReactSharedInternalsServer.js diff --git a/packages/react-client/src/__tests__/ReactFlight-test.js b/packages/react-client/src/__tests__/ReactFlight-test.js index 263709a6321fc..2164ca0b2781a 100644 --- a/packages/react-client/src/__tests__/ReactFlight-test.js +++ b/packages/react-client/src/__tests__/ReactFlight-test.js @@ -57,8 +57,8 @@ describe('ReactFlight', () => { ReactNoopFlightServer = require('react-noop-renderer/flight-server'); // This stores the state so we need to preserve it const flightModules = require('react-noop-renderer/flight-modules'); - __unmockReact(); jest.resetModules(); + __unmockReact(); jest.mock('react-noop-renderer/flight-modules', () => flightModules); React = require('react'); startTransition = React.startTransition; @@ -2161,7 +2161,9 @@ describe('ReactFlight', () => { jest.mock('react', () => require('react/react.react-server')); ReactServer = require('react'); ReactNoopFlightServer = require('react-noop-renderer/flight-server'); - transport = ReactNoopFlightServer.render({root: }); + transport = ReactNoopFlightServer.render({ + root: ReactServer.createElement(ServerComponent), + }); }).toErrorDev('err'); expect(mockConsoleLog).toHaveBeenCalledTimes(1); diff --git a/packages/react-server/src/ReactSharedInternalsServer.js b/packages/react-server/src/ReactSharedInternalsServer.js new file mode 100644 index 0000000000000..cf356f36a5423 --- /dev/null +++ b/packages/react-server/src/ReactSharedInternalsServer.js @@ -0,0 +1,24 @@ +/** + * 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 * as React from 'react'; + +const ReactSharedInternalsServer = + // $FlowFixMe: It's defined in the one we resolve to. + React.__SECRET_SERVER_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED; + +if (!ReactSharedInternalsServer) { + throw new Error( + 'The "react" package in this environment is not configured correctly. ' + + 'The "react-server" condition must be enabled in any environment that ' + + 'runs React Server Components.', + ); +} + +export default ReactSharedInternalsServer; diff --git a/packages/react/src/ReactServer.experimental.js b/packages/react/src/ReactServer.experimental.js index 76b11004b2cc9..c6036c79611ca 100644 --- a/packages/react/src/ReactServer.experimental.js +++ b/packages/react/src/ReactServer.experimental.js @@ -10,7 +10,7 @@ // Patch fetch import './ReactFetch'; -export {default as __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED} from './ReactSharedInternalsServer'; +export {default as __SECRET_SERVER_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED} from './ReactSharedInternalsServer'; import {forEach, map, count, toArray, only} from './ReactChildren'; import { diff --git a/packages/react/src/ReactServer.js b/packages/react/src/ReactServer.js index a63651d54c33a..fe1d13e9e5f23 100644 --- a/packages/react/src/ReactServer.js +++ b/packages/react/src/ReactServer.js @@ -10,7 +10,7 @@ // Patch fetch import './ReactFetch'; -export {default as __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED} from './ReactSharedInternalsServer'; +export {default as __SECRET_SERVER_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED} from './ReactSharedInternalsServer'; import {forEach, map, count, toArray, only} from './ReactChildren'; import { diff --git a/scripts/rollup/bundles.js b/scripts/rollup/bundles.js index 683208d1fd584..b5755725124aa 100644 --- a/scripts/rollup/bundles.js +++ b/scripts/rollup/bundles.js @@ -98,6 +98,7 @@ const bundles = [ moduleType: ISOMORPHIC, entry: 'react/src/ReactServer.js', name: 'react.react-server', + condition: 'react-server', global: 'React', minifyWithProdErrorCodes: true, wrapWithModuleBoundaries: false, @@ -109,6 +110,7 @@ const bundles = [ bundleTypes: __EXPERIMENTAL__ ? [FB_WWW_DEV, FB_WWW_PROD] : [], moduleType: ISOMORPHIC, entry: 'react/src/ReactServerFB.js', + condition: 'react-server', global: 'ReactServer', minifyWithProdErrorCodes: true, wrapWithModuleBoundaries: false, @@ -140,6 +142,7 @@ const bundles = [ moduleType: ISOMORPHIC, entry: 'react/src/jsx/ReactJSXServer.js', name: 'react-jsx-runtime.react-server', + condition: 'react-server', global: 'JSXRuntime', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, @@ -194,6 +197,7 @@ const bundles = [ moduleType: RENDERER, entry: 'react-dom/src/ReactDOMServer.js', name: 'react-dom.react-server', + condition: 'react-server', global: 'ReactDOM', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, @@ -289,6 +293,7 @@ const bundles = [ bundleTypes: __EXPERIMENTAL__ ? [FB_WWW_DEV, FB_WWW_PROD] : [], moduleType: RENDERER, entry: 'react-server-dom-fb/src/ReactDOMServerFB.js', + condition: 'react-server', global: 'ReactDOMServerStreaming', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, @@ -350,6 +355,7 @@ const bundles = [ bundleTypes: [NODE_DEV, NODE_PROD, UMD_DEV, UMD_PROD], moduleType: RENDERER, entry: 'react-server-dom-webpack/server.browser', + condition: 'react-server', global: 'ReactServerDOMServer', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, @@ -359,6 +365,7 @@ const bundles = [ bundleTypes: [NODE_DEV, NODE_PROD], moduleType: RENDERER, entry: 'react-server-dom-webpack/server.node', + condition: 'react-server', global: 'ReactServerDOMServer', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, @@ -368,6 +375,7 @@ const bundles = [ bundleTypes: [NODE_DEV, NODE_PROD], moduleType: RENDERER, entry: 'react-server-dom-webpack/server.node.unbundled', + condition: 'react-server', global: 'ReactServerDOMServer', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, @@ -377,6 +385,7 @@ const bundles = [ bundleTypes: [NODE_DEV, NODE_PROD], moduleType: RENDERER, entry: 'react-server-dom-webpack/server.edge', + condition: 'react-server', global: 'ReactServerDOMServer', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, @@ -437,6 +446,7 @@ const bundles = [ bundleTypes: [ESM_PROD], moduleType: RENDERER_UTILS, entry: 'react-server-dom-webpack/node-loader', + condition: 'react-server', global: 'ReactServerWebpackNodeLoader', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, @@ -449,6 +459,7 @@ const bundles = [ moduleType: RENDERER_UTILS, entry: 'react-server-dom-webpack/src/ReactFlightWebpackNodeRegister', name: 'react-server-dom-webpack-node-register', + condition: 'react-server', global: 'ReactFlightWebpackNodeRegister', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, @@ -460,6 +471,7 @@ const bundles = [ bundleTypes: [NODE_DEV, NODE_PROD, UMD_DEV, UMD_PROD], moduleType: RENDERER, entry: 'react-server-dom-turbopack/server.browser', + condition: 'react-server', global: 'ReactServerDOMServer', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, @@ -469,6 +481,7 @@ const bundles = [ bundleTypes: [NODE_DEV, NODE_PROD], moduleType: RENDERER, entry: 'react-server-dom-turbopack/server.node', + condition: 'react-server', global: 'ReactServerDOMServer', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, @@ -478,6 +491,7 @@ const bundles = [ bundleTypes: [NODE_DEV, NODE_PROD], moduleType: RENDERER, entry: 'react-server-dom-turbopack/server.node.unbundled', + condition: 'react-server', global: 'ReactServerDOMServer', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, @@ -487,6 +501,7 @@ const bundles = [ bundleTypes: [NODE_DEV, NODE_PROD], moduleType: RENDERER, entry: 'react-server-dom-turbopack/server.edge', + condition: 'react-server', global: 'ReactServerDOMServer', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, @@ -540,6 +555,7 @@ const bundles = [ bundleTypes: [ESM_PROD], moduleType: RENDERER_UTILS, entry: 'react-server-dom-turbopack/node-loader', + condition: 'react-server', global: 'ReactServerTurbopackNodeLoader', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, @@ -552,6 +568,7 @@ const bundles = [ moduleType: RENDERER_UTILS, entry: 'react-server-dom-turbopack/src/ReactFlightTurbopackNodeRegister', name: 'react-server-dom-turbopack-node-register', + condition: 'react-server', global: 'ReactFlightWebpackNodeRegister', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, @@ -563,6 +580,7 @@ const bundles = [ bundleTypes: [NODE_DEV, NODE_PROD], moduleType: RENDERER, entry: 'react-server-dom-esm/server.node', + condition: 'react-server', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, externals: ['react', 'util', 'crypto', 'async_hooks', 'react-dom'], @@ -591,6 +609,7 @@ const bundles = [ bundleTypes: [ESM_PROD], moduleType: RENDERER_UTILS, entry: 'react-server-dom-esm/node-loader', + condition: 'react-server', global: 'ReactServerESMNodeLoader', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, @@ -602,6 +621,7 @@ const bundles = [ bundleTypes: __EXPERIMENTAL__ ? [FB_WWW_DEV, FB_WWW_PROD] : [], moduleType: RENDERER, entry: 'react-server-dom-fb/src/ReactFlightDOMServerFB.js', + condition: 'react-server', global: 'ReactFlightDOMServer', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, @@ -796,6 +816,7 @@ const bundles = [ bundleTypes: [NODE_DEV, NODE_PROD], moduleType: RENDERER, entry: 'react-noop-renderer/flight-server', + condition: 'react-server', global: 'ReactNoopFlightServer', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, @@ -850,6 +871,7 @@ const bundles = [ bundleTypes: [NODE_DEV, NODE_PROD], moduleType: RECONCILER, entry: 'react-server/flight', + condition: 'react-server', global: 'ReactFlightServer', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, diff --git a/scripts/rollup/forks.js b/scripts/rollup/forks.js index 7cfdf58965d5d..f12f36744b5c9 100644 --- a/scripts/rollup/forks.js +++ b/scripts/rollup/forks.js @@ -58,7 +58,9 @@ const forks = Object.freeze({ './packages/shared/ReactSharedInternals.js': ( bundleType, entry, - dependencies + dependencies, + _moduleType, + bundle ) => { if (entry === 'react') { return './packages/react/src/ReactSharedInternalsClient.js'; @@ -69,6 +71,9 @@ const forks = Object.freeze({ ) { return './packages/react/src/ReactSharedInternalsServer.js'; } + if (bundle.condition === 'react-server') { + return './packages/react-server/src/ReactSharedInternalsServer.js'; + } if (!entry.startsWith('react/') && dependencies.indexOf('react') === -1) { // React 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.