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
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions .pnpmfile.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,6 @@ function afterAllResolved( lockfile ) {
return lockfile;
}

// eslint-disable-next-line no-unused-vars -- Don't care.
for ( const [ k, v ] of Object.entries( lockfile.packages ) ) {
// Forbid `@wordpress/dependency-extraction-webpack-plugin` v6 until WP 6.5 support is dropped.
// https://github.com/WordPress/gutenberg/issues/62202
if (
k.startsWith( '@wordpress/dependency-extraction-webpack-plugin@' ) &&
! k.startsWith( '@wordpress/dependency-extraction-webpack-plugin@5.' )
) {
throw new Error(
'@wordpress/dependency-extraction-webpack-plugin >= 6.0.0 is not allowed until we drop WordPress 6.5 support.\nSee https://github.com/WordPress/gutenberg/issues/62202 for details.'
);
}
}
return lockfile;
}

Expand Down
6 changes: 5 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
coder-karen marked this conversation as resolved.
Show resolved Hide resolved
Type: added

Assets: Add JSX runtime polyfill here so that it applies to all relevant plugins in the monorepo, preventing site breakages when WP < 6.6 and packages are included that use React's automatic runtime to transform JSX.
coder-karen marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions projects/packages/assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"test": "jest tests",
"validate": "pnpm exec validate-es build/"
},
"dependencies": {
"react": "18.3.1"
},
"devDependencies": {
"@automattic/jetpack-webpack-config": "workspace:*",
"@wordpress/browserslist-config": "6.2.0",
Expand Down
15 changes: 11 additions & 4 deletions projects/packages/assets/src/class-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -498,14 +498,16 @@ public static function wp_default_scripts_hook( $wp_scripts ) {

// Can't use self::register_script(), this action is called too early.
if ( file_exists( __DIR__ . '/../build/i18n-loader.asset.php' ) ) {
$path = '../build/i18n-loader.js';
$asset = require __DIR__ . '/../build/i18n-loader.asset.php';
$path = '../build/i18n-loader.js';
$asset = require __DIR__ . '/../build/i18n-loader.asset.php';
$jsx_path = '../build/react-jsx-runtime.js';
} else {
$path = 'js/i18n-loader.js';
$asset = array(
$path = 'js/i18n-loader.js';
$asset = array(
'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

}
$url = self::normalize_path( plugins_url( $path, __FILE__ ) );
$url = add_query_arg( 'minify', 'true', $url );
Expand All @@ -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

$jsx_url = self::normalize_path( plugins_url( $jsx_path, __FILE__ ) );
$wp_scripts->add( 'react-jsx-runtime', $jsx_url, array( 'react' ), '18.3.1', true );
}

// endregion .
Expand Down
12 changes: 10 additions & 2 deletions projects/packages/assets/tests/php/test-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ function ( $value ) use ( $value_sets, $i ) {
return $funcs;
};

$mock->expects( $this->exactly( 2 ) )->method( 'add' )
$mock->expects( $this->exactly( 3 ) )->method( 'add' )
->with(
...$with_consecutive(
array(
Expand All @@ -768,7 +768,15 @@ function ( $value ) use ( $value_sets, $i ) {
),
array( 'wp-i18n' ),
),
array( 'wp-jp-i18n-state', false, array( 'wp-deprecated', 'wp-jp-i18n-loader' ) )
array( 'wp-jp-i18n-state', false, array( 'wp-deprecated', 'wp-jp-i18n-loader' ) ),
array(
'react-jsx-runtime',
$this->logicalOr(
'http://www.example.com/wp-content/plugins/jetpack/packages/assets/build/react-jsx-runtime.js',
'http://www.example.com/wp-content/plugins/jetpack/packages/assets/src/js/react-jsx-runtime.js'
),
array( 'react' ),
)
)
);
$mock->expects( $this->exactly( 3 ) )->method( 'add_inline_script' )
Expand Down
19 changes: 19 additions & 0 deletions projects/packages/assets/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,23 @@ module.exports = [
],
},
},
{
entry: {
'react-jsx-runtime': {
import: 'react/jsx-runtime',
},
},
output: {
...jetpackWebpackConfig.output,
path: path.resolve( './build' ),
filename: 'react-jsx-runtime.js',
library: {
name: 'ReactJSXRuntime',
type: 'window',
},
},
externals: {
react: 'React',
},
},
];
Loading