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

Interactive template: Use viewModule #57712

Merged
merged 8 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions packages/create-block-interactive-template/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@

## Unreleased

### Enhancement

- Update the template to use `viewModule` in block.json ([#57712](https://github.com/WordPress/gutenberg/pull/57712)).

## 1.11.0 (2023-12-13)

- Add all files to the generated plugin zip. [#56943](https://github.com/WordPress/gutenberg/pull/56943)
- Prevent crash when Gutenberg plugin is not installed. [#56941](https://github.com/WordPress/gutenberg/pull/56941)
- Add all files to the generated plugin zip ([#56943](https://github.com/WordPress/gutenberg/pull/56943)).
- Prevent crash when Gutenberg plugin is not installed ([#56941](https://github.com/WordPress/gutenberg/pull/56941)).
Comment on lines -7 to +12
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most other CHANGELOGs in the repo follow this form so I updated these.


## 1.10.1 (2023-12-07)

- Update template to use modules instead of scripts. [#56694](https://github.com/WordPress/gutenberg/pull/56694)
- Update template to use modules instead of scripts ([#56694](https://github.com/WordPress/gutenberg/pull/56694)).

## 1.10.0 (2023-11-29)

### Enhancement

- Update `view.js` and `render.php` templates to the new `store()` API. [#56613](https://github.com/WordPress/gutenberg/pull/56613)
- Update `view.js` and `render.php` templates to the new `store()` API ([#56613](https://github.com/WordPress/gutenberg/pull/56613)).

## 1.9.0 (2023-11-16)

Expand All @@ -35,4 +39,4 @@

### Enhancement

- Moves the `example` property into block.json by leveraging changes to create-block to now support `example`. [#52801](https://github.com/WordPress/gutenberg/pull/52801)
- Moves the `example` property into block.json by leveraging changes to create-block to now support `example` ([#52801](https://github.com/WordPress/gutenberg/pull/52801)).
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@

// Generate unique id for aria-controls.
$unique_id = wp_unique_id( 'p-' );

// Enqueue the view file.
if (function_exists('gutenberg_enqueue_module')) {
gutenberg_enqueue_module( '{{namespace}}-view' );
}
?>

<div
Expand Down
6 changes: 6 additions & 0 deletions packages/create-block-interactive-template/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ module.exports = {
supports: {
interactivity: true,
},
viewScript: null,
viewModule: 'file:./view.js',
render: 'file:./render.php',
example: {},
customScripts: {
build: 'wp-scripts build --experimental-modules',
start: 'wp-scripts start --experimental-modules',
},
},
variants: {
basic: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,10 @@ if ( ! defined( 'ABSPATH' ) ) {
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function {{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init() {
register_block_type( __DIR__ . '/build' );

if (function_exists('gutenberg_register_module')) {
gutenberg_register_module(
'{{namespace}}-view',
plugin_dir_url( __FILE__ ) . 'src/view.js',
array( '@wordpress/interactivity' ),
'{{version}}'
);
if ( defined( 'GUTENBERG_VERSION' ) && version_compare( GUTENBERG_VERSION, '17.5', '>=' ) ) {
register_block_type_from_metadata( __DIR__ . '/build' );
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this for?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The viewModule support for registering blocks via metadata is only going to be available in the upcoming Gutenberg release (17.5), so I thought we should gate the way we register the block on the Gutenberg version, otherwise we should manually register the view module.

Copy link
Member Author

@sirreal sirreal Jan 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was intended to work with the following block, we should either drop the conditional here, or revert the commit that removes the manual module registration.

The metadata registration won't work prior to Gutenberg 17.5 for viewModule.

Copy link
Member

@luisherranz luisherranz Jan 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thank you.

This template already requires GB 17.2, so why not update it to require 17.5. After all, is just a way to get started with the Interactivity API, so requiring the latest version of Gutenberg makes sense.

I've pushed a commit with the changes but please review it yourself as well 🙂

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems fine to me 👍 although we will have a window where we require an unreleased Gutenberg version. Maybe we can dist-tag the previous version of this package as latest until 17.5 is released.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's one week only, I think it's fine.

register_block_type( __DIR__ . '/build' );
}
add_action( 'init', '{{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init' );
4 changes: 4 additions & 0 deletions packages/create-block/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### New Feature

- Add support for the `viewModule` property ([#57712](https://github.com/WordPress/gutenberg/pull/57712)).

## 4.32.0 (2023-12-13)

## 4.31.0 (2023-11-29)
Expand Down
2 changes: 2 additions & 0 deletions packages/create-block/lib/init-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ async function initBlockJSON( {
editorStyle,
style,
render,
viewModule,
viewScript,
customBlockJSON,
example,
Expand Down Expand Up @@ -62,6 +63,7 @@ async function initBlockJSON( {
editorStyle,
style,
render,
viewModule,
viewScript,
...customBlockJSON,
} ).filter( ( [ , value ] ) => !! value )
Expand Down
2 changes: 2 additions & 0 deletions packages/create-block/lib/scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module.exports = async (
editorStyle,
style,
render,
viewModule,
viewScript,
variantVars,
customPackageJSON,
Expand Down Expand Up @@ -84,6 +85,7 @@ module.exports = async (
editorStyle,
style,
render,
viewModule,
viewScript,
variantVars,
customPackageJSON,
Expand Down
Loading