Skip to content

Commit 2570c30

Browse files
Switch JSX transform from automatic to classic runtime
1 parent 8dfb5b2 commit 2570c30

7 files changed

Lines changed: 54 additions & 9 deletions

File tree

js/block-editor.asset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('react-jsx-runtime', 'wp-api-fetch', 'wp-components', 'wp-data', 'wp-i18n'), 'version' => 'aa5db967c92ad9d1ab3d');
1+
<?php return array('dependencies' => array('wp-api-fetch', 'wp-components', 'wp-data', 'wp-element', 'wp-i18n'), 'version' => 'aab7b2e0606348e43cc2');

js/block-editor.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/gallery-block.asset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('react-jsx-runtime', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-components/build-style/style.css', 'wp-data', 'wp-element', 'wp-i18n'), 'version' => '86369b03e045e27bd271');
1+
<?php return array('dependencies' => array('wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-components/build-style/style.css', 'wp-data', 'wp-element', 'wp-i18n'), 'version' => '98f4cd2c29395fad0b1e');

js/gallery-block.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/gallery.asset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('react-jsx-runtime', 'wp-block-editor', 'wp-components', 'wp-components/build-style/style.css', 'wp-data', 'wp-element', 'wp-i18n'), 'version' => '0af55e1154e80535a210');
1+
<?php return array('dependencies' => array('wp-block-editor', 'wp-components', 'wp-components/build-style/style.css', 'wp-data', 'wp-element', 'wp-i18n'), 'version' => 'ce273b9408344c97f28b');

js/gallery.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webpack.config.js

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const CssMinimizerPlugin = require( 'css-minimizer-webpack-plugin' );
77
const RtlCssPlugin = require( 'rtlcss-webpack-plugin' );
88
const TerserPlugin = require( 'terser-webpack-plugin' );
99
const CopyPlugin = require( 'copy-webpack-plugin' );
10+
const webpack = require( 'webpack' );
1011

1112
/**
1213
* WordPress dependencies
@@ -38,9 +39,44 @@ const sharedConfig = {
3839
...defaultConfig.module,
3940
rules: [
4041
// Remove the css/postcss loaders from `@wordpress/scripts` due to version conflicts.
41-
...defaultConfig.module.rules.filter(
42-
( rule ) => ! rule.test.toString().match( '.css' )
43-
),
42+
// Also patch the babel-loader rule to use the classic JSX transform so the build does
43+
// not depend on the `react-jsx-runtime` WP script handle (only available in WP 6.6+).
44+
...defaultConfig.module.rules
45+
.filter( ( rule ) => ! rule.test.toString().match( '.css' ) )
46+
.map( ( rule ) => {
47+
const uses = Array.isArray( rule.use )
48+
? rule.use
49+
: [ rule.use ];
50+
const hasBabelLoader = uses.some( ( use ) =>
51+
use?.loader?.includes( 'babel-loader' )
52+
);
53+
if ( ! hasBabelLoader ) {
54+
return rule;
55+
}
56+
return {
57+
...rule,
58+
use: uses.map( ( use ) => {
59+
if ( ! use?.loader?.includes( 'babel-loader' ) ) {
60+
return use;
61+
}
62+
return {
63+
...use,
64+
options: {
65+
...use.options,
66+
plugins: [
67+
...( use.options?.plugins ?? [] ),
68+
[
69+
require.resolve(
70+
'@babel/plugin-transform-react-jsx'
71+
),
72+
{ runtime: 'classic' },
73+
],
74+
],
75+
},
76+
};
77+
} ),
78+
};
79+
} ),
4480
{
4581
test: /\.css$/,
4682
use: [
@@ -150,6 +186,15 @@ const cldExtras = {
150186
'media-modal': './src/js/components/media-modal.js',
151187
'terms-order': './src/js/terms-order.js',
152188
},
189+
plugins: [
190+
...sharedConfig.plugins,
191+
// Inject React from wp-element into every module using JSX, so the
192+
// classic transform (React.createElement) works without explicit imports
193+
// on all WP versions (wp-element has been available since WP 5.0).
194+
new webpack.ProvidePlugin( {
195+
React: '@wordpress/element',
196+
} ),
197+
],
153198
};
154199

155200
module.exports = [ cldCore, cldExtras ];

0 commit comments

Comments
 (0)