WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.52k stars 4.21k forks source link

`@wordpress/api-fetch` package has `lodash` as a dependency #39495

Closed fabiankaegy closed 2 years ago

fabiankaegy commented 2 years ago

The @wordpress/api-fetch package is super useful even outside of the context of the editor. In theory it would be a great package to use on the frontend of any WordPress site to interact with the WordPress Rest API.

Currently however it relies on the @wordpress/url package which intern has a dependency on lodash. Because of that the size of the @wordpress/api-fetch npm package is 230 kB making it un suitable for the sage on the frontend.

gziolo commented 2 years ago

There is a more high-level issue that opened a long time ago: https://github.com/WordPress/gutenberg/issues/17025. I think there is a common agreement that we could replace lodash usage with native API wherever applicable. Should we consolidate the issue with the other one and close this one as duplicate?

gziolo commented 2 years ago

There is also https://github.com/WordPress/gutenberg/issues/16938 that is still open for @wordpress/element so it might need some more thinking on how we approach this growing inconvenience.

fabiankaegy commented 2 years ago

For reference, this here is the only instance of lodash being used in the @wordpress/url package:

https://github.com/WordPress/gutenberg/blob/71a63fd636b871b73e475821f94fa634e7550b92/packages/url/src/clean-for-slug.js#L4

gziolo commented 2 years ago

String.prototype.trim() is a simple one to use as replacement.

deburr is a more tricky one.

Mamaduka commented 2 years ago

We could probably have to re-implement remove_accents in JS and get rid of deburr.

Few folks also pointed out that deburr doesn't handle accent removal correctly.

fabiankaegy commented 2 years ago

@Mamaduka I didn't know that PHP function existed. If we were to port it to JS where should it live? 🤔 Would it be it's own @wordpress/ package or should it live under compose / i18n ?

gziolo commented 2 years ago

Do we use it outside of @wordpress/url? Otherwise, it can be internal for now.

fabiankaegy commented 2 years ago

deburr seems to be used in 9 files across the entire repo: https://github.com/WordPress/gutenberg/search?q=deburr

https://github.com/WordPress/gutenberg/blob/3da717b8d0ac7d7821fc6d0475695ccf3ae2829f/packages/components/src/navigation/utils.js#L4

https://github.com/WordPress/gutenberg/blob/ebb4a8a031061410040625faa801981579880498/packages/url/src/clean-for-slug.js#L4

https://github.com/WordPress/gutenberg/blob/9580b45e6e18dd06076af9f7e1ea66babee22bf5/packages/editor/src/components/page-attributes/parent.js#L11

https://github.com/WordPress/gutenberg/blob/f885488325f47e4c2842b8bb9686a9a9a922a4d4/packages/block-library/src/heading/autogenerate-anchors.js#L4

https://github.com/WordPress/gutenberg/blob/d5915916abc45e6682f4bdb70888aa41e98aa395/packages/components/src/autocomplete/get-default-use-items.js#L4

https://github.com/WordPress/gutenberg/blob/d5915916abc45e6682f4bdb70888aa41e98aa395/packages/components/src/combobox-control/index.js#L5

https://github.com/WordPress/gutenberg/blob/d5915916abc45e6682f4bdb70888aa41e98aa395/packages/block-editor/src/components/inserter/search-items.js#L4

https://github.com/WordPress/gutenberg/blob/4ba530c55bf4271a41ccd640f84165584d8fefd0/packages/blocks/src/store/selectors.js#L6

https://github.com/WordPress/gutenberg/blob/bd514b86b0c7866f4c92f0a395e57f660f0f7d22/packages/components/src/autocomplete/index.js#L4

But the usage in the url package is the only one I would consider one in a public package meant for consumption outside of the editor

Mamaduka commented 2 years ago

We could potentially replace most of them with updated cleanForSlug.

gziolo commented 2 years ago

We could potentially replace most of them with updated cleanForSlug.

The question would be if making @wordpress/url a dependency makes sense in all cases.

Mamaduka commented 2 years ago

The question would be if making @wordpress/url a dependency makes sense in all cases.

Probably not, but we don't need to replace them all. We can start with cases where strings are used for slugs.

gziolo commented 2 years ago

@wordpress/url alone is 3.78 kB without lodash according to https://bundlephobia.com/package/@wordpress/url@3.5.1.

Probably not, but we don't need to replace them all. We can start with cases where strings are used for slugs.

I think that would be a good first step. We can decide later whether it makes sense to extract to its own package.

tyxla commented 2 years ago

I'm planning to start working incrementally on removing Lodash completely from the repository. Usage is not so high, and the bundle size impact will be huge for some of the packages when used externally.

fabiankaegy commented 2 years ago

@tyxla That is awesome to hear <3

The biggest hurdle for me when looking into removing lodash from the @wordpress/url package was that I couldn't find a great replacement for the deburr function.

If you find a way to remove that dependency that will have a huge impact on the ability to use various @wordpress/ packages on the frontend of a site.

tyxla commented 2 years ago

Good to know @fabiankaegy 👍 We'll most likely end up having to introduce either a few new utility functions or reusing some well-established implementations that are broadly adopted by the community. deburr is definitely one of those, but throttle and debounce also immediately come to mind when talking about this. We'll get to them all eventually.

fabiankaegy commented 2 years ago

I took a shot at https://github.com/WordPress/gutenberg/issues/39495#issuecomment-1069107816 but failed because I don't really have a good enough understanding of the underlying text encoding.

tyxla commented 2 years ago

Interesting. Luckily, a few years ago I did implement an almost exact replica of the WP core remove_accents() for a side project: remove-accents.

It seems to be used quite extensively, and I couldn't be happier to spend more time maintaining it than if it was used in Gutenberg. It could be one way to get rid of deburr() usage, even if it meant releasing a new version with some improvements.

tyxla commented 2 years ago

I'm interested to tackle this one as I'm currently focusing on Lodash removal.

tyxla commented 2 years ago

This one should be resolved by #41687