-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
Convert the Source to ES Modules #11389
Conversation
d68f7c8
to
ca686fe
Compare
38147b9
to
43b5a04
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work Dan! This is a big diff! Hope I didn't overlook something important. 😁
Tests, lint, and Flow checks all pass locally. I also did a local sync to fbsource to quickly test the React Native renderer and didn't notice any obvious problems so... 👍
I wondered about the impact on bundle size of converting conditional/DEV-only requires to imports, but it looks like this PR has a mostly positive impact on bundle size. There are a couple of small size increases but they seem to be primarily in test renderer and experimental packages.
I noticed lots of new warnings during yarn build
:
'default' is imported from external module '...' but never used
Looks like these are all coming from prod builds, for code that used to only be imported within a DEV conditional. It's unfortunate that this clutters up the output of build.
import * as ReactFiberErrorLogger | ||
from 'react-reconciler/src/ReactFiberErrorLogger'; | ||
import ReactErrorUtils from 'shared/ReactErrorUtils'; | ||
// TODO: direct imports like some-package/src/* are bad. Fix me. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment should be moved above line 14?
@@ -7,4 +7,5 @@ | |||
|
|||
'use strict'; | |||
|
|||
// TODO: this is special because it gets imported during build. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The release script uses this, but it would be easy to change it. Looks like the only other reference is our version-check
task which would also be easy to update.
I think those are the only 2 things blocking this TODO? If so, I'd be happy to help with a follow-up PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe the cleanest way would be to shim this file with a fake module that is generated by reading package.json
.
I also noticed in some cases the size before gzip decreased, but slightly increased after. Since gzip number isn’t that solid anyway (it depends on surrounding context) I wouldn’t worry too much about that.
Yeah. Basically CommonJS builds now include calls like |
Is this PR ready for merge ? It is really a greate work! |
Not yet. I'm still fixing up some things. |
This is clearer because we can use ES imports in it.
faeb440
to
9551d73
Compare
Great job! |
Nice job! |
Nice! |
this didn't make it into 16.1.0-beta, did it @gaearon ? been itching to import react in browsers that support modules |
That's not what it is. It's not for loading React from browsers, it's changing React's own internal module system. We can do the ES build too though—just needs somebody who wants to contribute it :-) |
ahh i see, thanks for the clarification! |
* Update transforms to handle ES modules * Update Jest to handle ES modules * Convert react package to ES modules * Convert react-art package to ES Modules * Convert react-call-return package to ES Modules * Convert react-test-renderer package to ES Modules * Convert react-cs-renderer package to ES Modules * Convert react-rt-renderer package to ES Modules * Convert react-noop-renderer package to ES Modules * Convert react-dom/server to ES modules * Convert react-dom/{client,events,test-utils} to ES modules * Convert react-dom/shared to ES modules * Convert react-native-renderer to ES modules * Convert react-reconciler to ES modules * Convert events to ES modules * Convert shared to ES modules * Remove CommonJS support from transforms * Move ReactDOMFB entry point code into react-dom/src This is clearer because we can use ES imports in it. * Fix Rollup shim configuration to work with ESM * Fix incorrect comment * Exclude external imports without side effects * Fix ReactDOM FB build * Remove TODOs I don’t intend to fix yet
* Update transforms to handle ES modules * Update Jest to handle ES modules * Convert react package to ES modules * Convert react-art package to ES Modules * Convert react-call-return package to ES Modules * Convert react-test-renderer package to ES Modules * Convert react-cs-renderer package to ES Modules * Convert react-rt-renderer package to ES Modules * Convert react-noop-renderer package to ES Modules * Convert react-dom/server to ES modules * Convert react-dom/{client,events,test-utils} to ES modules * Convert react-dom/shared to ES modules * Convert react-native-renderer to ES modules * Convert react-reconciler to ES modules * Convert events to ES modules * Convert shared to ES modules * Remove CommonJS support from transforms * Move ReactDOMFB entry point code into react-dom/src This is clearer because we can use ES imports in it. * Fix Rollup shim configuration to work with ESM * Fix incorrect comment * Exclude external imports without side effects * Fix ReactDOM FB build * Remove TODOs I don’t intend to fix yet
Great job! |
So in 2023 I still have to use some conversion thingy to natively use React modules in the browser, even though basically all browsers support ES modules? Any reason why the switch to ES modules hasn't been made in over 5 years? Is it lack of interest, lack of contributors, or some technical reason? |
Fix transforms to work in both CJS and ESM modes
invariant
object-assign
Convert packages to ESM
Note: ESM can depend on CJS, but not the other way around. Convert things with less dependents on internals first.
Final cleanup
Perhaps, a whitelist of modules with no side effects?lowPriorityWarning
,warning
, DOM hooks, DOM nesting, hyphenation utils, prop types, dev whitelists, debug things.default
hack in top-levelindex.js
files