Skip to content
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

Migrate all React functionality to use @wordpress/element with dependency extraction #2756

Merged
merged 11 commits into from
May 17, 2022

Conversation

JakePT
Copy link
Contributor

@JakePT JakePT commented May 12, 2022

Description of the Change

Updates dependencies of scripts that were importing React from node_modules to use @WordPress packages with dependency extraction.

Also removes a number of third party dependencies in the Synonyms and Autosuggest features and replaces them with core equivalents where appropraite.

Closes #2613

Alternate Designs

NA

Possible Drawbacks

It's possible that there were compatibility issues with the scripts and other dependencies with the bundled versions of react, but everything appears to be working correctly.

Verification Process

Verify that all admin scripts are working, including the Synonyms editor, Custom Search Results and the Related Posts block.

Checklist:

  • I have read the CONTRIBUTING document.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests passed.

Changelog Entry

Changed - Updated admin scripts to use WordPress's version of React instead of including their own.

Credits

Props @JakePT

@JakePT JakePT requested a review from felipeelia May 12, 2022 08:50
@felipeelia felipeelia added this to the 4.2.0 milestone May 13, 2022
/**
* WordPress dependencies.
*/
import apiFetch from '@wordpress/api-fetch';
Copy link
Member

@nicholasio nicholasio May 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: @wordpress/api-fetch will force loading lodash on the front-end which is a huge dep. I think you should stick to fetch on the front-end.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also recommend looking into this: https://github.com/10up/10up-toolkit/blob/develop/packages/toolkit/README.md#-react--wordpress

There are a few things you can prob dequeue on the front-end (assuming IE 11 is not supported anymore)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicholasio Thanks for the tip re: fetch.

Regarding dequeuing the polyfill, we aren't supporting IE11 ourselves, but it looks like the suggestions would remove the polyfill as a dependency of wp-element for all themes and plugins that use it, not just ElasticPress, and I don't think that's something we'd want to do as a publicly distributed plugin. Thoughts @felipeelia?

We could remove wp-polyfill as a dependency of our scripts in the get_asset_info() function, but if it's a dependecy of wp-element anyway there's probably not much point.

Open to any other ideas though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the change introduced in this PR start to enqueue lodash and wp-polyfill or is that something that is already happening?

Curious to hear from @nicholasio about that anyway. It sounds like if someone out there is relying on those packages and we simply dequeue them in the name of performance, things will break. In that case, we won't be touching it in this release.

In general, that sounds more like an optimization that site owners should apply rather than something we could make in the plugin -- although the performance gain would be generally more significant for most of the sites, we obviously will only hear from the sites it actually broke what was already working before.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are both right, doing this optimization on the plugin wouldn't make sense! I'd however try to stick with standard apis on the front-end. Most of the @wordpress/packages are not optimized for usage outise of the editor. Most of them will load lodash (and some will load moment.js) both well know to be huge packages.

Copy link
Member

@nicholasio nicholasio May 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the very least I'd move away from @wordpress/date as @wordpress/element can be optimized by site owners that do not support IE 11 (and I know wordpress core is trying to optimize the polyfills they add as well)

Moving away from @wordpress/element means shipping react which could cause multiple react to be enqueed on the front-end if other plugins are also relying on react or wordpress/element.

Copy link
Contributor Author

@JakePT JakePT May 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicholasio @wordpress/date does things that we need, namely take raw date data and display it in the site's timezone and language in JS. In the future we'd probably also want to let site owners or devs use their own date format as well. Would you recommend a different approach?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are smaller libraries tath can do what you need. Look at daysjs https://day.js.org/docs/en/plugin/utc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicholasio Ok cool we'll check that out. @felipeelia In the meantime, this PR isn't introducing that package so it probably doesn't need to hold this up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've filed an issue to look into those packages #2768

@JakePT JakePT marked this pull request as draft May 16, 2022 03:09
@JakePT JakePT marked this pull request as ready for review May 17, 2022 09:35
@JakePT JakePT self-assigned this May 17, 2022
@felipeelia felipeelia merged commit d5bd52b into develop May 17, 2022
@felipeelia felipeelia deleted the feature/2613 branch May 17, 2022 18:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Migrate all React functionality to use @wordpress/element with dependency extraction
3 participants