Skip to content

Commit

Permalink
Script Modules: Centralize registration (#65460)
Browse files Browse the repository at this point in the history
Rework how Script Modules are registered in Gutenberg.

Script Module registration is handled in one central place.

A combined assets file is used for Script Modules and registration. This
means that dependencies and versions will be used correctly and kept
up-to-date while avoiding repeated file reads.

Block library Script Module assets that are enqueued on demand _are
registered in a centralized location_. The assets are enqueued on
demand. **This requires a Core change** since the block library PHP
files are synced to Core and also require centralized Script Module
registration (WordPress/wordpress-develop#7360).

This solves a problem where Gutenberg-specific code was being shipped in
Core through block-library.

The block library Script Module asset Module IDs are renamed to indicate
they are view files and align with the naming from #65064:
@wordpress/block-library/query is @wordpress/block-library/query/view
(indicating it is a view file).

---

This is sufficient to change Script Modules to use Gutenberg in a
backwards compatible way:

- `@wordpress/ineractivity` and `@wordpress/interactivity-router` were
  registered on `wp_enqueue_scripts`. That action fires after the
  `wp_default_scripts` used here. Registering an already registered
  Script Module is a no-op. This change registers first.
- The only other Script Modules currently available in Core are from the
  block library. Those have been registered conditionally on use. The ID
  is changed here, so there's little risk of the wrong version being
  used.

There is a Core companion PR that will be necessary to land:
WordPress/wordpress-develop#7360

---

Co-authored-by: sirreal <jonsurrell@git.wordpress.org>
Co-authored-by: gziolo <gziolo@git.wordpress.org>

Source: WordPress/gutenberg@2632234
  • Loading branch information
sirreal committed Sep 20, 2024
1 parent 264943d commit 8b14a08
Show file tree
Hide file tree
Showing 28 changed files with 68 additions and 143 deletions.
1 change: 0 additions & 1 deletion build-module/a11y/index.min.asset.php

This file was deleted.

2 changes: 1 addition & 1 deletion build-module/a11y/index.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions build-module/assets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('a11y/index.min.js' => array('dependencies' => array(), 'version' => '25c9ec16abe91b62e906', 'type' => 'module'), 'block-library/file/view.min.js' => array('dependencies' => array('@wordpress/interactivity'), 'version' => '0600eabcab185ceb0430', 'type' => 'module'), 'block-library/image/view.min.js' => array('dependencies' => array('@wordpress/interactivity'), 'version' => 'e90ba1f1006920e85ee5', 'type' => 'module'), 'block-library/navigation/view.min.js' => array('dependencies' => array('@wordpress/interactivity'), 'version' => '7b370e821516feba4955', 'type' => 'module'), 'block-library/query/view.min.js' => array('dependencies' => array('@wordpress/interactivity', array('id' => '@wordpress/interactivity-router', 'import' => 'dynamic')), 'version' => '1100711c4bda188467ac', 'type' => 'module'), 'block-library/search/view.min.js' => array('dependencies' => array('@wordpress/interactivity'), 'version' => '69851b7ac418d6ef7967', 'type' => 'module'), 'interactivity/index.min.js' => array('dependencies' => array(), 'version' => '5f35b6d3bd962e6e82b5', 'type' => 'module'), 'interactivity/debug.min.js' => array('dependencies' => array(), 'version' => 'b78f5dee01439db66197', 'type' => 'module'), 'interactivity-router/index.min.js' => array('dependencies' => array('@wordpress/interactivity', array('id' => '@wordpress/a11y', 'import' => 'dynamic')), 'version' => '67c16aba5bbfcdd72f9a', 'type' => 'module'));
1 change: 0 additions & 1 deletion build-module/block-library/file/view.min.asset.php

This file was deleted.

Loading

0 comments on commit 8b14a08

Please sign in to comment.