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

Convert the Source to ES Modules #11389

Merged
merged 23 commits into from
Nov 2, 2017
Merged

Convert the Source to ES Modules #11389

merged 23 commits into from
Nov 2, 2017

Conversation

gaearon
Copy link
Collaborator

@gaearon gaearon commented Oct 27, 2017

  • Fix transforms to work in both CJS and ESM modes

    • invariant
    • object-assign
    • Jest
  • Convert packages to ESM

    • react
    • react-art
    • react-call-return
    • react-test-renderer
    • react-cs-renderer
    • react-rt-renderer
    • react-noop-renderer
    • react-dom
      • server
      • client
      • events
      • shared
    • react-native-renderer
    • react-reconciler
    • events
    • shared

    Note: ESM can depend on CJS, but not the other way around. Convert things with less dependents on internals first.

  • Final cleanup

    • Remove CJS-only code in Rollup build
      • Forbid CJS
      • Make sure shims work
    • Ensure we don't regress on bundling DEV-only code
      • Perhaps, a whitelist of modules with no side effects?
      • Don't forget: lowPriorityWarning, warning, DOM hooks, DOM nesting, hyphenation utils, prop types, dev whitelists, debug things
    • Revisit the .default hack in top-level index.js files
      • Perhaps, also ESM-ify tests?
    • Consider turning some default exports into named

@gaearon gaearon force-pushed the esm-react branch 4 times, most recently from d68f7c8 to ca686fe Compare October 31, 2017 15:49
@gaearon gaearon requested a review from sebmarkbage October 31, 2017 19:51
@gaearon gaearon force-pushed the esm-react branch 4 times, most recently from 38147b9 to 43b5a04 Compare October 31, 2017 23:01
@bvaughn bvaughn requested review from bvaughn and removed request for sebmarkbage November 1, 2017 16:28
Copy link
Contributor

@bvaughn bvaughn left a 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.
Copy link
Contributor

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.
Copy link
Contributor

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.

Copy link
Collaborator Author

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.

@gaearon
Copy link
Collaborator Author

gaearon commented Nov 1, 2017

There are a couple of small size increases but they seem to be primarily in test renderer and experimental packages.

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.

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.

Yeah. Basically CommonJS builds now include calls like require('warning') that don’t actually do anything. Now that I think of it, this would slightly increase the bundle size for people consuming CommonJS builds (but not increase the bundle themselves). We should find a fix for this.

@blling
Copy link

blling commented Nov 1, 2017

Is this PR ready for merge ? It is really a greate work!

@gaearon
Copy link
Collaborator Author

gaearon commented Nov 1, 2017

Not yet. I'm still fixing up some things.

@gaearon gaearon force-pushed the esm-react branch 2 times, most recently from faeb440 to 9551d73 Compare November 2, 2017 19:15
@gaearon gaearon merged commit 21d0c11 into facebook:master Nov 2, 2017
@gaearon gaearon deleted the esm-react branch November 2, 2017 19:50
@selbekk
Copy link
Contributor

selbekk commented Nov 3, 2017

Great job!

@jerrylau91
Copy link

Nice job!

@Jaikant
Copy link

Jaikant commented Nov 3, 2017

Nice!

@opeologist
Copy link

this didn't make it into 16.1.0-beta, did it @gaearon ? been itching to import react in browsers that support modules

@gaearon
Copy link
Collaborator Author

gaearon commented Nov 3, 2017

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

@opeologist
Copy link

ahh i see, thanks for the clarification!

Ethan-Arrowood pushed a commit to Ethan-Arrowood/react that referenced this pull request Dec 8, 2017
* 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
NMinhNguyen referenced this pull request in enzymejs/react-shallow-renderer Jan 29, 2020
* 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
@getspooky
Copy link

Great job!

@garretwilson
Copy link

garretwilson commented Mar 17, 2023

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

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?

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

Successfully merging this pull request may close these issues.

10 participants