Skip to content

Conversation

@sebmarkbage
Copy link
Contributor

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.

We might need to also specify imports because jsx-runtime imports our own entry point.

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.
@facebook-github-bot facebook-github-bot added CLA Signed React Core Team Opened by a member of the React Core Team labels Nov 20, 2020
@codesandbox-ci
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 5413001:

Sandbox Source
React Configuration

"./build-info.json": "./build-info.json",
"./jsx-runtime": "./jsx-runtime.js",
"./jsx-dev-runtime": "./jsx-dev-runtime.js",
"./": "./"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exports protocol is an allow list so we must list everything we want to expose here. That's actually great because we can explicitly forbid deep linking. Unfortunately our build tooling is using this same file to find things so we need to export everything until we fix that.

@sizebot
Copy link

sizebot commented Nov 20, 2020

Details of bundled changes.

Comparing: 89d4fe1...5413001

react

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-jsx-runtime.profiling.min.js 0.0% +0.2% 970 B 970 B 607 B 608 B NODE_PROFILING
React-profiling.js 0.0% 0.0% 16.37 KB 16.37 KB 4.27 KB 4.27 KB FB_WWW_PROFILING
React-dev.js 0.0% 0.0% 87.45 KB 87.45 KB 20.98 KB 20.98 KB RN_FB_DEV
React-prod.js 0.0% 🔺+0.1% 16.63 KB 16.63 KB 4.43 KB 4.43 KB RN_FB_PROD
React-profiling.js 0.0% +0.1% 16.63 KB 16.63 KB 4.43 KB 4.43 KB RN_FB_PROFILING
react-jsx-dev-runtime.profiling.min.js 0.0% +0.3% 464 B 464 B 321 B 322 B NODE_PROFILING
react-unstable-index.server.development.js n/a n/a 0 B 463 B 0 B 336 B NODE_DEV
react-unstable-index.server.production.min.js n/a n/a 0 B 373 B 0 B 278 B NODE_PROD
React-dev.js 0.0% 0.0% 95.58 KB 95.58 KB 23.3 KB 23.3 KB FB_WWW_DEV
React-prod.js 0.0% 0.0% 16.37 KB 16.37 KB 4.27 KB 4.27 KB FB_WWW_PROD

Size changes (stable)

Generated by 🚫 dangerJS against 5413001

"main": "index.js",
"exports": {
".": {
"react-server": "./unstable-index.server.js",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't know this feature existed, that's cool

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was thinking the same thing. Just finished reading about it myself.

@sebmarkbage
Copy link
Contributor Author

Note that this would have implications if we publish a minor from this branch.

@sizebot
Copy link

sizebot commented Nov 20, 2020

Details of bundled changes.

Comparing: 89d4fe1...5413001

react

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-unstable-cache.development.js 0.0% +0.2% 1.05 KB 1.05 KB 588 B 589 B NODE_DEV
react-unstable-cache.production.min.js 0.0% 🔺+0.2% 867 B 867 B 563 B 564 B NODE_PROD
react-unstable-cache.profiling.min.js 0.0% +0.2% 866 B 866 B 562 B 563 B NODE_PROFILING
react-jsx-dev-runtime.profiling.min.js 0.0% +0.3% 477 B 477 B 330 B 331 B NODE_PROFILING
react.development.js 0.0% 0.0% 108.3 KB 108.3 KB 26.5 KB 26.5 KB UMD_DEV
react-unstable-index.server.development.js n/a n/a 0 B 65.42 KB 0 B 17.81 KB NODE_DEV
react.production.min.js 0.0% -0.0% 11.58 KB 11.58 KB 4.58 KB 4.58 KB UMD_PROD
react-unstable-index.server.production.min.js n/a n/a 0 B 6.17 KB 0 B 2.56 KB NODE_PROD
React-dev.js 0.0% 0.0% 94.59 KB 94.59 KB 23.07 KB 23.07 KB FB_WWW_DEV

React: size: 0.0%, gzip: -0.0%

Size changes (experimental)

Generated by 🚫 dangerJS against 5413001

@acdlite
Copy link
Collaborator

acdlite commented Nov 20, 2020

Oh yeah. Prefix it?

@acdlite
Copy link
Collaborator

acdlite commented Nov 20, 2020

Throwing in the stable channel seems fine, though

@sebmarkbage
Copy link
Contributor Author

sebmarkbage commented Nov 20, 2020

It’s prefixed but the package json changes can’t be prefixed in case the resolution causes breakages.

@sebmarkbage sebmarkbage merged commit d93b58a into facebook:master Nov 20, 2020
@sebmarkbage
Copy link
Contributor Author

The "react-server" name is confusing because it doesn't apply to SSR, but we decided to settle on that. SSR is not "server environment". It's an "emulated client environment".

@sebmarkbage
Copy link
Contributor Author

Unfortunately it doesn't make sense to add a "react-client" resolution. That's just the default. Even if we did in some new tooling, React in general isn't that opinionated about configuring this stuff so not all environments would resolve it.

lazy,
memo,
useCallback,
useContext,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is createContext not available in server components?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. There will likely be a new type of context like a "server context" that's serializable or something like that.

@nstepien
Copy link

Hi,

Could the changes in this PR, specifically adding the exports field in package.json, be backported to React 16? It'd help fix #20235, though a release still needs to be made.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed React Core Team Opened by a member of the React Core Team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants