From 9138c6111108b8e1b7cfced77eebd3eb7430942e 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/ReactSharedInternalsServer.js | 24 ++++++++++++++ packages/react/src/ReactServer.js | 2 +- scripts/rollup/bundles.js | 31 +++++++++++++++++++ scripts/rollup/forks.js | 7 ++++- 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 packages/react-server/src/ReactSharedInternalsServer.js 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.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..b593029eb81fb 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, @@ -388,6 +397,7 @@ const bundles = [ bundleTypes: [NODE_DEV, NODE_PROD, UMD_DEV, UMD_PROD], moduleType: RENDERER, entry: 'react-server-dom-webpack/client.browser', + condition: 'react-server', global: 'ReactServerDOMClient', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, @@ -397,6 +407,7 @@ const bundles = [ bundleTypes: [NODE_DEV, NODE_PROD], moduleType: RENDERER, entry: 'react-server-dom-webpack/client.node', + condition: 'react-server', global: 'ReactServerDOMClient', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, @@ -406,6 +417,7 @@ const bundles = [ bundleTypes: [NODE_DEV, NODE_PROD], moduleType: RENDERER, entry: 'react-server-dom-webpack/client.node.unbundled', + condition: 'react-server', global: 'ReactServerDOMClient', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, @@ -415,6 +427,7 @@ const bundles = [ bundleTypes: [NODE_DEV, NODE_PROD], moduleType: RENDERER, entry: 'react-server-dom-webpack/client.edge', + condition: 'react-server', global: 'ReactServerDOMClient', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, @@ -437,6 +450,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 +463,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 +475,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 +485,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 +495,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 +505,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, @@ -498,6 +517,7 @@ const bundles = [ bundleTypes: [NODE_DEV, NODE_PROD, UMD_DEV, UMD_PROD], moduleType: RENDERER, entry: 'react-server-dom-turbopack/client.browser', + condition: 'react-server', global: 'ReactServerDOMClient', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, @@ -507,6 +527,7 @@ const bundles = [ bundleTypes: [NODE_DEV, NODE_PROD], moduleType: RENDERER, entry: 'react-server-dom-turbopack/client.node', + condition: 'react-server', global: 'ReactServerDOMClient', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, @@ -516,6 +537,7 @@ const bundles = [ bundleTypes: [NODE_DEV, NODE_PROD], moduleType: RENDERER, entry: 'react-server-dom-turbopack/client.node.unbundled', + condition: 'react-server', global: 'ReactServerDOMClient', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, @@ -525,6 +547,7 @@ const bundles = [ bundleTypes: [NODE_DEV, NODE_PROD], moduleType: RENDERER, entry: 'react-server-dom-turbopack/client.edge', + condition: 'react-server', global: 'ReactServerDOMClient', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, @@ -540,6 +563,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 +576,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 +588,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'], @@ -573,6 +599,7 @@ const bundles = [ bundleTypes: [NODE_DEV, NODE_PROD, ESM_DEV, ESM_PROD], moduleType: RENDERER, entry: 'react-server-dom-esm/client.browser', + condition: 'react-server', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, externals: ['react', 'react-dom'], @@ -581,6 +608,7 @@ const bundles = [ bundleTypes: [NODE_DEV, NODE_PROD], moduleType: RENDERER, entry: 'react-server-dom-esm/client.node', + condition: 'react-server', minifyWithProdErrorCodes: false, wrapWithModuleBoundaries: false, externals: ['react', 'react-dom', 'util', 'crypto'], @@ -591,6 +619,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 +631,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, @@ -613,6 +643,7 @@ const bundles = [ bundleTypes: __EXPERIMENTAL__ ? [FB_WWW_DEV, FB_WWW_PROD] : [], moduleType: RENDERER, entry: 'react-server-dom-fb/src/ReactFlightDOMClientFB.js', + condition: 'react-server', global: 'ReactFlightDOMClient', 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.