-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Bundle the Interactivity API privately in WordPress Core #5195
Bundle the Interactivity API privately in WordPress Core #5195
Conversation
Please open one. In general, they should be opened before a PR. as noted in the GitHub Pull Requests for Code Review important notes:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
These changes will need to be added simultaneously with the npm package updates, so they can be covered by the Trac ticket for those updates (to be created). At the time we should add everything in the same PR to make things easier for testing purposes.
Some questions about the parts I'm not sure about:
It’s been a constant challenge to coordinate webpack changes that need to be applied in both the Gutenberg plugin and WordPress core. This one seems even more complex because there is a completely new Babel config required for the front end code. |
Good catch. We need to use As for the In the future, however, we will want to allow JSX via Preact in the Does that make sense?
In Gutenberg, it's registered automatically, so I assumed it would be registered automatically in WordPress as well. Is that not the case? As for enqueueing, we'll do it in the By the way, everything is tracked in this Tracking Issue. I should have mentioned it in the description.
Webpack takes care of the order execution and it doesn't matter which chunk loads first. Actually, these types of bundles used to be loaded with |
Makes sense. Does that mean that I don't need to open a Trac ticket for this, or do I? |
It's rather different in WordPress core, but you need to double check. vs wordpress-develop/src/wp-includes/script-loader.php Lines 280 to 282 in 36f8240
The runtime chunk for Interactivity might not be included in the combined asset file because it's excluded from entry points.
That can be automated by the DependencyExtractionWebpackPlugin. I see that the flag |
I looked at the documentation but I couldn't find a way to avoid the replacement of
Ok, we'll take a look at that file. Thank you!
Yes, the runtime is included in the shared chunk. You can test it by running |
You don't need a Track ticket for this; it will be part of the package update ticket. |
I can confirm that you can't use The usage in the webpack config would be: // Same values as keys in config.entry.
const interactiveBlockFiles = [
'file/view',
'image/view',
'navigation/view',
'query/view',
];
// Later in the config.
new DependencyExtractionPlugin( {
injectPolyfill: false,
useDefaults: false,
__experimentalInteractiveBlockFiles: interactiveBlockFiles,
} ) Example asset file for the File block: |
Thinking more broadly about the use case that we have here - shared runtime chunk, I think we should also consider changing the behavior in the Dependency Extraction Webpack Plugin: I think this code could get refactored to as it's no longer the externals, but it could cover also shared chunks like the runtime: const processModule = ( { userRequest } ) => {
chunkDeps.add( this.mapRequestToDependency( userRequest ) );
}; then something like the following could work: new DependencyExtractionPlugin( {
injectPolyfill: false,
useDefaults: false,
requestToHandle( request) {
if ( '@wordpress/interactivity' ) {
return 'wp-interactivity';
}
}
} ) |
Taking into account that we will probably only need this for the WP 6.4 version, I thought that we could avoid modifying What do you think about creating an ad-hoc Webpack plugin to generate the assets file? Something like this: 1f59ead (I only tested the JavaScript side). That way, we can easily remove the plugin once we don't need it anymore. |
You don't necessarily need the asset file to register the temporary script (assuming it's only for WordPress 6.4) with runtime containing the code for Interactive API. It doesn't have any dependencies, and the default version used for registered scripts defaults to the current WP version – 6.4.0, 6.4.1, etc. The following might work, too (I'm not sure if $scripts->add(
'wp-interactivity',
'/wp-includes/js/dist/interactivity.js'
); For view files, I was only referring to the fact that they don't list their dependency on the runtime, so assuming the example it would be Anyway, there are multiple ways it can be done, so feel free to pick one that is the easier to maintain in the full release cycle. |
Ok, I thought the hash was a standard in WP Core and I didn't want to change that for this script. Then it's much easier, thanks Greg 🙂
My idea was to put that in the block themselves ( |
We had to automate managing all the entry points and their dependencies because updating it manually was error-prone and took forever to update on every change in Gutenberg. The nice side-effect was the unique versions generated by webpack that allowed to improve the caching for JS files, as the version only changes when the content of the file is different between releases. So it's particularly handy during bug fix or security releases. It isn't mandatory though and it shouldn't be a big deal for a single entry point if that is meant to exist only for 6.4 cycle. |
This reverts commit 1f59ead.
This reverts commit 940d239.
Ok, I think this should be ready now in case someone wants to take another look. I finally added the |
Closing this, as the works continues now in the official PR: |
What
Prepare WordPress Webpack configuration to bundle the interactive blocks (Navigation, File, Search, Image and Query) using a private version of the Interactivity API.
Why
WP 6.4 will include Gutenberg up to the 16.7 version, which includes the new Image Lightbox, an enhanced pagination for the Query block and accessibility fixes for the Navigation block, all of them powered by the Interactivity API. Although the Interactivity API is still in the proposal phase, we can include a private version that only Core blocks can access in WP 6.4 to release these features/enhancements.
Please note that the Interactivity API is still likely to change before the final public version is released, but by keeping it private to Core blocks in WP 6.4, we can still modify it before it's public. Furthermore, if the proposal is finally rejected for some reason, we just have to replace the Interactivity API implementation of the Core blocks with whatever new approach is adopted instead.
How
By modifying the Webpack configuration of the frontend (
view.js
) files to treat@wordpress/interactivity
as a private module that is moved into its own chunk so it can be shared by all of them.I'm porting the changes that I tested in Gutenberg itself and can be viewed in this PR:
Testing instructions
The latest versions of
@wordpress/block-library
and@wordpress/interactivity
have been included in this PR to simulate a sync and be able to test it. Thosepackage.json
modifications should be removed before merging this PR.You will need to test this using both WordPress and Gutenberg projects.
In the WordPress project:
npm install
to update the@wordpress/block-library
and install@wordpress/interactivity
.npm run build
to generate the new files.In the Gutenberg project:
release/16.6
branch, which is the same version than the one installed from npm.npm install
andnpm run build
to generate the rest of the files.gutenberg/build/block-library/blocks/navigation/view.min.js
).const e = window.wp.interactivity
gutenberg/build/interactivity/index.min.js
).window.wp = window.wp || {}).interactivity = e
That is the public version of the Interactivity API.
Now move the generated files from WordPress to Gutenberg so we can test that they work:
build/wp-includes/js/dist/interactivity.min.js
from WordPress.build/interactivity/index.min.js
in Gutenbergbuild/wp-includes/blocks/navigation/view.min.js
from WordPress.build/block-library/blocks/navigation/view.min.js
in Gutenbergimage
,file
andquery
blocks. Pay attention to the view filenames, some of them have not been renamed yet and are namedview-interactivity.js
Now test that the files work correctly:
gutenberg/build/block-library/blocks/navigation/view.min.js
).self.__WordPressCorePrivateInteractivityAPI__
gutenberg/build/interactivity/index.min.js
).self.__WordPressPrivateInteractivityAPI__
That is the private version of the Interactivity API.
Tasks
@wordpress/interactivity
package privatelywp-interactivity
wp-interactivity
dependency to each interactive Core blockwp-interactivity
dependency gutenberg#54594Before merging this PR…
package.json
changes (this commit: 940d239)view.js
).I haven't opened a Trac ticket yet because I'm not sure if/when I should do it. If I have to open one, let me know.