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.
Sebastian Markbåge committed
Nov 20, 2020 at 11:47 UTC
d93b58a5e38cf06998da628932029291cc605c2d
7 files changed
+84
-4
fixtures/flight/package.json
+1
-1
@@ -67,7 +67,7 @@
67
"prebuild": "cp -r ../../build/node_modules/* ./node_modules/",
68
"start": "concurrently \"npm run start:server\" \"npm run start:client\"",
69
"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",
71
"start:prod": "node scripts/build.js && NODE_ENV=production node server",
72
"build": "node scripts/build.js",
73
"test": "node scripts/test.js --env=jsdom"
packages/react/npm/unstable-index.server.js
new
+7
@@ -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
@@ -17,9 +17,24 @@
17
"umd/",
18
"jsx-runtime.js",
19
"jsx-dev-runtime.js",
20
+ "unstable-index.server.js",
21
"unstable-cache.js"
22
],
23
"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
+ },
38
"repository": {
39
"type": "git",
40
"url": "https://github.com/facebook/react.git",
packages/react/unstable-index.server.experimental.js
new
+37
@@ -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';
packages/react/unstable-index.server.js
new
+12
@@ -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
@@ -90,6 +90,15 @@ const bundles = [
90
externals: [],
91
},
92
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
+
102
/******* React JSX Runtime *******/
103
{
104
bundleTypes: [
scripts/rollup/forks.js
+3
-3
@@ -43,7 +43,7 @@ const forks = Object.freeze({
43
// happens. Other bundles just require('object-assign') anyway.
44
return null;
45
}
46
- if (entry === 'react') {
46
+ if (entry === 'react' || entry === 'react/unstable-index.server') {
47
// Use the forked version that uses ES modules instead of CommonJS.
48
return 'shared/forks/object-assign.inline-umd.js';
49
}
@@ -64,8 +64,8 @@ const forks = Object.freeze({
64
// Without this fork, importing `shared/ReactSharedInternals` inside
65
// the `react` package itself would not work due to a cyclical dependency.
66
'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';
69
}
70
if (!entry.startsWith('react/') && dependencies.indexOf('react') === -1) {
71
// React internals are unavailable if we can't reference the package.