Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export React as Named Exports instead of CommonJS #18106

Merged
merged 12 commits into from Feb 25, 2020

Conversation

sebmarkbage
Copy link
Collaborator

@sebmarkbage sebmarkbage commented Feb 22, 2020

The goal is to ensure more consistency in our tooling (e.g. we didn't even Flow check any imports of React in our code), better compiler output and unblock the ability to publish ES modules.

We can't conditionally export names, so we can't use feature flags to determine the exports anymore.

Instead, in my approach the default index.js exports all exports. This allows Flow to check and tests based on flags to get all features. Then I use a file extension scheme to fork the exports based on the four build types we need.

.modern.fb.js - experimental FB builds
.classic.fb.js - stable FB builds
.experimental.js - experimental npm builds
.stable.js - stable npm builds

I'd review the individual commit messages.

The output builds:

Stable - sebmarkbage/react-builds@ed86c04
Experimental - sebmarkbage/react-builds@5c0f29c

@facebook-github-bot facebook-github-bot added CLA Signed React Core Team Opened by a member of the React Core Team labels Feb 22, 2020
@codesandbox-ci
Copy link

codesandbox-ci bot commented Feb 22, 2020

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 cb4ff7b:

Sandbox Source
amazing-browser-5eq6e Configuration

@sizebot
Copy link

sizebot commented Feb 22, 2020

Details of bundled changes.

Comparing: 8d7535e...cb4ff7b

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom.profiling.min.js +0.1% -0.1% 123.61 KB 123.7 KB 38.61 KB 38.58 KB NODE_PROFILING
react-dom-unstable-fizz.browser.development.js 0.0% -0.2% 3.61 KB 3.61 KB 1.37 KB 1.36 KB UMD_DEV
ReactDOM-dev.js -0.0% -0.0% 894.46 KB 894.37 KB 199.66 KB 199.61 KB FB_WWW_DEV
react-dom-unstable-fizz.browser.production.min.js 0.0% -0.3% 1.2 KB 1.2 KB 708 B 706 B UMD_PROD
react-dom-test-utils.development.js 0.0% -0.0% 53.69 KB 53.69 KB 14.01 KB 14.01 KB UMD_DEV
react-dom-testing.profiling.min.js +0.1% -0.1% 121.17 KB 121.25 KB 38.14 KB 38.11 KB NODE_PROFILING
react-dom-server.browser.development.js 0.0% -0.0% 138.16 KB 138.16 KB 35.19 KB 35.19 KB UMD_DEV
react-dom-test-utils.production.min.js 0.0% -0.1% 11.22 KB 11.22 KB 4.16 KB 4.16 KB UMD_PROD
react-dom-server.browser.production.min.js 0.0% -0.0% 20.45 KB 20.45 KB 7.49 KB 7.49 KB UMD_PROD
react-dom-test-utils.development.js 0.0% -0.0% 49.52 KB 49.52 KB 13.55 KB 13.55 KB NODE_DEV
react-dom-server.browser.development.js 0.0% -0.0% 127.45 KB 127.45 KB 33.83 KB 33.83 KB NODE_DEV
react-dom-test-utils.production.min.js 0.0% -0.0% 10.97 KB 10.97 KB 4.09 KB 4.09 KB NODE_PROD
react-dom-server.browser.production.min.js 0.0% -0.0% 20.36 KB 20.36 KB 7.45 KB 7.45 KB NODE_PROD
react-dom.development.js 0.0% -0.0% 914.14 KB 914.29 KB 200.26 KB 200.19 KB UMD_DEV
react-dom-unstable-native-dependencies.development.js 0.0% -0.0% 59.08 KB 59.08 KB 14.75 KB 14.75 KB UMD_DEV
react-dom.production.min.js 0.0% -0.0% 119.74 KB 119.75 KB 38.22 KB 38.22 KB UMD_PROD
react-dom-unstable-native-dependencies.production.min.js 0.0% -0.1% 10.28 KB 10.28 KB 3.49 KB 3.49 KB UMD_PROD
ReactDOMServer-dev.js 0.0% -0.0% 134.46 KB 134.46 KB 34.28 KB 34.27 KB FB_WWW_DEV
react-dom.profiling.min.js 0.0% -0.0% 123.39 KB 123.4 KB 39.39 KB 39.38 KB UMD_PROFILING
ReactDOMServer-prod.js 0.0% -0.0% 48.1 KB 48.1 KB 10.99 KB 10.99 KB FB_WWW_PROD
react-dom.development.js 0.0% -0.0% 866.54 KB 866.69 KB 196.59 KB 196.52 KB NODE_DEV
react-dom-unstable-native-dependencies.development.js 0.0% -0.0% 56.05 KB 56.05 KB 14.54 KB 14.53 KB NODE_DEV
react-dom-unstable-fizz.node.development.js 0.0% -0.1% 3.94 KB 3.94 KB 1.46 KB 1.45 KB NODE_DEV
react-dom.production.min.js 🔺+0.1% -0.1% 119.82 KB 119.9 KB 37.48 KB 37.45 KB NODE_PROD
react-dom-unstable-native-dependencies.production.min.js 0.0% -0.1% 10 KB 10 KB 3.39 KB 3.38 KB NODE_PROD
react-dom-unstable-fizz.node.production.min.js 0.0% -0.4% 1.19 KB 1.19 KB 685 B 682 B NODE_PROD
ReactDOM-prod.js -0.0% -0.1% 370.79 KB 370.74 KB 67.49 KB 67.45 KB FB_WWW_PROD
ReactDOM-profiling.js -0.0% -0.1% 382 KB 381.95 KB 69.61 KB 69.57 KB FB_WWW_PROFILING
react-dom-unstable-fizz.browser.development.js 0.0% -0.2% 3.21 KB 3.21 KB 1.3 KB 1.3 KB NODE_DEV
react-dom-unstable-fizz.browser.production.min.js 0.0% -0.5% 1.03 KB 1.03 KB 630 B 627 B NODE_PROD
react-dom-testing.development.js 0.0% -0.0% 887.75 KB 887.91 KB 195.8 KB 195.74 KB UMD_DEV
react-dom-testing.production.min.js 0.0% 0.0% 117.4 KB 117.41 KB 37.7 KB 37.72 KB UMD_PROD
ReactDOMTesting-dev.js -0.0% -0.1% 869.89 KB 869.57 KB 194.64 KB 194.5 KB FB_WWW_DEV
react-dom-testing.profiling.min.js 0.0% -0.0% 120.93 KB 120.94 KB 38.89 KB 38.89 KB UMD_PROFILING
ReactDOMTesting-prod.js -0.0% -0.1% 368.94 KB 368.76 KB 67.41 KB 67.34 KB FB_WWW_PROD
react-dom-testing.development.js 0.0% -0.0% 841.22 KB 841.37 KB 192.26 KB 192.18 KB NODE_DEV
ReactDOMTesting-profiling.js -0.0% -0.1% 368.94 KB 368.76 KB 67.41 KB 67.34 KB FB_WWW_PROFILING
react-dom-server.node.development.js 0.0% -0.0% 128.57 KB 128.57 KB 34.05 KB 34.05 KB NODE_DEV
react-dom-testing.production.min.js 🔺+0.1% -0.0% 117.48 KB 117.57 KB 37.07 KB 37.05 KB NODE_PROD
react-dom-server.node.production.min.js 0.0% -0.0% 20.77 KB 20.77 KB 7.6 KB 7.6 KB NODE_PROD

react

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react.development.js +0.1% -0.2% 105 KB 105.12 KB 25.93 KB 25.88 KB UMD_DEV
react.production.min.js 🔺+0.3% -0.1% 12.57 KB 12.6 KB 4.91 KB 4.91 KB UMD_PROD
react.profiling.min.js +0.2% -0.2% 16.1 KB 16.14 KB 6.02 KB 6.01 KB UMD_PROFILING
react.development.js +0.2% -0.3% 61.46 KB 61.57 KB 16.51 KB 16.46 KB NODE_DEV
react.production.min.js 🔺+3.3% -0.2% 6.83 KB 7.06 KB 2.81 KB 2.81 KB NODE_PROD
React-dev.js +0.1% -0.4% 71.21 KB 71.25 KB 18.03 KB 17.96 KB FB_WWW_DEV
React-prod.js -2.9% -1.6% 18.17 KB 17.65 KB 4.63 KB 4.56 KB FB_WWW_PROD
React-profiling.js -2.9% -1.6% 18.17 KB 17.65 KB 4.63 KB 4.56 KB FB_WWW_PROFILING

ReactDOM: size: 0.0%, gzip: -0.3%

React: size: 🔺+0.3%, gzip: -0.1%

Size changes (experimental)

Generated by 🚫 dangerJS against cb4ff7b

@sizebot
Copy link

sizebot commented Feb 22, 2020

Details of bundled changes.

Comparing: 8d7535e...cb4ff7b

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
ReactDOM-profiling.js +0.1% -0.2% 405.37 KB 405.88 KB 73.95 KB 73.83 KB FB_WWW_PROFILING
react-dom-testing.production.min.js -0.0% -0.1% 117.02 KB 117.01 KB 36.97 KB 36.92 KB NODE_PROD
react-dom-unstable-native-dependencies.development.js 0.0% -0.0% 59.07 KB 59.07 KB 14.75 KB 14.75 KB UMD_DEV
react-dom-unstable-fizz.browser.production.min.js 0.0% -0.5% 1.02 KB 1.02 KB 622 B 619 B NODE_PROD
react-dom-testing.profiling.min.js 0.0% -0.1% 120.64 KB 120.7 KB 38 KB 37.97 KB NODE_PROFILING
react-dom-unstable-native-dependencies.production.min.js 0.0% -0.1% 10.26 KB 10.26 KB 3.49 KB 3.48 KB UMD_PROD
react-dom-unstable-fizz.node.development.js 0.0% -0.1% 3.93 KB 3.93 KB 1.45 KB 1.45 KB NODE_DEV
react-dom.development.js 0.0% -0.0% 885.45 KB 885.6 KB 195.27 KB 195.2 KB UMD_DEV
react-dom-server.browser.development.js 0.0% -0.0% 136.57 KB 136.57 KB 34.98 KB 34.98 KB UMD_DEV
react-dom-unstable-fizz.node.production.min.js 0.0% -0.4% 1.17 KB 1.17 KB 677 B 674 B NODE_PROD
react-dom.production.min.js 0.0% 0.0% 115.84 KB 115.85 KB 37.08 KB 37.08 KB UMD_PROD
react-dom-server.browser.production.min.js 0.0% -0.0% 19.99 KB 19.99 KB 7.4 KB 7.4 KB UMD_PROD
react-dom.profiling.min.js 0.0% 0.0% 119.37 KB 119.38 KB 38.28 KB 38.28 KB UMD_PROFILING
ReactDOMTesting-dev.js 0.0% +0.1% 895.46 KB 895.79 KB 199.65 KB 199.81 KB FB_WWW_DEV
react-dom.development.js 0.0% -0.0% 839.05 KB 839.19 KB 191.69 KB 191.6 KB NODE_DEV
ReactDOMTesting-prod.js 🔺+0.2% -0.1% 380.96 KB 381.65 KB 69.43 KB 69.4 KB FB_WWW_PROD
react-dom-server.browser.development.js 0.0% -0.0% 125.94 KB 125.94 KB 33.61 KB 33.61 KB NODE_DEV
react-dom.production.min.js 0.0% -0.0% 115.88 KB 115.93 KB 36.46 KB 36.45 KB NODE_PROD
react-dom-testing.development.js -0.0% -0.1% 885.65 KB 885.56 KB 195.62 KB 195.47 KB UMD_DEV
ReactDOMTesting-profiling.js +0.2% -0.1% 380.96 KB 381.65 KB 69.43 KB 69.4 KB FB_WWW_PROFILING
react-dom-server.browser.production.min.js 0.0% -0.0% 19.9 KB 19.9 KB 7.37 KB 7.37 KB NODE_PROD
react-dom-unstable-fizz.browser.development.js 0.0% -0.2% 3.59 KB 3.59 KB 1.36 KB 1.36 KB UMD_DEV
react-dom.profiling.min.js 0.0% -0.0% 119.56 KB 119.61 KB 37.53 KB 37.51 KB NODE_PROFILING
react-dom-testing.production.min.js -0.0% -0.1% 116.93 KB 116.89 KB 37.61 KB 37.58 KB UMD_PROD
react-dom-unstable-fizz.browser.production.min.js 0.0% -0.4% 1.19 KB 1.19 KB 700 B 697 B UMD_PROD
ReactDOM-dev.js 0.0% 0.0% 936.45 KB 936.45 KB 208.19 KB 208.25 KB FB_WWW_DEV
react-dom-testing.profiling.min.js -0.0% -0.1% 120.46 KB 120.42 KB 38.79 KB 38.75 KB UMD_PROFILING
ReactDOMServer-dev.js 0.0% -0.0% 135.38 KB 135.38 KB 34.44 KB 34.44 KB FB_WWW_DEV
ReactDOM-prod.js 🔺+0.1% -0.2% 394.12 KB 394.59 KB 71.81 KB 71.68 KB FB_WWW_PROD
react-dom-testing.development.js -0.0% -0.1% 839.21 KB 839.13 KB 192.08 KB 191.91 KB NODE_DEV
ReactDOMServer-prod.js 0.0% 0.0% 48.8 KB 48.8 KB 11.14 KB 11.14 KB FB_WWW_PROD
react-dom-unstable-fizz.browser.development.js 0.0% -0.2% 3.2 KB 3.2 KB 1.29 KB 1.29 KB NODE_DEV
react-dom-unstable-native-dependencies.development.js 0.0% -0.0% 56.04 KB 56.04 KB 14.53 KB 14.53 KB NODE_DEV
react-dom-test-utils.development.js 0.0% -0.0% 53.68 KB 53.68 KB 14 KB 14 KB UMD_DEV
react-dom-unstable-native-dependencies.production.min.js 0.0% -0.1% 9.99 KB 9.99 KB 3.38 KB 3.38 KB NODE_PROD
react-dom-test-utils.production.min.js 0.0% -0.1% 11.2 KB 11.2 KB 4.15 KB 4.15 KB UMD_PROD
react-dom-test-utils.development.js 0.0% -0.0% 49.51 KB 49.51 KB 13.54 KB 13.54 KB NODE_DEV
react-dom-server.node.development.js 0.0% -0.0% 127.06 KB 127.06 KB 33.83 KB 33.83 KB NODE_DEV
react-dom-test-utils.production.min.js 0.0% -0.0% 10.96 KB 10.96 KB 4.08 KB 4.08 KB NODE_PROD
react-dom-server.node.production.min.js 0.0% -0.0% 20.31 KB 20.31 KB 7.53 KB 7.52 KB NODE_PROD

react

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react.development.js +0.1% -0.2% 102.56 KB 102.69 KB 25.5 KB 25.46 KB UMD_DEV
react.production.min.js 🔺+0.3% 0.0% 12.15 KB 12.19 KB 4.79 KB 4.79 KB UMD_PROD
react.profiling.min.js +0.2% +0.1% 15.69 KB 15.72 KB 5.9 KB 5.91 KB UMD_PROFILING
react.development.js +0.2% -0.3% 59.12 KB 59.24 KB 16.12 KB 16.08 KB NODE_DEV
react.production.min.js 🔺+3.0% -0.1% 6.34 KB 6.54 KB 2.66 KB 2.66 KB NODE_PROD
React-dev.js +0.1% -0.3% 71.85 KB 71.92 KB 18.21 KB 18.15 KB FB_WWW_DEV
React-prod.js -2.0% -1.6% 18.06 KB 17.71 KB 4.64 KB 4.57 KB FB_WWW_PROD
React-profiling.js -2.0% -1.6% 18.06 KB 17.71 KB 4.64 KB 4.57 KB FB_WWW_PROFILING

ReactDOM: size: 0.0%, gzip: -0.1%

React: size: 🔺+0.3%, gzip: 0.0%

Size changes (stable)

Generated by 🚫 dangerJS against cb4ff7b

@sebmarkbage
Copy link
Collaborator Author

The increase in pre-gzip size for the react package is because this causes Rollup to encode this as:

exports.a = ...;
exports.b = ...;
exports.c = ...;

Instead of:

module.exports = {
  a: ...,
  b: ...,
  c: ...,
};

Which I guess is more correct in a sense but unclear what is actually better for perf.

This probably doesn't matter much since gzip takes care of it and the other packages won't matter as much since they have much fewer exports.

@joeldenning
Copy link
Contributor

Apologies for jumping in here without full context, but I'm a bit worried about switching to only named exports for react and react-dom, which appears to be the direction the ESM bundling is going. Isn't that a hugely breaking change? The following code is so very common:

import ReactDOM from 'react-dom';
import React from 'react';

React.createElement('div')
ReactDOM.render(...)

For those wishing to use the ESM bundle once it's finished, will they have to migrate all code to use named exports? Is a codemod planned to help with migration? Or am I misunderstanding this entirely?

We currently fork .fb.js entry points. This adds a few more options.

.modern.fb.js - experimental FB builds
.classic.fb.js - stable FB builds
.fb.js - if no other FB build, use this for FB builds
.experimental.js - experimental builds
.stable.js - stable builds
.js - used if no other override exists

This will be used to have different ES exports for different builds.
We need to re-export the Flow exported types so we can use them in our code.

We don't want to use the Flow types from upstream since it doesn't have the non-public APIs that we have.

This should be able to use export * but I don't know why it doesn't work.

This actually enables Flow typing of React which was just "any" before.
This exposed some Flow errors that needs fixing.
None of our builds expose all exports and they all differ in at least one
way, so we need four forks.
We don't want to emit the esModule compatibility flag on our CommonJS
output. For now we treat our named exports as if they're CommonJS.

This is a potentially breaking change for scheduler (but all those apis
are unstable), react-is and use-subscription. However, it seems unlikely
that anyone would rely on this since these only have named exports.
This lets it do the negative test by ensuring that the right tests fail.

However, this in turn will make other tests that are not behind
__EXPERIMENTAL__ fail. So I need to do that next.
Since there's no way to override the exports using feature flags
in .intern.js anymore we can't use these APIs in stable.

The tradeoff here is that we can either enable the negative tests on
"stable" that means experimental are expected to fail, or we can disable
tests on stable. This is unfortunate since some of these APIs now run on
a "stable" config at FB instead of the experimental.
Same strategy as React.

I moved the ReactDOMFB runtime injection to classic.fb.js

Since we only fork the entrypoint, the `/testing` entrypoint needs to
be forked too to re-export the same things plus `act`. This is a bit
unfortunate. If it becomes a pattern we can consider forking in the
module resolution deeply.

fix flow
Now that ReactDOM is Flow type checked we need to fix up its types.
These are only flagging the exports and no implementation details so we
can control them fully through the export overrides.
@TrySound
Copy link
Contributor

@joeldenning Don't worry. This is commonjs only change. ESM will have separate entry points with with default exports.

@ericmorand
Copy link

@TrySound, do you know when the change is planned for? Specifically, when will react comes with an ESM variant alongside CJS and UMD ones?

@TrySound
Copy link
Contributor

TrySound commented Dec 1, 2020

No idea. Probably when conditional exports will be widely supported. Either in react 18 or 19.

@ericmorand
Copy link

Thanks for your answer. I'm wondering if it would be possible to just provide the sources as-is in the published packages, alongside the "compiled" CJS and UMD variants. This way we could simple import from the sources and create our own application using ES syntax.

@TrySound
Copy link
Contributor

TrySound commented Dec 1, 2020

Sources will not help. There are a lot of configurations on top of it like flow syntax, aliases to shared package which is not published and many more.

@ericmorand
Copy link

It's sad. Sounds like we are years away to get rid of bundling to build a react app. I am perfectly aware that bundling is here to stay because we have a long way to go before ES modules are widely adopted by all browsers, but I'm talking here about not bundling when developing - it's not needed and bundling impairs the performance a lot for developers.

Is it planned that React migrate to TypeScript at one point?

@TrySound
Copy link
Contributor

TrySound commented Dec 1, 2020

Snowpack allows to not use bundler for development. And their service has ready to use esm version of react bundle.
https://www.snowpack.dev/

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.

None yet

7 participants