-
-
Notifications
You must be signed in to change notification settings - Fork 27k
A workaround to use latest JS features without ejecting, is this considered bad practice? #2912
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
Comments
I don't see anything immediately wrong with this other than the cons you listed. You are, however, exposing yourself to breakages and ecosystem churn by using stage-0 features. This is considered very poor practice for a non-side-project. We always enable the "latest JS features", remember anything not stage 4 is not really JavaScript. It's a proposal -- we do enable proposals, but only those in stage 3. |
Stage 0 was just an example, typically one would go with stage 2 or stage 3 or just cherry pick. I've been developing with a nice set of less-than-stage-3 features for over a year now in React Native, I reached a point where I just cannot truly be productive without utilizing them. Take these two examples which are both at stage 2 at the moment. e.g. I didn't have to write It is similar to going back to building web apps with jQuery instead of React, it's just not the same experience, and that's just the beginning of a very long list of arguments. Language can be -and is- a true enabler. I am hoping that maybe someone already went through this and could possibly provide some input on it. |
We support class properties, just not decorators. The decorators spec implemented by babel has been abandoned and replaced AFAIK; we should see movement on this front with the Babel 7 release. This proposal is still stage 2 so we aren't considering it yet; probably when they go stage 3. |
IMO this is making the setup more complicated than ejecting in the first place. |
@Timer Are you sure? Writing something like this would throw for me. export default new class {
foo = 'bar'; // this.foo is undefined
baz = () => new Date().getTime(); // this.baz() is undefined
}(); |
@gaearon Tried ejecting, I disagree, too much exposed variables that I would rather a tool takes care of them for me, CRA is doing an excellent job in this area. One question though that I couldn't find a straightforward answer to was "What do I lose when ejecting from CRA", any info on that? |
@sonaye switch to NPM 4, wipe out your node_modules, and reinstall. An NPM 5 bug causes ESLint to think that syntax is invalid. |
That's definitely a bug! It works on my system. Can you provide a repro? |
I just tested this on a fresh CRA app and no errors were thrown, after further investigation my guess is that this was ESLint related and not CRA. |
I would like to point out that the decorators spec has changed significantly. I don't recommend anyone to rely on the current legacy transform. See this thread for more info: https://twitter.com/dan_abramov/status/897491076537356288 |
I just want to document that I have personally moved to what I think is a better solution since then, react-app-rewired. const { injectBabelPlugin } = require('react-app-rewired');
module.exports = override = config => {
config = injectBabelPlugin('transform-decorators-legacy', config);
config = injectBabelPlugin('transform-export-extensions', config); // no flow support yet
// getting eslint to work is a bit tricky but can be achieved
// config.module.rules[0].use[0].options.useEslintrc = true;
return config;
};
Coming back to my list of cons.
Hopefully we can see something like this within CRA in the near future, related to #670. |
Nevermind, just realized I already wrote this in the above comment :-) My point is decorators are not a "latest feature". They are a spec with an old legacy version that will never be supported by the browsers (and which you use), and a new version that has not been implemented by Babel (and which is incompatible with your existing code). This is a footgun. Even if we implement a plugin system we won't let users depend on an abandoned proposal (even if they find it useful). It's just a net loss for the ecosystem due to all the confusion and pain it causes. |
@gaearon I completely understand your point, and I do agree to a great extent. I am not advocating for using decorators in any way, but simply documenting/offering a solution/workaround for those who still -or choose to- use the legacy transformation or any other transformations for that matter that are not backed by CRA. At the end of the day, we are responsible for it and moved forward knowing that it's still a proposal. At this stage, legacy decorators are still very valuable for me to be dropped from my stack, until a good replacement is found out there at least. I don't agree however with your point about not giving the developers the freedom to choose or build their own plugins -when that a system is available-, this decision should be left to the developers in my opinion, given that they are prompted that no support is provided for non-CRA-packed plugins. I wouldn't call it a plugin system if only whitelisted add-ons are allowed, I am following the progress here btw #2784. CRA is wonderful because it offers zero configuration out of the box, this is extremely powerful, I would even call it "revolutionary" because I've built with React before CRA. The range between 0 configs and all the configs is massive, the current workaround I showcased is for those who still don't want to go all the way to the extreme end of the range (by ejecting) and can still tweak minor flags that are located at the beginning of this range. You are the maintainers of this project, and whatever decisions you make I will respect and support at the end, and that's not just because you do have a valid point behind it too, which I completely understand its motives, we make different design decisions, that's all :) |
Fair enough! :-) |
@sonaye I created a library called 'create-react-scripts', it use the similar manner as what "react-app-rewired" did but you are able to re-publish it like your version of react-scripts which "react-app-rewired" not allow you to do so. To enable the decorator support, I am still a newbie on open source community, looking for feedback. |
Take a look at this command/script:
We are telling Babel to:
src
directory, this is the main CRAsrc
we are used to.dev
directory, and compile accordingly (compiled version of alldev
is now located insrc
).dev
, now we have an exact replicated and compiled version of our app insrc
.Translating this idea via
package.json
:When running
yarn start
in parallel withyarn start:babel
, we get the desired result.Pros
Cons
Would love to hear your thoughts on this.
The text was updated successfully, but these errors were encountered: