Skip to content
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

Blocks: Add handling for block.json viewModule #57437

Merged
merged 20 commits into from
Jan 9, 2024

Conversation

sirreal
Copy link
Member

@sirreal sirreal commented Dec 28, 2023

What?

Add viewModule support for registering blocks from metadata.

Part of #57492

To do:

  • Add viewModule field to REST API

Why?

As we introduce module support and the Interactivity API (only available as a module at this time), we should provide at least a similar experience to what we currently have with scripts.

How?

This follows a similar pattern to how WordPress Core manages block scripts. Different script handles are attached to the WP_Block_Type instance. Then those are enqueued when the block is rendered.

We use some filters to achieve the same result. I'd expect this functionality to move to Core when Modules support lands (WordPress/wordpress-develop#5818). See sirreal/wordpress-develop#2 for an example of what that might look like.

Testing Instructions

Add a block using block.json with a viewModule field pointing to its view module. I've compiled a block (with #57461) that's available at this gist. It can be unzipped as a plugin and used for testing.

We should be able to add our block in the editor. If we use the REST API to query block types and search for view_module_ids we should find our block:

(await wp.apiFetch( { path: '/wp/v2/block-types' }))
  .filter( ({ view_module_ids }) => view_module_ids.length )
  .map(blockType => ({
    name: blockType.name,
    view_module_ids: blockType.view_module_ids,
  }))[0]
// { "name": "jon/the-block", "view_module_ids": [ "jon-the-block-view-module" ] }

If the block is added to a post and we visit it on the frontend, we should see it working as expected (a counter in our example).

We should also see the expected modules added to the importmap and module preloads on our post with the block. I used an older theme to ensure nothing else was enqueuing the interactivity module:

document.querySelector('[type=importmap]');
// <script type=​"importmap">​{"imports":{"@wordpress\/interactivity":"…"}}</script>​
document.querySelector('#\\@wordpress\\/interactivity[rel=modulepreload]');
// <link rel="modulepreload" href="…" id="@wordpress/interactivity">

Copy link

This pull request has changed or added PHP files. Please confirm whether these changes need to be synced to WordPress Core, and therefore featured in the next release of WordPress.

If so, it is recommended to create a new Trac ticket and submit a pull request to the WordPress Core Github repository soon after this pull request is merged.

If you're unsure, you can always ask for help in the #core-editor channel in WordPress Slack.

Thank you! ❤️

View changed files
❔ lib/experimental/modules/class-gutenberg-modules.php

@sirreal sirreal added [Feature] Blocks Overall functionality of blocks [Status] In Progress Tracking issues with work in progress [Feature] Script Modules API Related to the Script Modules API that adds support for native ES modules and import maps [Type] Feature New feature to highlight in changelogs. [Type] New API New API to be used by plugin developers or package users. and removed [Type] Feature New feature to highlight in changelogs. labels Dec 28, 2023
@sirreal sirreal force-pushed the js/add-view-module-handling branch from 37e636d to 56a3d3c Compare December 29, 2023 18:59
sirreal added a commit that referenced this pull request Jan 4, 2024
sirreal added a commit that referenced this pull request Jan 4, 2024
This reverts commit 07fc04e64149c70606417ac288bffc648ca4816a.
@sirreal sirreal force-pushed the js/add-view-module-handling branch from 56a3d3c to 3fd6278 Compare January 4, 2024 22:02
Copy link

github-actions bot commented Jan 4, 2024

Flaky tests detected in 3fd6278.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7415313014
📝 Reported issues:

sirreal added a commit that referenced this pull request Jan 5, 2024
Squashed commit of the following:

commit 3fd6278
Author: Jon Surrell <[email protected]>
Date:   Fri Dec 29 15:19:57 2023 +0100

    Comment out useles i18n code

commit 2fdf006
Author: Jon Surrell <[email protected]>
Date:   Fri Dec 29 15:19:44 2023 +0100

    Fix lints

commit 4f7d640
Author: Jon Surrell <[email protected]>
Date:   Fri Dec 29 15:19:31 2023 +0100

    Reuse translation from core

commit 6594c16
Author: Jon Surrell <[email protected]>
Date:   Thu Dec 28 21:13:43 2023 +0100

    Fix lints

commit 58c66d8
Author: Jon Surrell <[email protected]>
Date:   Thu Dec 28 21:10:22 2023 +0100

    Use existing filter over new action

    This is really an action (side effect) but we
    don't need to add a new action if we use
    the filter

commit 7bcb68a
Author: Jon Surrell <[email protected]>
Date:   Thu Dec 28 20:59:05 2023 +0100

    Working implementation with caveats
@sirreal sirreal force-pushed the js/add-view-module-handling branch from fc1fdf3 to 34b5b44 Compare January 9, 2024 15:54
@sirreal
Copy link
Member Author

sirreal commented Jan 9, 2024

I reverted the identifier change, we're back to ID 😄

@sirreal sirreal enabled auto-merge (squash) January 9, 2024 16:06
@sirreal sirreal added the Needs Dev Note Requires a developer note for a major WordPress release cycle label Jan 9, 2024
@sirreal
Copy link
Member Author

sirreal commented Jan 9, 2024

@luisherranz Any preference for how we handle the dev note? Are there other announcements around modules and Interactivity that go together?

@luisherranz
Copy link
Member

I've never done a dev note so I have no idea 😄

But the Interactivity API and Modules API should be announced separately. It happens that the Interactivity API will use the Modules API, but they are conceptually separate things.

@sirreal sirreal merged commit 6a2fda1 into trunk Jan 9, 2024
56 checks passed
@sirreal sirreal deleted the js/add-view-module-handling branch January 9, 2024 16:32
@github-actions github-actions bot added this to the Gutenberg 17.5 milestone Jan 9, 2024
@getdave getdave added the Needs PHP backport Needs PHP backport to Core label Jan 15, 2024
sirreal added a commit that referenced this pull request Feb 7, 2024
Support `viewScriptModule` block.json field for registering blocks from metadata.

#57437 added `viewModule` support, but the Modules API changed in core to refer to "script modules."

---

- `viewModule` is deprecated but supported.
- `viewScriptModule` support is added.

If both fields are present, `viewScriptModule` will be used.

Two REST API fields are included for block types with the same information. `view_module_ids` (deprecated) and `view_script_module_ids`.
pento pushed a commit to WordPress/wordpress-develop that referenced this pull request Feb 8, 2024
Syncing changes from the Gutenberg plugin: WordPress/gutenberg#57437.

Scripts and styles can be registered for blocks via `block.json` metadata. There is now a Modules API, but was no way to register or associate module assets with blocks via `block.json`.

Fixes #60233.
Props jonsurrell, gziolo, cbravobernal, luisherranz, youknowriad.



git-svn-id: https://develop.svn.wordpress.org/trunk@57565 602fd350-edb4-49c9-b593-d223f7449a82
markjaquith pushed a commit to markjaquith/WordPress that referenced this pull request Feb 8, 2024
Syncing changes from the Gutenberg plugin: WordPress/gutenberg#57437.

Scripts and styles can be registered for blocks via `block.json` metadata. There is now a Modules API, but was no way to register or associate module assets with blocks via `block.json`.

Fixes #60233.
Props jonsurrell, gziolo, cbravobernal, luisherranz, youknowriad.


Built from https://develop.svn.wordpress.org/trunk@57565


git-svn-id: http://core.svn.wordpress.org/trunk@57066 1a063a9b-81f0-0310-95a4-ce76da25c4cd
github-actions bot pushed a commit to gilzow/wordpress-performance that referenced this pull request Feb 8, 2024
Syncing changes from the Gutenberg plugin: WordPress/gutenberg#57437.

Scripts and styles can be registered for blocks via `block.json` metadata. There is now a Modules API, but was no way to register or associate module assets with blocks via `block.json`.

Fixes #60233.
Props jonsurrell, gziolo, cbravobernal, luisherranz, youknowriad.


Built from https://develop.svn.wordpress.org/trunk@57565


git-svn-id: https://core.svn.wordpress.org/trunk@57066 1a063a9b-81f0-0310-95a4-ce76da25c4cd
@youknowriad youknowriad added Backported to WP Core Pull request that has been successfully merged into WP Core and removed Needs PHP backport Needs PHP backport to Core labels Feb 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backported to WP Core Pull request that has been successfully merged into WP Core [Feature] Blocks Overall functionality of blocks [Feature] Script Modules API Related to the Script Modules API that adds support for native ES modules and import maps Needs Dev Note Requires a developer note for a major WordPress release cycle [Type] New API New API to be used by plugin developers or package users.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants