Skip to content

Proposal: Support module metadata in block.json for Blocks + Modules API #57492

@sirreal

Description

@sirreal

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:

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/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:

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Feature] Block APIAPI that allows to express the block paradigm.[Feature] Script Modules APIRelated to the Script Modules API that adds support for native ES modules and import maps[Type] DiscussionFor issues that are high-level and not yet ready to implement.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions