-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Description
What problem does this address?
As the Modules API matures and is considered for WordPress Core (WordPress/wordpress-develop#5818), we should consider the experience for working with modules and blocks. The experience of working with modules should not regress compared to the experience of working with scripts.
This proposal is to add Modules API support to blocks via block.json
. The proposal suggests the addition of editorModule
, module
, and viewModule
to block.json
metadata. These fields are analogous to editorScript
, script
, and viewScript
but use the Modules API.
This issue is intended for discussion and tracking of the necessary work to better support modules when working with blocks. The ideas here came from discussions with @luisherranz.
Current status and suggested changes
Block registration
Blocks can be registered using register_block_type_from_metadata
and a block.json
file. These mechanisms support the editorScript
, script
, and viewScript
fields to associate scripts with the block in different contexts so the scripts and be registered and enqueued under the appropriate circumstances.
The Modules API is in Gutenberg and has a proposal for WordPress Core: WordPress/wordpress-develop#5818.
Suggested changes:
register_block_type_from_metadata
should support *module fields:- This can be accomplished initially in Gutenberg via hooks: Blocks: Add handling for block.json viewModule #57437 (proof of concept)
- This functionality should move to Core at some point: POC: Support *module fields in block.json metadata registration sirreal/wordpress-develop#2 (proof of concept)
WordPress script/module dependencies
@wordpress/dependency-extraction-webpack-plugin handles automatic externalization of WordPress script dependencies and generation of a [script].asset.php
file used by register_block_type_from_metadata
.
Suggested changes:
- @wordpress/dependency-extraction-webpack-plugin should generate externalize modules assets correctly and generate asset files in the expected Modules API format when compiling modules: Dependency Extraction Webpack Plugin: Add Module support #57199.
@wordpress/scripts will detect block.json
files in a project and perform webpack compilation to generate the expected assets for use in a production site.
Suggested changes:
- @wordpress/scripts should compile modules as necessary for
*module
fields discovered inblock.json
: WP Scripts: Build block.json viewModule #57461 (proof of concept)
Script/Module compatibility
WordPress core scripts are only available as scripts at this time. None of the core scripts can be used as modules. The exception is @wordpress/interactivity
which is only available as a module.
viewModule
can be used with block using the Interactivity API (@wordpress/interactivity
), but not with other core scripts. Some core scripts are not well suited for use on the frontend so this may not be limiting for viewModule
. One notable exception is an internalization library, @wordpress/i18n
would be nice to have available to modules.
Block.json editorModule
and module
become much less useful, because they will likely need to use scripts to integrate with the editor. We can still build out functionality to support these fields which is very similar to viewModule
.
Suggested changes:
None. This is a broader consideration for the Modules API. Existing scripts need to be made available to modules. I know that @luisherranz has ideas for how to move forward in this area (#55942 (comment)), but for simplicity I'd like to consider the modules-scripts interoperability out of scope for this discussion.