From 84b0c6341d765230694e443db5c34c41783c4b30 Mon Sep 17 00:00:00 2001 From: Sebastian Sebbie Silbermann Date: Mon, 13 Jan 2025 17:40:03 +0100 Subject: [PATCH 1/3] [rcr, Flight] Add throwing RSC entrypoint for compiler-runtime --- .../packages/react-compiler-runtime/package.json | 7 +++++++ .../react-compiler-runtime/scripts/build.js | 7 +++++-- .../src/index.react-server.ts | 12 ++++++++++++ packages/react/compiler-runtime.react-server.js | 10 ++++++++++ .../react/npm/compiler-runtime.react-server.js | 14 ++++++++++++++ packages/react/package.json | 2 +- packages/react/src/ReactCompilerRuntimeServer.js | 14 ++++++++++++++ scripts/error-codes/codes.json | 3 ++- scripts/rollup/bundles.js | 13 +++++++++++++ 9 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 compiler/packages/react-compiler-runtime/src/index.react-server.ts create mode 100644 packages/react/compiler-runtime.react-server.js create mode 100644 packages/react/npm/compiler-runtime.react-server.js create mode 100644 packages/react/src/ReactCompilerRuntimeServer.js diff --git a/compiler/packages/react-compiler-runtime/package.json b/compiler/packages/react-compiler-runtime/package.json index c8bbc47345870..78f71abd3b90d 100644 --- a/compiler/packages/react-compiler-runtime/package.json +++ b/compiler/packages/react-compiler-runtime/package.json @@ -5,6 +5,13 @@ "license": "MIT", "main": "dist/index.js", "typings": "dist/index.d.ts", + "exports": { + ".": { + "types": "./dist/index.d.ts", + "react-server": "./dist/index.react-server.js", + "default": "./dist/index.js" + } + }, "files": [ "dist", "src" diff --git a/compiler/packages/react-compiler-runtime/scripts/build.js b/compiler/packages/react-compiler-runtime/scripts/build.js index f87650bc8754b..10b11f73193e5 100755 --- a/compiler/packages/react-compiler-runtime/scripts/build.js +++ b/compiler/packages/react-compiler-runtime/scripts/build.js @@ -26,8 +26,11 @@ const argv = yargs(process.argv.slice(2)) .parse(); const config = { - entryPoints: [path.join(__dirname, '../src/index.ts')], - outfile: path.join(__dirname, '../dist/index.js'), + entryPoints: [ + path.join(__dirname, '../src/index.ts'), + path.join(__dirname, '../src/index.react-server.ts'), + ], + outdir: path.join(__dirname, '../dist'), bundle: true, external: ['react'], format: 'cjs', diff --git a/compiler/packages/react-compiler-runtime/src/index.react-server.ts b/compiler/packages/react-compiler-runtime/src/index.react-server.ts new file mode 100644 index 0000000000000..59d82de741613 --- /dev/null +++ b/compiler/packages/react-compiler-runtime/src/index.react-server.ts @@ -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. + */ + +throw new Error( + 'The React Compiler is currently not supported in a React Server environment. ' + + 'Ensure that files importable with a `react-server` condition are not compiled with the React Compiler. ' + + "Libraries should provide a dedicated `react-server` entrypoint that wasn't compiled with the React Compiler.", +); diff --git a/packages/react/compiler-runtime.react-server.js b/packages/react/compiler-runtime.react-server.js new file mode 100644 index 0000000000000..95a0dc33c4452 --- /dev/null +++ b/packages/react/compiler-runtime.react-server.js @@ -0,0 +1,10 @@ +/** + * 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 + */ + +export * from './src/ReactCompilerRuntimeServer'; diff --git a/packages/react/npm/compiler-runtime.react-server.js b/packages/react/npm/compiler-runtime.react-server.js new file mode 100644 index 0000000000000..36583139f6bb5 --- /dev/null +++ b/packages/react/npm/compiler-runtime.react-server.js @@ -0,0 +1,14 @@ +/** + * 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. + */ + +'use strict'; + +if (process.env.NODE_ENV === 'production') { + module.exports = require('./cjs/react-compiler-runtime.react-server.production.js'); +} else { + module.exports = require('./cjs/react-compiler-runtime.react-server.development.js'); +} diff --git a/packages/react/package.json b/packages/react/package.json index 84f4c0d41beeb..690854c0d6899 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -36,7 +36,7 @@ "default": "./jsx-dev-runtime.js" }, "./compiler-runtime": { - "react-server": "./compiler-runtime.js", + "react-server": "./compiler-runtime.react-server.js", "default": "./compiler-runtime.js" }, "./src/*": "./src/*" diff --git a/packages/react/src/ReactCompilerRuntimeServer.js b/packages/react/src/ReactCompilerRuntimeServer.js new file mode 100644 index 0000000000000..68fc7934f3d87 --- /dev/null +++ b/packages/react/src/ReactCompilerRuntimeServer.js @@ -0,0 +1,14 @@ +/** + * 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 + */ + +throw new Error( + 'The React Compiler is currently not supported in a React Server environment. ' + + 'Ensure that files importable with a `react-server` condition are not compiled with the React Compiler. ' + + "Libraries should provide a dedicated `react-server` entrypoint that wasn't compiled with the React Compiler.", +); diff --git a/scripts/error-codes/codes.json b/scripts/error-codes/codes.json index 8109e2431fd31..b1143569a4fab 100644 --- a/scripts/error-codes/codes.json +++ b/scripts/error-codes/codes.json @@ -529,5 +529,6 @@ "541": "Compared context values must be arrays", "542": "Suspense Exception: This is not a real error! It's an implementation detail of `useActionState` to interrupt the current render. You must either rethrow it immediately, or move the `useActionState` call outside of the `try/catch` block. Capturing without rethrowing will lead to unexpected behavior.\n\nTo handle async errors, wrap your component in an error boundary.", "543": "Expected a ResourceEffectUpdate to be pushed together with ResourceEffectIdentity. This is a bug in React.", - "544": "Found a pair with an auto name. This is a bug in React." + "544": "Found a pair with an auto name. This is a bug in React.", + "545": "The React Compiler is currently not supported in a React Server environment. Ensure that files importable with a `react-server` condition are not compiled with the React Compiler. Libraries should provide a dedicated `react-server` entrypoint that wasn't compiled with the React Compiler." } diff --git a/scripts/rollup/bundles.js b/scripts/rollup/bundles.js index 3d90283c5e6e0..f1b68067126af 100644 --- a/scripts/rollup/bundles.js +++ b/scripts/rollup/bundles.js @@ -126,6 +126,19 @@ const bundles = [ externals: ['react'], }, + /******* Compiler Runtime React Server *******/ + { + bundleTypes: [NODE_DEV, NODE_PROD], + moduleType: ISOMORPHIC, + entry: 'react/src/ReactCompilerRuntimeServer.js', + name: 'react-compiler-runtime.react-server', + condition: 'react-server', + global: 'CompilerRuntime', + minifyWithProdErrorCodes: true, + wrapWithModuleBoundaries: false, + externals: ['react'], + }, + /******* React JSX Runtime React Server *******/ { bundleTypes: [NODE_DEV, NODE_PROD], From 745c7f04fb6780e8008b962f86b0c2f3e2e5998c Mon Sep 17 00:00:00 2001 From: Sebastian Sebbie Silbermann Date: Mon, 13 Jan 2025 17:59:58 +0100 Subject: [PATCH 2/3] "files importable" > "modules imported" --- .../packages/react-compiler-runtime/src/index.react-server.ts | 2 +- packages/react/src/ReactCompilerRuntimeServer.js | 2 +- scripts/error-codes/codes.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/packages/react-compiler-runtime/src/index.react-server.ts b/compiler/packages/react-compiler-runtime/src/index.react-server.ts index 59d82de741613..23c10153b375f 100644 --- a/compiler/packages/react-compiler-runtime/src/index.react-server.ts +++ b/compiler/packages/react-compiler-runtime/src/index.react-server.ts @@ -7,6 +7,6 @@ throw new Error( 'The React Compiler is currently not supported in a React Server environment. ' + - 'Ensure that files importable with a `react-server` condition are not compiled with the React Compiler. ' + + 'Ensure that modules imported with a `react-server` condition are not compiled with the React Compiler. ' + "Libraries should provide a dedicated `react-server` entrypoint that wasn't compiled with the React Compiler.", ); diff --git a/packages/react/src/ReactCompilerRuntimeServer.js b/packages/react/src/ReactCompilerRuntimeServer.js index 68fc7934f3d87..bc8f761498fa4 100644 --- a/packages/react/src/ReactCompilerRuntimeServer.js +++ b/packages/react/src/ReactCompilerRuntimeServer.js @@ -9,6 +9,6 @@ throw new Error( 'The React Compiler is currently not supported in a React Server environment. ' + - 'Ensure that files importable with a `react-server` condition are not compiled with the React Compiler. ' + + 'Ensure that modules imported with a `react-server` condition are not compiled with the React Compiler. ' + "Libraries should provide a dedicated `react-server` entrypoint that wasn't compiled with the React Compiler.", ); diff --git a/scripts/error-codes/codes.json b/scripts/error-codes/codes.json index b1143569a4fab..ef12d83b032d6 100644 --- a/scripts/error-codes/codes.json +++ b/scripts/error-codes/codes.json @@ -530,5 +530,5 @@ "542": "Suspense Exception: This is not a real error! It's an implementation detail of `useActionState` to interrupt the current render. You must either rethrow it immediately, or move the `useActionState` call outside of the `try/catch` block. Capturing without rethrowing will lead to unexpected behavior.\n\nTo handle async errors, wrap your component in an error boundary.", "543": "Expected a ResourceEffectUpdate to be pushed together with ResourceEffectIdentity. This is a bug in React.", "544": "Found a pair with an auto name. This is a bug in React.", - "545": "The React Compiler is currently not supported in a React Server environment. Ensure that files importable with a `react-server` condition are not compiled with the React Compiler. Libraries should provide a dedicated `react-server` entrypoint that wasn't compiled with the React Compiler." + "545": "The React Compiler is currently not supported in a React Server environment. Ensure that modules imported with a `react-server` condition are not compiled with the React Compiler. Libraries should provide a dedicated `react-server` entrypoint that wasn't compiled with the React Compiler." } From 33c78b394ac1df22bfeb15c21561bd2efb8302f8 Mon Sep 17 00:00:00 2001 From: Sebastian Sebbie Silbermann Date: Mon, 13 Jan 2025 19:49:56 +0100 Subject: [PATCH 3/3] Ensure all relevant files are published --- packages/react/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/react/package.json b/packages/react/package.json index 690854c0d6899..f9602c2727e67 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -14,6 +14,7 @@ "index.js", "cjs/", "compiler-runtime.js", + "compiler-runtime.react-server.js", "jsx-runtime.js", "jsx-runtime.react-server.js", "jsx-dev-runtime.js",