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..23c10153b375f --- /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 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/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..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", @@ -36,7 +37,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..bc8f761498fa4 --- /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 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 8109e2431fd31..ef12d83b032d6 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 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/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],