-
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: Remove remaining Lodash from @wordpress/components
package
#49794
Conversation
Size Change: 0 B Total Size: 1.37 MB ℹ️ View Unchanged
|
@@ -68,7 +68,6 @@ | |||
"gradient-parser": "^0.1.5", | |||
"highlight-words-core": "^1.2.2", | |||
"is-plain-object": "^5.0.0", | |||
"lodash": "^4.17.21", |
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.
Lodash is no longer bundled for the web build since it was only used for mobile components, but in any case it's nice to see it gone from one of our largest packages.
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.
Is this the reason why there are no bundle size differences for this PR?
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.
Yes 😉 You can see the bundle size reduction and Lodash removal when we removed the last non-mobile bits:
- Last bundle that contains Lodash: https://bundlephobia.com/package/@wordpress/components@23.5.0
- First bundle that no longer contains Lodash: https://bundlephobia.com/package/@wordpress/components@23.6.0
@@ -273,8 +272,8 @@ class BottomSheetCell extends Component { | |||
return accessibilityLabel || label; | |||
} | |||
|
|||
if ( isEmpty( value ) ) { | |||
return isEmpty( help ) | |||
if ( ! value ) { |
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.
As you can see below, value
is used as a string, therefore we can safely use it as a string in the if()
, simplifying the condition.
@@ -288,7 +287,7 @@ class BottomSheetCell extends Component { | |||
help | |||
); | |||
} | |||
return isEmpty( help ) | |||
return ! help |
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.
As you can see above and below, help
is used as a string, therefore we can safely use it as a string in the if()
, simplifying the condition.
@@ -323,7 +322,7 @@ class BottomSheetCell extends Component { | |||
const opacity = | |||
activeOpacity !== undefined | |||
? activeOpacity | |||
: get( platformStyles, 'activeOpacity.opacity' ); | |||
: platformStyles.activeOpacity?.opacity; |
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.
platformStyles
is already being used as an object in this component, so using platformStyles.activeOpacity
is safe. We're keeping the optional chaining for .opacity
since it is not certain that it will exist.
packages/components/CHANGELOG.md
Outdated
- `BottomSheetCell`: Refactor away from Lodash (mobile) ([#49794](https://github.com/WordPress/gutenberg/pull/49794)). | ||
- `parseStylesVariables()`: Refactor away from Lodash (mobile) ([#49794](https://github.com/WordPress/gutenberg/pull/49794)). | ||
- Remove Lodash dependency from components package ([#49794](https://github.com/WordPress/gutenberg/pull/49794)). |
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.
We're adding 3 separate entries since we're touching 2 distinct components and removing a dependency.
const getValueFromObjectPath = ( object, path ) => { | ||
let value = object; | ||
path.forEach( ( fieldName ) => { | ||
value = value?.[ fieldName ]; |
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.
We're abstracting to a separate function because this is needed twice in this component and because it IMHO improves readability.
Optional chaining here will help us handle nullish values both at the first level and nested inside objects or arrays, making the function essentially as save as get()
was.
@@ -231,11 +246,11 @@ export function parseStylesVariables( styles, mappedValues, customValues ) { | |||
customValuesData | |||
) | |||
) { | |||
return get( customValuesData, path ); | |||
return getValueFromObjectPath( customValuesData, path ); |
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.
This is as safe as get()
was, because getValueFromObjectPath()
will handle nullish values.
} | ||
|
||
// Check for camelcase properties. | ||
return get( customValuesData, [ | ||
return getValueFromObjectPath( customValuesData, [ |
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.
This is as safe as get()
was, because getValueFromObjectPath()
will handle nullish values.
acfd83a
to
280b4e9
Compare
What?
This PR removes the remaining Lodash from the
@wordpress/components
package. There were just a couple of straightforward usages in mobile components.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?
We're using direct access with optional chaining as an alternative. We're extracting to a separate module-specific function to avoid repetition and increase clarity.
Testing Instructions