-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Lodash: Refactor away from _.noop()
#41674
Conversation
Size Change: +218 B (0%) Total Size: 1.24 MB
ℹ️ View Unchanged
|
This is a big merge. Do we know what is really saved by this change in terms of bundle size? It'd be a good rationale for the refactoring. |
e486772
to
2e1873b
Compare
Thank you for asking. I know it looks like a lot in this PR, but even if the change touches a lot of files, it's a very straightforward and non-risky one, which is why I preferred to do it in a single PR. It's fair to say particular PR doesn't achieve much in terms of bundle size improvement immediately. However, the bundle size of each package that directly or consequently uses Lodash will end up reduced once all of Lodash is removed. Currently, Lodash is still used in most of the packages, and it's around 73KB in size. We're currently in the process of removing all these usages incrementally (see https://github.com/orgs/WordPress/projects/32) and the collective result is yet to be seen when we end up having fewer and fewer usages in the packages. This comment explains a lot of why we're heading in that direction: #16938 (comment) |
Cool thanks for explaining @tyxla 🙇🏻 |
Isn't lodash removed by using the WordPress Dependency Extraction Plugin? So technically it does not increase the bundle size right? |
That is valid only for the wp-admin, though. Outside of wp-admin, or when a package is used externally, |
What?
Lodash's
noop
is the most used Lodash method in this repository. It's also one of the simplest: it's just an empty function that takes no arguments and returns nothing. This PR aims to remove that usage. While it might look like a large one, it really does the same thing for allnoop
occurrences.Why?
Lodash is known to unnecessarily inflate the bundle size of packages, and in most cases, it can be replaced with native language functionality. See these for more information and rationale:
@wordpress/api-fetch
package haslodash
as a dependency #39495How?
Removing
noop
is straightforward in favor of a simple function with no arguments and an empty body that returns nothing. I've made a conscious decision here not to abstract our own function and reuse it everywhere because that would needlessly complicate the dependency graph. Instead, I've declared anoop
function inline per component where necessary, and that way we get the best of both worlds: we have no additional dependencies, but still, we prevent extra re-renders becausenoop
will always be the same within each component.Testing Instructions
Verify all tests still pass.
Notes
Seems like there is a JS unit test failure, but it's unrelated, and seems to be caused by an obsolete version of#41675 has been merged, so unit tests will now pass.caniuse-lite
, I'm proposing a fix for it in #41675.