Skip to content

Commit d93b58a

Browse files
authored
Add flight specific entry point for react package (#20304)
This configures the Flight fixture to use the "react-server" environment. This allows the package.json exports field to specify a different resolution in this environment. I use this in the "react" package to resolve to a new bundle that excludes the Hooks that aren't relevant in this environment like useState and useEffect. This allows us to error early if these names are imported. If we actually published ESM, it would we a static error. Now it's a runtime error. You can test this by importing useState in Container.js which is used by the client and server.
1 parent 89d4fe1 commit d93b58a

File tree

7 files changed

+84
-4
lines changed

7 files changed

+84
-4
lines changed

fixtures/flight/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"prebuild": "cp -r ../../build/node_modules/* ./node_modules/",
6868
"start": "concurrently \"npm run start:server\" \"npm run start:client\"",
6969
"start:client": "node scripts/start.js",
70-
"start:server": "NODE_ENV=development node --experimental-loader ./loader/index.js server",
70+
"start:server": "NODE_ENV=development node --experimental-loader ./loader/index.js --conditions=react-server server",
7171
"start:prod": "node scripts/build.js && NODE_ENV=production node server",
7272
"build": "node scripts/build.js",
7373
"test": "node scripts/test.js --env=jsdom"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
if (process.env.NODE_ENV === 'production') {
4+
module.exports = require('./cjs/react-unstable-index.server.production.min.js');
5+
} else {
6+
module.exports = require('./cjs/react-unstable-index.server.development.js');
7+
}

packages/react/package.json

+15
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,24 @@
1717
"umd/",
1818
"jsx-runtime.js",
1919
"jsx-dev-runtime.js",
20+
"unstable-index.server.js",
2021
"unstable-cache.js"
2122
],
2223
"main": "index.js",
24+
"exports": {
25+
".": {
26+
"react-server": "./unstable-index.server.js",
27+
"default": "./index.js"
28+
},
29+
"./index": {
30+
"react-server": "./unstable-index.server.js",
31+
"default": "./index.js"
32+
},
33+
"./build-info.json": "./build-info.json",
34+
"./jsx-runtime": "./jsx-runtime.js",
35+
"./jsx-dev-runtime": "./jsx-dev-runtime.js",
36+
"./": "./"
37+
},
2338
"repository": {
2439
"type": "git",
2540
"url": "https://github.com/facebook/react.git",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its 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+
export {
11+
Children,
12+
createRef,
13+
forwardRef,
14+
lazy,
15+
memo,
16+
useCallback,
17+
useContext,
18+
useDebugValue,
19+
useMemo,
20+
useMutableSource as unstable_useMutableSource,
21+
createMutableSource as unstable_createMutableSource,
22+
Fragment,
23+
Profiler,
24+
StrictMode,
25+
Suspense,
26+
createElement,
27+
cloneElement,
28+
isValidElement,
29+
version,
30+
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
31+
// exposeConcurrentModeAPIs
32+
useDeferredValue as unstable_useDeferredValue,
33+
SuspenseList as unstable_SuspenseList,
34+
unstable_useOpaqueIdentifier,
35+
// enableDebugTracing
36+
unstable_DebugTracingMode,
37+
} from './src/React';
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its 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+
throw new Error(
11+
'This entry point is not yet supported outside of experimental channels',
12+
);

scripts/rollup/bundles.js

+9
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ const bundles = [
9090
externals: [],
9191
},
9292

93+
/******* Isomorphic Server Only *******/
94+
{
95+
bundleTypes: [NODE_DEV, NODE_PROD],
96+
moduleType: ISOMORPHIC,
97+
entry: 'react/unstable-index.server',
98+
global: 'React',
99+
externals: [],
100+
},
101+
93102
/******* React JSX Runtime *******/
94103
{
95104
bundleTypes: [

scripts/rollup/forks.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const forks = Object.freeze({
4343
// happens. Other bundles just require('object-assign') anyway.
4444
return null;
4545
}
46-
if (entry === 'react') {
46+
if (entry === 'react' || entry === 'react/unstable-index.server') {
4747
// Use the forked version that uses ES modules instead of CommonJS.
4848
return 'shared/forks/object-assign.inline-umd.js';
4949
}
@@ -64,8 +64,8 @@ const forks = Object.freeze({
6464
// Without this fork, importing `shared/ReactSharedInternals` inside
6565
// the `react` package itself would not work due to a cyclical dependency.
6666
'shared/ReactSharedInternals': (bundleType, entry, dependencies) => {
67-
if (entry === 'react') {
68-
return 'react/src/ReactSharedInternals';
67+
if (entry === 'react' || entry === 'react/unstable-index.server') {
68+
return 'react/src/ReactSharedInternals.js';
6969
}
7070
if (!entry.startsWith('react/') && dependencies.indexOf('react') === -1) {
7171
// React internals are unavailable if we can't reference the package.

0 commit comments

Comments
 (0)