-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Experiment: A Modules API with optional static server dependency graph #56118
Experiment: A Modules API with optional static server dependency graph #56118
Conversation
}, | ||
externalsType: 'module', | ||
externals: { | ||
'@wordpress/interactivity': '@wordpress/interactivity', |
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.
I think at some point in our config we need to maintain an esmodules array to keep these as is in the code (just like we have a dedicated config for bundled modules). I expect interactivity to be just one of these modules
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.
Yeah. I didn't pay much attention to the bundling side yet as it shouldn't affect the PHP API, so I just made it work 😄
(I'm not a fan of the code produced by Webpack for ES modules, by the way.)
} | ||
|
||
add_action( 'wp_print_scripts', 'block_core_search_ensure_interactivity_dependency' ); | ||
add_action( 'init', 'register_block_core_search' ); |
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.
I really like how this simplifies the code of all these "interactive" blocks.
This is now part of WordPress core 🎉 The final approach is documented in the dev note: Script Modules in 6.5. |
What?
Built on top of #56092.
This is the second of a series of experiments to understand the requirements of a hypothetical Modules API. More info in the Tracking Issue:
This experiment aims to implement an API that is slightly more complex than the one implemented in the first experiment, with the same characteristics but adding an optional static server dependency graph to be able to preload modules and avoid initial waterfalls:
<script>
tag in the head.<link rel="modulepreload">
tag in the head.Why?
In the first experiment, it's not easy to know which modules need to be preloaded and, therefore, it's not easy to avoid the initial waterfalls.
How?
By leveraging a new
Gutenberg_Modules
class that acts as a singleton and a couple of functions to expose the functionality:gutenberg_register_module
to register modules and add entries to the import map.gutenberg_enqueue_module
to enqueue modules.Then, we print three times using the
wp_head
action:Testing Instructions
type="module"
are loaded by the interactive blocks.@wordpress/interactivity
module is loaded by the other modules usingimport ... from "@wordpress/interactivity"
.@wordpress/interactivity
module is download at the same time than the rest (instead of after, like in the first experiment), and that there is a link tag withrel="modulepreload"
in the head.