babel-react-optimize icon indicating copy to clipboard operation
babel-react-optimize copied to clipboard

Added support for ES6 imports and exports (and PureComponent)

Open vdh opened this issue 9 years ago • 9 comments

Fixes #23 & #8 by checking imports for import React, { Component } from 'react', and also adds support for the new PureComponent.

It possibly also fixes #22 due to the fix for export declarations (which are mostly only used in the tests, since Babel already converts the ES6 exports down to ES5 syntax).

  • Updated babel-helper-is-react-class
    • Version bump to 2.0.0 because of the API change (requires path instead of path.node)
    • Added a traversal for named imports (e.g. import React, { Component } from 'react')
    • Added checks for PureComponent
  • Updated babel-plugin-transform-react-pure-class-to-function
    • Refactored replacement logic to read more clearly now that there's 3 code paths
    • Added export declaration check, to avoid "We don't know what to do with this node type" errors
    • Added extra tests for new functionality

vdh avatar Dec 12 '16 04:12 vdh

@LeoIannacone Well, I've made a temporary fork on NPM that I've been using: babel-plugin-transform-react-pure-class-to-function-fork

I was planning on deprecating it once this PR got merged.

vdh avatar Mar 24 '17 04:03 vdh

@thedillonb Wouldn't a shouldComponentUpdate inherently contain references to this, and thus would skip the transformation?

vdh avatar Aug 17 '17 23:08 vdh

@thedillonb Actually, after a cursory glance over the plugin, it looks like anything that contains more than just render, static propTypes, or static defaultProps will not be transformed.

vdh avatar Aug 18 '17 00:08 vdh

@vdh, you're right. If there are methods other than render then it will not be transformed. However, in your test case for test/fixtures/pure-component/actual.js and test/fixtures/pure-component/expected.js, the expected is incorrect. Converting a PureComponent into a stateless functional component (SFC) drops the functionality. React.PureComponent implements componentShouldUpdate for you as a shallowCompare a SFC does not do any comparisons on the prop types and with thus always re-render even if the props are the same (see https://github.com/facebook/react/issues/5677, and more specifically https://github.com/facebook/react/issues/5677#issuecomment-241190513 and https://github.com/facebook/react/issues/5677#issuecomment-289324043).

thedillonb avatar Aug 18 '17 14:08 thedillonb

@thedillonb Hmm, that's sort of a limitation in how React currently implements the rendering of stateless functional components, but I get what you're saying.

Perhaps if I added some sort of optional configuration toggle to skip PureComponent? Something like ignorePureComponent to mirror the related config in the prefer-stateless-function rule from eslint-plugin-react.

vdh avatar Aug 22 '17 00:08 vdh

I have forked and revived the original plugin (here, don't like the idea of having presets and plugins mixed) and made it up to date with some of the pull requests in here.

I have added the option (disabled by default) to convert PureComponents or not, and added some additional features.

basilfx avatar Oct 03 '17 21:10 basilfx

You know, maybe if a single person in any of these PRs offered to help out maintaining this project, then it would be more active. But everyone just wants to complain. So you all get nothing

jamiebuilds avatar Oct 03 '17 23:10 jamiebuilds

@thejameskyle I can try at least to go though the opened issues. I use the plugin in production, so I'm interested in it. Would you mind to add some permissions to me?

havenchyk avatar Oct 04 '17 09:10 havenchyk

Added

jamiebuilds avatar Oct 04 '17 23:10 jamiebuilds