-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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: Remove completely from @wordpress/media-utils
package
#41967
Conversation
Size Change: +21 B (0%) Total Size: 1.25 MB
ℹ️ View Unchanged
|
5ba6537
to
80a2cbb
Compare
Rebased to keep up to date. Would still love to get a review of this one if anyone has spare review cycles 🙌 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for yet another good removal PR @tyxla! Each of these changes looks good to me visually, and I didn't run into any errors while smoke testing via the Gallery block, and a variety of different Upload / Replace image actions.
LGTM!
Thanks for taking a look @andrewserong 🙌 |
What?
This PR removes all of the Lodash from the
@wordpress/media-utils
package, including thelodash
dependency altogether. There are just a few usages and conversion is pretty straightforward.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?
This PR could serve as a demonstration of why Lodash can be just completely unnecessary and can most often be replaced with elegant JavaScript code. We're dealing with replacing a few methods, namely:
defaults
For the simple use case, it can be replaced just by creating a new object and using object spread syntax. This is the one and only usage of this method, so we're also deprecating it in ESLint as well.
castArray
Replacing
castArray
is as simple as checking if X is an array, and creating an array with the X element if it's not.pick
Replacing
pick
is generally pretty easy when we destructure a few object properties we can name, and create an object with them. However, the usage here has a bit more properties, so we're using areduce()
call to iterate through them all.compact
One of the most straightforward conversions when it's called with arrays - we can just replace it with
Array.prototype.filter( Boolean )
.flatMap
We're replacing it with a
map()
on the result ofObject.entries()
, followed by a.flat()
to flatten the result array.forEach
Simple enough to replace with
Array.prototype.forEach()
get
Straightforward replace with direct access, when necessary we add optional chaining and use nullish coalescing for handling default values.
has
Usually replaced with
Object.hasOwn()
orObject.hasOwnProperty()
, but in this case it's not necessary, because anError
object always has amessage
, and even if it doesn't, we're still allowed to access an undefined property.includes
Simple enough to replace with
Array.prototype.includes()
andString.prototype.includes()
.map
Simple enough to replace with
Array.prototype.map()
.omit
We're using destructuring with rest props, which nicely handles the omission.
some
Simple enough to replace with
Array.prototype.some()
startsWith
Simple enough to replace with
String.prototype.startsWith()
.Testing Instructions
npm run test-unit packages/media-utils/src/utils