-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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
Add babel-plugin-lodash if app depends on Lodash #1069
Comments
My turn to contribute, I will have time Tuesday 👍 |
Sounds great, you got it! |
This seems like an odd departure from the intentional simplicity of this project. |
Simplicity is in how to use it, not necessarily in how it works internally. |
To be clear this change has zero effect on the "public" API pre-ejecting or even post-ejecting if you don't use Lodash. If you do use it, you'll see one line in I just described how the feature should be implemented, not how it is used. |
I haven't checked recently, but at one point |
I tend to agree with @tmc But there's already a fix available for this original issue. Just use the following syntax. import pick from 'lodash/pick'; That's what recommends by lodash. I think there could be more optimizations we could do like this. (may be in the future for different libraries). So, are we going to add those babel plugins as well? Anyway, I think it's time to re-think about allowing the user to customize babel and webpack config. |
Just complementing @arunoda's post: import pick from 'lodash.pick' And just install So, it will be great if we had a way to customize webpack witout eject it :) |
I know that but many beginners don't.
I would add them for very popular libraries. Let's not forget Lodash is (one of?) the most depended package on npm.
Customising Babel config leads to hard-to-trace issues like https://github.com/cssinjs/react-jss/issues/32#issuecomment-190819245. Until Babel fixes such issues I am opposed to it. Customising Webpack config is even more dangerous because we plan to migrate to Webpack 2 soon. So there would be breakage if you rely on the configuration format. Not to say changes like #1059 would be hard to introduce without breaking everyone. So no, I don't think this is a good idea for the project either at this time.
If it breaks the code we can't use it of course. My assumption is it doesn't but we need to check the issue tracker. However if by "doesn't work" you mean that it bails out of optimizing this is fine. |
👍 but I don't like the |
I would love to hear more arguments for why we shouldn't do it but not the "slippery slope" kind. I think nothing prevents us from being reasonably smart and I'm open to adding more useful plugins if they can be enabled when you use a specific library, don't need configuration, and just make your code run better. |
I didn't mean it would behave incorrectly, I meant that we should optimize performance and avoid running it for people who don't depend on Lodash. Now that I think of it, we don't need a flag at all. We can just add a plugin separately. |
I understand :) But, AFAIK, |
So, I suppose I started this round of this discussion by hacking our webpack config in Automattic/wp-api-console#45. If lodash chaining is a problem (#1069 (comment)), this would be a reason not to add If It's a shame to have to eject, and thereby lose out on all of the future features of create-react-app, because of a couple specific things your project would benefit from. I think monkey-patching the config like this is a fair compromise, but it comes along with a few extra tasks that should be done:
I would love to see this technique described in the documentation with some or all of those caveats next to it. |
Why does it? This sounds like a bug or a misunderstanding to me. Since Babel operates on files separately, how can this be true? |
Fair enough, I'm just asking you to file issues along the way so that excluding them is a conscious decision on our side and not just an omission. |
@jdalton explained this in this comment. |
@alanhussey's comment above bears discussion. The If this plugin is disabled except on production builds, then their prod build will throw errors whenever |
Yes, of course if this is how this plugin behaves, we can't include it. |
Is this a problem though? My suggestion is to include it automatically if lodash is a dependency. So if it's not a dependency we wouldn't include it at all. |
@gaearon, nope, as I said it's not an issue for you :) |
Ah, I missed that sentence, sorry 😅 |
@jdalton Not necessary for create-react-app, but if it's easy I would appreciate it for myself :) |
Easy peasy lodash/babel-plugin-lodash@33e503a. |
It seems like the above change to babel-plugin-lodash is necessary for create-react-app, unless I am missing something any code using |
I like the option, like @gaearon said lodash is lot used and I think sometimes it's better to take decision for the user (also providing opt-out option with ejecting) and educate him through the creat-react-app choices on how to build webapps. And about Dropped my first work into a PR, will wait the go to continue from the maintainers after everyone agreed on how to implement this option 👍 |
The showstopper here is plugin failing when But it seems unavoidable because if plugin didn't fail, it would risk users including both a full build and a modular build which is even worse from size point of view. So we likely won't proceed with it. |
I may be able to simply punt on cherry-picking in babel-plugin-lodash if chaining is detected instead of throwing an error. Pull requests welcomed. |
The problem is you couldn't do that reliably cross-file, could you? That is, if file A uses |
I could set a flag so once chain is hit, it punts for all other files after. So while not perfect it reduces the impact for the edge case and improves things for everyone else. |
@jdalton Is it really something that should be done? I thought using chaining was discouraged? |
If you dig a more FP approach or cherry-picking then you'll likely avoid chaining. What choice do I have when folks like Dan refuse to use it otherwise 😋 ? From my standpoint, more folks with smaller bundles is a good thing. So if it helps to avoid throwing, it's a win. |
To be clear I’m not a fan of chaining myself but I can’t break existing project code and all the snippets people copy-paste from StackOverflow. |
Are there plans to drop chain altogether in some future major? |
Yep! In v5 by way of removing the monolithic entry point in favor of |
Closing because of #1088 (comment). |
If we upgrade the lodash to v5, can we use babel-plugin-lodash in the next release of create-react-app? |
Update: issue is taken by @iam4x. Please don't try to "finish it first"!
As seen in Automattic/wp-api-console#45, it seems like a good idea to do.
While we generally don't harcode support for libraries, Lodash is extremely popular to the point it can deserve special treatment, especially given that the plugin is easy to include conditionally and has significant benefits.
Our Babel preset could accept
isUsingLodash
as an option:It should be
false
by default.Inside the preset, if
isUsingLodash
istrue
and the environment isproduction
(we already have an if branch for environments there), we shouldrequire()
and includebabel-plugin-lodash
(but not in other cases).In Webpack config, we should conditionally pass
isUsingLodash
depending on whether app'spackage.json
'sdependencies
contains Lodash of compatible versions.Finally, after ejecting people who didn't depend on Lodash should have
"presets": ["react-app"]
in.babelrc
, but people who used Lodash at the time they ejected should see"presets": [["react-app", { isUsingLodash": true }]]
in it.Update: issue is taken by @iam4x. Please don't try to "finish it first"!
The text was updated successfully, but these errors were encountered: