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

Assets: Add JSX runtime polyfill to prevent issues with React's automatic runtime being used to transform jsx #38428

Merged
merged 15 commits into from
Jul 23, 2024

Conversation

coder-karen
Copy link
Contributor

@coder-karen coder-karen commented Jul 19, 2024

Fixes #38424

Proposed changes:

  • This PR adds a polyfill for the react-jsx-runtime script, which will allow us to update the dependency-extraction-webpack-plugin package or add any other relevant package that uses React's automatic runtime to transform JSX, as it prevents breakages caused by old JSX transforms used throughout the codebase.
  • It also removes a recently added script to prevent updating the above mentioned package.

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Jetpack product discussion

#38424

Does this pull request change what data or activity we track or use?

No.

Testing instructions:

To test, you'll need WordPress 6.5.5 active on a test site.

  • If you're testing locally and using Docker, download WordPress 5.5 and then replace tools/docker/wordpress/wp-admin, and tools/docker/wordpress/wp-includes. Then replace tools/docker/wordpress/wp-settings.php. I found this was enough to be able to replicate the issue and test the fix.
  • You'll also need to make sure your site doesn't include a plugin which also uses the polyfill, and Gutenberg will need to be deactivated. So best to keep it simple with plugins.

To replicate the issue:

  • Apply this patch, and comment out the changes in webpack.config.js and class-assets.php.
  • Also, search for all instances of "@wordpress/dependency-extraction-webpack-plugin" (there should be 6), and change the version to 6.0.0.
  • Then run jetpack build --deps plugins/jetpack.
  • Visit /wp-admin/admin.php?page=my-jetpack. The main My Jetpack UI doesn't load anything.
  • Visit /wp-admin/admin.php?page=jetpack#/dashboard. The main content area doesn't load anything.
  • Other plugins to test with that display broken / no UI:
    • Jetpack Social plugin: /wp-admin/admin.php?page=jetpack-social
    • Automattic for Agencies: /wp-admin/options-general.php?page=automattic-for-agencies-client
    • Jetpack Backup: /wp-admin/admin.php?page=jetpack-backup
    • Jetpack Search: /wp-admin/admin.php?page=jetpack-search
    • Jetpack VideoPress: /wp-admin/admin.php?page=jetpack-videopress

To test the fix:

  • Un-comment the previously commented out changes, and re-build.
  • Both of those Jetpack pages should load properly, as should the admin pages for the other plugins mentioned above.

Copy link
Contributor

github-actions bot commented Jul 19, 2024

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Team Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


The e2e test report can be found here. Please note that it can take a few minutes after the e2e tests checks are complete for the report to be available.


Once your PR is ready for review, check one last time that all required checks appearing at the bottom of this PR are passing or skipped.
Then, add the "[Status] Needs Team Review" label and ask someone from your team review the code. Once reviewed, it can then be merged.
If you need an extra review from someone familiar with the codebase, you can update the labels from "[Status] Needs Team Review" to "[Status] Needs Review", and in that case Jetpack Approvers will do a final review of your PR.


Jetpack plugin:

The Jetpack plugin has different release cadences depending on the platform:

  • WordPress.com Simple releases happen daily.
  • WoA releases happen weekly.
  • Releases to self-hosted sites happen monthly. The next release is scheduled for August 6, 2024 (scheduled code freeze on August 5, 2024).

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Backup plugin:

  • Next scheduled release: August 6, 2024.
  • Scheduled code freeze: July 29, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Boost plugin:

  • Next scheduled release: August 6, 2024.
  • Scheduled code freeze: July 29, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Search plugin:

  • Next scheduled release: August 6, 2024.
  • Scheduled code freeze: July 29, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Social plugin:

  • Next scheduled release: August 6, 2024.
  • Scheduled code freeze: July 29, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Starter Plugin plugin:

  • Next scheduled release: August 6, 2024.
  • Scheduled code freeze: July 29, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Protect plugin:

  • Next scheduled release: August 6, 2024.
  • Scheduled code freeze: July 29, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Videopress plugin:

  • Next scheduled release: August 6, 2024.
  • Scheduled code freeze: July 29, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Migration plugin:

  • Next scheduled release: August 6, 2024.
  • Scheduled code freeze: July 29, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Mu Wpcom plugin:

  • Next scheduled release: August 6, 2024.
  • Scheduled code freeze: July 29, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Inspect plugin:

  • Next scheduled release: August 6, 2024.
  • Scheduled code freeze: July 29, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Wpcomsh plugin:

  • Next scheduled release: August 6, 2024.
  • Scheduled code freeze: July 29, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Automattic For agencies client plugin:

  • Next scheduled release: August 6, 2024.
  • Scheduled code freeze: July 29, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Classic Theme helper plugin plugin:

  • Next scheduled release: August 6, 2024.
  • Scheduled code freeze: July 29, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

Copy link
Contributor

github-actions bot commented Jul 19, 2024

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WordPress.com Simple site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin, and enable the fix/jsx-runtime-react-19-polyfill branch.

    • For jetpack-mu-wpcom changes, also add define( 'JETPACK_MU_WPCOM_LOAD_VIA_BETA_PLUGIN', true ); to your wp-config.php file.
  • To test on Simple, run the following command on your sandbox:

    bin/jetpack-downloader test jetpack fix/jsx-runtime-react-19-polyfill
    
    bin/jetpack-downloader test jetpack-mu-wpcom-plugin fix/jsx-runtime-react-19-polyfill
    

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@coder-karen coder-karen marked this pull request as ready for review July 22, 2024 13:31
…fy whoever removes this to also clean up the related test
@coder-karen coder-karen requested a review from a team July 22, 2024 13:55
@coder-karen coder-karen added [Status] Needs Review To request a review from Crew. Label will be renamed soon. and removed [Status] In Progress labels Jul 22, 2024
@sc0ttkclark
Copy link
Contributor

Use this same polyfill handling for Pods (modified for our stuff) and it worked great.

Copy link
Contributor

@anomiex anomiex left a comment

Choose a reason for hiding this comment

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

Nice! A few small notes inline.

Should we go ahead and update our existing @wordpress/dependency-extraction-webpack-plugin deps to 6.3.0 right away instead of waiting for the next renovate run to do it?

Also, we should remove this so Renovate will do it:

// Forbid `@wordpress/dependency-extraction-webpack-plugin` v6 until WP 6.5 support is dropped.
// https://github.com/WordPress/gutenberg/issues/62202
{
matchPackageNames: [ '@wordpress/dependency-extraction-webpack-plugin' ],
allowedVersions: '<6.0.0',
},

'dependencies' => array( 'wp-i18n' ),
'version' => filemtime( __DIR__ . "/$path" ),
);
$jsx_path = 'js/react-jsx-runtime.js';
Copy link
Contributor

Choose a reason for hiding this comment

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

This file doesn't exist.

OTOH, if the package hasn't been built I don't know of anything that will work right. Even if we were to create this file, the browser wouldn't process the import or require() correctly. Even if we point it at ../node_modules/react/cjs/react-jsx-runtime.production.min.js, that has a require() in it too.

So it might be best to just use ../build/react-jsx-runtime.js always (just hard-code it on line 537 instead of setting a $jsx_path at all IMO) and let the browser 404 on that if the package hasn't been built. 🤷

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, I've gone with hard coding: ab51d24

@@ -529,6 +531,11 @@ public static function wp_default_scripts_hook( $wp_scripts ) {
$wp_scripts->add( 'wp-jp-i18n-state', false, array( 'wp-deprecated', 'wp-jp-i18n-loader' ) );
$wp_scripts->add_inline_script( 'wp-jp-i18n-state', 'wp.deprecated( "wp-jp-i18n-state", { alternative: "wp-jp-i18n-loader" } );' );
$wp_scripts->add_inline_script( 'wp-jp-i18n-state', 'wp.jpI18nState = wp.jpI18nLoader.state;' );

// Register the React JSX runtime script - used as a polyfill until we can update JSX transforms. See https://github.com/Automattic/jetpack/issues/38424.
// Note: when removing this, also remove the inclusion of `react-jsx-runtime` from test_wp_default_scripts_hook where it is expected to be returned when the $mock calls the 'add' method.
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add a @todo here mentioning WordPress 6.5, because that's usually the first thing we look for when preparing PRs like #38386. 🙂

Suggested change
// Note: when removing this, also remove the inclusion of `react-jsx-runtime` from test_wp_default_scripts_hook where it is expected to be returned when the $mock calls the 'add' method.
// @todo Remove this when we drop support for WordPress 6.5
// Note: when removing this, also remove the inclusion of `react-jsx-runtime` from test_wp_default_scripts_hook where it is expected to be returned when the $mock calls the 'add' method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the tip. I extended the comment a bit to mention the test so added it in a new commit: ce92f21

@github-actions github-actions bot added [Plugin] Automattic For Agencies Client [Plugin] Backup A plugin that allows users to save every change and get back online quickly with one-click restores. labels Jul 22, 2024
@github-actions github-actions bot added [Plugin] Boost A feature to speed up the site and improve performance. [Plugin] Classic Theme Helper Plugin [Plugin] CRM Issues about the Jetpack CRM plugin [Plugin] Inspect [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Plugin] Migration [Plugin] mu wpcom jetpack-mu-wpcom plugin [Plugin] Protect A plugin with features to protect a site: brute force protection, security scanning, and a WAF. [Plugin] Search A plugin to add an instant search modal to your site to help visitors find content faster. [Plugin] Social Issues about the Jetpack Social plugin [Plugin] Starter Plugin [Plugin] VideoPress A standalone plugin to add high-quality VideoPress videos to your site. [Plugin] Wpcomsh labels Jul 22, 2024
@coder-karen
Copy link
Contributor Author

Thanks for the review!

Should we go ahead and update our existing @wordpress/dependency-extraction-webpack-plugin deps to 6.3.0 right away instead of waiting for the next renovate run to do it?

I think I'll leave it for this PR as I personally haven't looked at everything else that the updated version may introduce, if relevant, so to be safe I think it's better in another PR.

Also, we should remove this so Renovate will do it:

Nice, thanks for pointing that out - I've removed that line now so auto-updating can happen.

Copy link
Contributor

@anomiex anomiex left a comment

Choose a reason for hiding this comment

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

Let's do it!

@coder-karen coder-karen merged commit cad07b5 into trunk Jul 23, 2024
73 checks passed
@coder-karen coder-karen deleted the fix/jsx-runtime-react-19-polyfill branch July 23, 2024 07:13
@github-actions github-actions bot removed the [Status] Needs Review To request a review from Crew. Label will be renamed soon. label Jul 23, 2024
fushar added a commit that referenced this pull request Jul 23, 2024
…'s automatic runtime being used to transform jsx (#38428)"

This reverts commit cad07b5.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Assets [Plugin] Automattic For Agencies Client [Plugin] Backup A plugin that allows users to save every change and get back online quickly with one-click restores. [Plugin] Boost A feature to speed up the site and improve performance. [Plugin] Classic Theme Helper Plugin [Plugin] CRM Issues about the Jetpack CRM plugin [Plugin] Inspect [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Plugin] Migration [Plugin] mu wpcom jetpack-mu-wpcom plugin [Plugin] Protect A plugin with features to protect a site: brute force protection, security scanning, and a WAF. [Plugin] Search A plugin to add an instant search modal to your site to help visitors find content faster. [Plugin] Social Issues about the Jetpack Social plugin [Plugin] Starter Plugin [Plugin] VideoPress A standalone plugin to add high-quality VideoPress videos to your site. [Plugin] Wpcomsh [Tests] Includes Tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JSX Transform: React compatibility - Add polyfill to prevent issues on sites running WP 6.5.5 or lower
3 participants