GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Easily Add an Babel Plugin · Issue #411 · react/create-react-app

Easily Add an Babel Plugin #411

New issue
Copy link
New issue
Copy link
Closed
Closed
Easily Add an Babel Plugin#411
Copy link

Description

@birdwell
birdwell
opened on Aug 9, 2016
Issue body actions

Note from maintainers

For people coming to this thread later—if you use MobX or a similar library, you don’t need decorators. They are just syntax sugar in this case.
Learn more: #214 (comment), #411 (comment).
Here is why we don’t include them: #411 (comment).

The setup is excellent. I need to add the babel plugin ""transform-decorators-legacy"" because a library I'm using has decorators. Yet, I do not want to eject for one simple babel plugin. Is there a way to add a babel plugin without ejecting?

Reacted by Inessa Pokromkina

Activity

  1. eliperelman commented on Aug 9, 2016

    @eliperelman
    eliperelman
    on Aug 9, 2016
    Contributor
    More actions

    @birdwell No, and that is by design. If you need to deviate from the standard babel configuration, you will need to eject. Thanks!

    Reacted by Arty Gus and Anand Kumar Parhi
    Reacted by Tom Robinson, Dmitry Zherebko, Yeyo, Mathew Kleppin, Stefan Korunovski, Prem Ananth and janaszer
  2. birdwell
    closed this as completedon Aug 9, 2016
  3. gaearon commented on Aug 9, 2016

    @gaearon
    gaearon
    on Aug 9, 2016
    Contributor
    More actions

    need to add the babel plugin ""transform-decorators-legacy"" because a library I'm using has decorators.

    This sounds a but confused. If a library uses decorators internally, presumably it is transpiled to ES5 on build so this shouldn’t be an issue.

    If a library tells you to use decorators, most likely there is an alternative way of using it without them. For example people commonly say MobX “needs” decorators but it couldn’t be further from truth: you can use plain functions instead.

  4. gaearon commented on Aug 9, 2016

    @gaearon
    gaearon
    on Aug 9, 2016
    Contributor
    More actions

    If you can clarify which library you meant, I can point you to an example of using it without the decorator syntax.

  5. birdwell commented on Aug 10, 2016

    @birdwell
    birdwell
    on Aug 10, 2016
    Author
    More actions

    Hey Dan, this the library. Thanks a ton. Great work to all of you guys on this project. You are right there is probably an easy way to get around using them. @gaearon

  6. lacker commented on Aug 10, 2016

    @lacker
    lacker
    on Aug 10, 2016
    Contributor
    More actions

    Basically putting a decorator @foo before the definition of X is the same as saying X = foo(X) after X is defined. It's like one more line of code to do things without decorators.

    So there's two decorators in that react-redux-firebase example. One is connect in react-redux. http://redux.js.org/docs/basics/UsageWithReact.html has some examples of using it with no decorator.

    To convert the firebase decorator, look at one of their examples like https://github.com/tiberiuc/redux-react-firebase/blob/master/example/App.js -

    @firebase()
    class TodoItem extends Component {
      // Lots of stuff here
    }
    

    Instead of that you could just do

    class TodoItem extends Component {
      // Lots of stuff here
    }
    TodoItem = firebase()(TodoItem)
    

    Hopefully that helps!

    Reacted by dan, Heliton Nordt, Alexander Christiansson, Fernando Montoya, Alwin Arrasyid, Loginov Roman, Josh Birdwell, Antony Mativos, Pedro Duarte, Huy Nguyen Quang and 13 more
  7. alexanderchr commented on Aug 10, 2016

    @alexanderchr
    alexanderchr
    on Aug 10, 2016
    More actions

    Adding to what @lacker says, I believe that applying HOCs as functions instead of decorators should not be a way to do it, but the preferred way, for three reasons:

    1. It's standardised and one less dependency
    2. It uses the same syntax for functional and class components
    3. It allows exporting the unwrapped component for testing

    Where point three is the most important one because when testing you usually want to test your component, not the HOC.

    // Todo.js
    
    export class Todo extends Component {
      // ...
    }
    
    // Defining an enhance function is down to personal preference, but I find using it in
    // combination with a compose function to be a lot cleaner when using multiple HOCs
    const enhance = compose( // compose can be imported from redux or recompose
      firebase(),
      /* more HOCs */
    );
    
    export default enhance(Todo);
    // Todo.test.js
    
    import EnhancedTodo, { Todo } from 'Todo.js';
    
    // EnhancedTodo is the component wrapped by any HOCs
    // Todo is the bare component
    Reacted by Yevgen Safronov, Jason Trill, Dallon Feldner, Lance Harper, Josh Birdwell, Antony Mativos, erikatg, Brenton Simpson, Josh Carr, Chris and 5 more
    Reacted by 小乔流水乔 and Chris
    Reacted by Patrick Navarro and Chris
  8. gaearon commented on Aug 10, 2016

    @gaearon
    gaearon
    on Aug 10, 2016
    Contributor
    More actions

    Another nice thing that’s nice to know: you can chain decorators applied as functions with any functional composition helper. Redux provides compose, you can find the same function in Lodash as flowRight:

    import { compose } from 'redux'
    
    class MyThing extends Component {
      // ...
    }
    
    // compose multiple "decorators"
    const enhance = compose(
      connect(mapStateToProps),
      injectLocale(),
      firebase()
    )
    
    // export wrapped component
    export default enhance(MyThing)
    Reacted by Huy Nguyen Quang, Lewis Blackwood, Brenton Simpson, Stepan Zastupov, Josh Carr, amirhosein ahmadi, Clint, Marcus Karlsson, stunaz, Alan Thomas and 4 more
  9. birdwell commented on Aug 11, 2016

    @birdwell
    birdwell
    on Aug 11, 2016
    Author
    More actions

    @gaearon @alexanderchr @lacker Thank you guys so much! Learned a bunch from those three comments. Plus, now I don't have to eject. 😄 👍

    Update: Got my project working with above methods and no ejecting! Thank you again!

    Reacted by Tim Arney
  10. seanrasmussen commented on Aug 26, 2016

    @seanrasmussen
    seanrasmussen
    on Aug 26, 2016
    More actions

    As create-react-app is probably more for people starting out, my vote would be to add the decorators to the basic kit. Makes it easy to follow existing tutorials, without having to insert a different syntax and to get it working. For example this great tutorial on MobX us strewn with decorators. https://www.youtube.com/watch?v=nYvNqKrl69s. I'm having to go through and alter code and for a newbie it just makes getting it working a bit more painful...

    BTW- Thanks for all you guys do. I LOVE create-react-app.

    Reacted by Chris
  11. gaearon commented on Aug 26, 2016

    @gaearon
    gaearon
    on Aug 26, 2016
    Contributor
    More actions

    Our position is simple: we add transforms that are either stable enough (like async/await) or heavily used by Facebook (like class properties). Only that lets us be confident in suggesting them, because if something changes in the standard, we’ll write and release a codemod to migrate away from them.

    Since we don’t currently use decorators, we don’t take it upon ourselves to provide a migration path if the standard becomes incompatible. Additionally decorators aren’t even officially supported by Babel (-legacy is there for a reason). And when they are configured slightly incorrectly people blame React.

    So no, we won’t provide support for them until they either become a part of the language, or we start using them internally at Facebook. Sorry! Bring it up with the tutorial authors because MobX absolutely does not require decorators.

    Reacted by Andreas Köberle, seanrasmussen, Pedro Duarte, Sergey Urzhumskov, Brenton Simpson, Chukwuerika Dike, Iddan Aaronsohn, Vcing, Raj Nigam, Franco Risso and 12 more
    Reacted by xiemeilong, Drew Potter, jpdorsy, MiroslavPetrik and Hamid Reza Nikoonia
    Reacted by Hans Piña and Hamid Reza Nikoonia
  12. hybrist commented on Aug 26, 2016

    @hybrist
    hybrist
    on Aug 26, 2016
    More actions

    Another argument against adding the -legacy transform is that any decorator written against it is - as it stands - incompatible with the current decorators proposal. There's multiple breaking changes between the stage-0 proposal (implemented by the -legacy transform) and the current stage-2 proposal. So if you use one of the -legacy-style decorators in your code, you run the risk of locking yourself out of future versions of babel or at least making the upgrade a lot more painful.

    Reacted by dan, Stephane Coué, Brenton Simpson, Akshay Gagal and Hans Piña
  13. seanrasmussen commented on Aug 26, 2016

    @seanrasmussen
    seanrasmussen
    on Aug 26, 2016
    More actions

    Thanks Dan,

    The frontend JS ecosystem has many permutations and it really slows down learning and gettingstuffdone for JSnewbies like me. The reason I like React/Redux so much is that the community is so active and supportive. Cheers!

  14. arisAlexis commented on Aug 26, 2016

    @arisAlexis
    arisAlexis
    on Aug 26, 2016
    More actions

    Decorators would be a very nice addition to have and frankly do you really think they will not be included in JS in the future? Angular uses them heavily, Typescript uses them, Mobx uses them.

    Reacted by fred, Drew Potter and jpdorsy
  15. gaearon commented on Aug 26, 2016

    @gaearon
    gaearon
    on Aug 26, 2016
    Contributor
    More actions

    @arisAlexis I’m afraid you missed my comment where I described exactly why we don’t include them in this tool. Whether I think they will become a part of JS or not is irrelevant here.

  16. gaearon commented on Sep 1, 2016

    @gaearon
    gaearon
    on Sep 1, 2016
    Contributor
    More actions

    For people coming to this thread later—if you use MobX or a similar library, you don’t need decorators. They are just syntax sugar in this case.

    Learn more: #214 (comment), #411 (comment).
    Here is why we don’t include them: #411 (comment).

  17. smmoosavi
    added a commit that references this issue on Mar 13, 2017

    add decorator example

    b2427a4
  18. mgallant23
    added a commit that references this issue on May 10, 2017

    Update README.md

    c5094ac
  19. gaearon commented on Aug 15, 2017

    @gaearon
    gaearon
    on Aug 15, 2017
    Contributor
    More actions

    For anyone reading this in the future, I would like to point out that the decorators spec has changed significantly. I'm really glad we made the decision to not support them at early stage, so users who didn't eject don't need to change their code.

    I don't recommend anyone to rely on the legacy decorator transform (that is not part of CRA). See this thread for more info: https://twitter.com/dan_abramov/status/897491076537356288.

    Reacted by Peter Schussheim, Tomas Tichy, Cam Jackson, raymondtam, Can Ngo and Hans Piña
  20. jerrrrrrrrrrrrry
    added a commit that references this issue on Jul 31, 2018

    This project was bootstrapped with [Create React App](https://github.…

    f45c6a2
  21. maicss commented on Oct 6, 2018

    @maicss
    maicss
    on Oct 6, 2018
    More actions

    Bable implemented decorator plugin: https://babeljs.io/docs/en/babel-plugin-proposal-decorators, is that stable enough? #214 (comment). Thanks!!!

    @gaearon Sorry to bother you, no one replys for a long time.

  22. sadi304 commented on Jan 8, 2019

    @sadi304
    sadi304
    on Jan 8, 2019
    More actions

    i guess it won't be added until it is finalized. @maicss

  23. lock
    locked and limited conversation to collaborators on Jan 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402792 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.