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

Experiment: A simple Modules API with no server dependency graph #56092

Closed
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ Insert an image to make a visual statement. ([Source](https://github.com/WordPre

- **Name:** core/image
- **Category:** media
- **Supports:** anchor, color (~~background~~, ~~text~~), filter (duotone)
- **Supports:** anchor, color (~~background~~, ~~text~~), filter (duotone), interactivity
- **Attributes:** align, alt, aspectRatio, caption, height, href, id, lightbox, linkClass, linkDestination, linkTarget, rel, scale, sizeSlug, title, url, width

## Latest Comments
Expand Down
19 changes: 19 additions & 0 deletions lib/experimental/interactivity-api/modules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Interactive modules.
*
* @package Gutenberg
* @subpackage Interactivity API
*/

/**
* Register the `@wordpress/interactivity` module.
*/
function gutenberg_register_interactivity_module() {
gutenberg_register_module(
'@wordpress/interactivity',
'/wp-content/plugins/gutenberg/build/interactivity/index.min.js'
);
}

add_action( 'wp_enqueue_scripts', 'gutenberg_register_interactivity_module' );
40 changes: 0 additions & 40 deletions lib/experimental/interactivity-api/scripts.php

This file was deleted.

161 changes: 161 additions & 0 deletions lib/experimental/modules/class-gutenberg-modules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php
/**
* Gutenberg_Modules class.
*
* Native support for ES Modules and Import Maps.
*
* @package Gutenberg
* @subpackage Modules
*/
class Gutenberg_Modules {
/**
* An array of registered modules, keyed by module identifier.
*
* @var array
*/
private static $registered = array();


/**
* An array of queued modules.
*
* @var string[]
*/
private static $enqueued = array();

/**
* Registers the module if no module with that module identifier already
* exists.
*
* @param string $module_identifier The identifier of the module. Should be unique. It will be used in the final import map.
* @param string $src Full URL of the module, or path of the script relative to the WordPress root directory.
* @param array $args {
* Optional array of arguments.
*
* @type string|bool $ver Optional. String specifying script version number, if it has one, it is added to the URL
* as a query string for cache busting purposes. If version is set to false, a version
* number is automatically added equal to current installed WordPress version. If SCRIPT_DEBUG
* is set to true, it uses the timestamp instead.
* }
*/
public static function register( $module_identifier, $src, $args = array() ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

For the basic version (no server graph), I was wondering if we could support some kind of "flags" or something to say that a script is a backend script, frontend script or both. (It's a small optimization as there's a lot of backend script that are not meant to be loaded in the frontend)

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure. I don't think it makes sense to have a version of the API that doesn't do that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added in 11b4e1b.

// Register the module if it's not already registered.
if ( ! isset( self::$registered[ $module_identifier ] ) ) {
self::$registered[ $module_identifier ] = array(
'src' => $src,
'args' => $args,
);
}
}

/**
* Enqueues a module for output in the page.
*
* @param string $module_identifier The identifier of the module.
* @param string $src Optional. Full URL of the module, or path of the script relative to the WordPress root directory.
* @param array $args {
* Optional array of arguments.
*
* @type string|bool $ver Optional. String specifying script version number, if it has one, it is added to the URL
* as a query string for cache busting purposes. If version is set to false, a version
* number is automatically added equal to current installed WordPress version. If SCRIPT_DEBUG
* is set to true, it uses the timestamp instead.
* }
*/
public static function enqueue( $module_identifier, $src = null, $args = array() ) {
// Register the module if a source is provided and it's not already registered.
if ( $src && ! isset( self::$registered[ $module_identifier ] ) ) {
self::register( $module_identifier, $src, $args );
}

// Add the module to the queue if it's not already there.
if ( ! in_array( $module_identifier, self::$enqueued, true ) ) {
self::$enqueued[] = $module_identifier;
}
}

/**
* Returns the import map array.
*
* @return string The import map.
*/
public static function get_import_map() {
$import_map = array(
'imports' => array(),
);

foreach ( self::$registered as $module_identifier => $module_data ) {
$version = SCRIPT_DEBUG ? '?ver=' . time() : '?ver=' . $module_data['args']['version'] || '';

$import_map['imports'][ $module_identifier ] = $module_data['src'] . $version;
}

return $import_map;
}

/**
* Prints the import map
*/
public static function print_import_map() {
echo '<script type="importmap">' . wp_json_encode( self::get_import_map(), JSON_HEX_TAG | JSON_HEX_AMP ) . '</script>';
}

/**
* Prints all enqueued modules using script tags with type "module".
*/
public static function print_enqueued_modules() {
foreach ( self::$enqueued as $module_identifier ) {
if ( isset( self::$registered[ $module_identifier ] ) ) {
$module = self::$registered[ $module_identifier ];
$version = SCRIPT_DEBUG ? '?ver=' . time() : '?ver=' . $module['args']['version'] || '';
echo '<script type="module" src="' . $module['src'] . $version . '" id="' . $module_identifier . '"></script>';
}
}
}
}

/**
* Registers a JavaScript module. It will be added to the import map.
*
* @param string $module_identifier The identifier of the module. Should be unique. It will be used in the final import map.
* @param string $src Full URL of the module, or path of the script relative to the WordPress root directory.
* @param array $args {
* Optional array of arguments.
*
* @type string|bool $ver Optional. String specifying script version number, if it has one, it is added to the URL
* as a query string for cache busting purposes. If version is set to false, a version
* number is automatically added equal to current installed WordPress version. If SCRIPT_DEBUG
* is set to true, it uses the timestamp instead.
* }
*/
function gutenberg_register_module( $module_identifier, $src, $args = array() ) {
Gutenberg_Modules::register( $module_identifier, $src, $args );
}

/**
* Enqueues a JavaScript module. It will be added to both the import map and a
* script tag with the "module" type.
*
* It registers the module if a source is provided but it won't overwrites the
* value if there is an existing one.
*
* @param string $module_identifier The identifier of the module. Should be unique. It will be used in the final import map.
* @param string $src Optional. Full URL of the module, or path of the script relative to the WordPress root directory.
* @param array $args {
* Optional array of arguments.
*
* @type string|bool $ver Optional. String specifying script version number, if it has one, it is added to the URL
* as a query string for cache busting purposes. If version is set to false, a version
* number is automatically added equal to current installed WordPress version. If SCRIPT_DEBUG
* is set to true, it uses the timestamp instead.
* }
*/
function gutenberg_enqueue_module( $module_identifier, $src = '', $args = array() ) {
Gutenberg_Modules::enqueue( $module_identifier, $src, $args );
}

// Attach the above function to 'wp_head' action hook.
add_action( 'wp_head', array( 'Gutenberg_Modules', 'print_import_map' ) );

// Attach the new function to 'wp_head' action hook.
add_action( 'wp_head', array( 'Gutenberg_Modules', 'print_enqueued_modules' ) );
4 changes: 3 additions & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function gutenberg_is_experiment_enabled( $name ) {

require __DIR__ . '/experimental/interactivity-api/class-wp-interactivity-store.php';
require __DIR__ . '/experimental/interactivity-api/store.php';
require __DIR__ . '/experimental/interactivity-api/scripts.php';
require __DIR__ . '/experimental/interactivity-api/modules.php';
require __DIR__ . '/experimental/interactivity-api/class-wp-directive-processor.php';
require __DIR__ . '/experimental/interactivity-api/class-wp-directive-context.php';
require __DIR__ . '/experimental/interactivity-api/directive-processing.php';
Expand All @@ -148,6 +148,8 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/experimental/interactivity-api/directives/wp-style.php';
require __DIR__ . '/experimental/interactivity-api/directives/wp-text.php';

require __DIR__ . '/experimental/modules/class-gutenberg-modules.php';

// Fonts API / Font Face.
remove_action( 'plugins_loaded', '_wp_theme_json_webfonts_handler' ); // Turns off WordPress 6.0's stopgap handler.

Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
}
},
"supports": {
"interactivity": true,
"anchor": true,
"color": {
"text": false,
Expand Down Expand Up @@ -130,6 +131,5 @@
{ "name": "rounded", "label": "Rounded" }
],
"editorStyle": "wp-block-image-editor",
"style": "wp-block-image",
"viewScript": "file:./view.min.js"
"style": "wp-block-image"
}
16 changes: 4 additions & 12 deletions packages/block-library/src/image/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ function render_block_core_image( $attributes, $content, $block ) {
$link_destination = isset( $attributes['linkDestination'] ) ? $attributes['linkDestination'] : 'none';
$lightbox_settings = block_core_image_get_lightbox_settings( $block->parsed_block );

$view_js_file_handle = 'wp-block-image-view';
$script_handles = $block->block_type->view_script_handles;

/*
* If the lightbox is enabled and the image is not linked, add the filter
* and the JavaScript view file.
Expand All @@ -50,11 +47,10 @@ function render_block_core_image( $attributes, $content, $block ) {
isset( $lightbox_settings['enabled'] ) &&
true === $lightbox_settings['enabled']
) {
$block->block_type->supports['interactivity'] = true;

if ( ! in_array( $view_js_file_handle, $script_handles, true ) ) {
$block->block_type->view_script_handles = array_merge( $script_handles, array( $view_js_file_handle ) );
}
gutenberg_enqueue_module(
'@wordpress/block-library/image',
'/wp-content/plugins/gutenberg/build/interactivity/image.min.js'
);

/*
* This render needs to happen in a filter with priority 15 to ensure
Expand All @@ -71,10 +67,6 @@ function render_block_core_image( $attributes, $content, $block ) {
* other Image blocks.
*/
remove_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15 );
// If the script is not needed, and it is still in the `view_script_handles`, remove it.
if ( in_array( $view_js_file_handle, $script_handles, true ) ) {
$block->block_type->view_script_handles = array_diff( $script_handles, array( $view_js_file_handle ) );
}
}

return $processor->get_updated_html();
Expand Down
1 change: 0 additions & 1 deletion packages/block-library/src/navigation/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@
"interactivity": true,
"renaming": false
},
"viewScript": "file:./view.min.js",
"editorStyle": "wp-block-navigation-editor",
"style": "wp-block-navigation"
}
19 changes: 6 additions & 13 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -637,20 +637,13 @@ function render_block_core_navigation( $attributes, $content, $block ) {
}

$should_load_view_script = ( $has_submenus && ( $attributes['openSubmenusOnClick'] || $attributes['showSubmenuIcon'] ) ) || $is_responsive_menu;
$view_js_file = 'wp-block-navigation-view';

// If the script already exists, there is no point in removing it from viewScript.
if ( ! wp_script_is( $view_js_file ) ) {
$script_handles = $block->block_type->view_script_handles;

// If the script is not needed, and it is still in the `view_script_handles`, remove it.
if ( ! $should_load_view_script && in_array( $view_js_file, $script_handles, true ) ) {
$block->block_type->view_script_handles = array_diff( $script_handles, array( $view_js_file ) );
}
// If the script is needed, but it was previously removed, add it again.
if ( $should_load_view_script && ! in_array( $view_js_file, $script_handles, true ) ) {
$block->block_type->view_script_handles = array_merge( $script_handles, array( $view_js_file ) );
}
// Load the modules.
if ( $should_load_view_script ) {
gutenberg_enqueue_module(
'@wordpress/block-library/navigation-block',
'/wp-content/plugins/gutenberg/build/interactivity/navigation.min.js'
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think we can avoid the file source from the enqueue function? I expect enqueued modules to be registered before hand no? (I know the current scripts API also allows this but it feels weird to me)

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure. I'll do that.

One thing that feels a bit weird to me, though, is to add an entry to the import map for the enqueued modules because they rarely export anything. So I wonder if register and enqueue should be separate things, one adds entries to the import map and the other adds <script> tags. Something like:

  • gutenberg_include_module: adds the module to the import map
  • gutenberg_enqueue_module: adds the module to the header using <script>

Copy link
Contributor

Choose a reason for hiding this comment

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

I see what you mean, I'm not sure at the moment. I also wonder if there are scripts that could be entry points but also dependencies at the same time.

Copy link
Member Author

Choose a reason for hiding this comment

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

I also wonder if there are scripts that could be entry points but also dependencies at the same time.

I think it's a possibility, but it's going to be uncommon.

);
}

// Add directives to the submenu if needed.
Expand Down
3 changes: 1 addition & 2 deletions packages/block-library/src/query/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,5 @@
"layout": true
},
"editorStyle": "wp-block-query-editor",
"style": "wp-block-query",
"viewScript": "file:./view.min.js"
"style": "wp-block-query"
}
19 changes: 5 additions & 14 deletions packages/block-library/src/query/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,11 @@ class="wp-block-query__enhanced-pagination-animation"
}
}

$view_asset = 'wp-block-query-view';
if ( ! wp_script_is( $view_asset ) ) {
$script_handles = $block->block_type->view_script_handles;
// If the script is not needed, and it is still in the `view_script_handles`, remove it.
if (
( ! $attributes['enhancedPagination'] || ! isset( $attributes['queryId'] ) )
&& in_array( $view_asset, $script_handles, true )
) {
$block->block_type->view_script_handles = array_diff( $script_handles, array( $view_asset ) );
}
// If the script is needed, but it was previously removed, add it again.
if ( $attributes['enhancedPagination'] && isset( $attributes['queryId'] ) && ! in_array( $view_asset, $script_handles, true ) ) {
$block->block_type->view_script_handles = array_merge( $script_handles, array( $view_asset ) );
}
if ( $attributes['enhancedPagination'] ) {
gutenberg_enqueue_module(
'@wordpress/block-library/query',
'/wp-content/plugins/gutenberg/build/interactivity/query.min.js'
);
}

$style_asset = 'wp-block-query';
Expand Down
Loading
Loading