diff --git a/src/wp-includes/class-wp-script-modules.php b/src/wp-includes/class-wp-script-modules.php
index dbfa038f8cbe2..e41d1004350c9 100644
--- a/src/wp-includes/class-wp-script-modules.php
+++ b/src/wp-includes/class-wp-script-modules.php
@@ -30,6 +30,18 @@ class WP_Script_Modules {
*/
private $enqueued_before_registered = array();
+ /**
+ * Holds script module identifiers that have been marked for inclusion in the import map.
+ *
+ * A script module that appears here should be include in the import map regardless of
+ * whether it is in the dependency graph of enqueued script modules.
+ *
+ * @since 6.8.0
+ *
+ * @var string[]
+ */
+ private $marked_for_inclusion = array();
+
/**
* Tracks whether the @wordpress/a11y script module is available.
*
@@ -148,6 +160,21 @@ public function enqueue( string $id, string $src = '', array $deps = array(), $v
}
}
+ /**
+ * Marks the script module for inclusion in the import map.
+ *
+ * This method is intended for use outside of the script module dependency system.
+ * It's recommended that script modules rely on the script module dependency system
+ * to manage the import map.
+ *
+ * @since 6.8.0
+ *
+ * @param string $id The identifier of the script module.
+ */
+ public function include_in_import_map( string $id ) {
+ $this->marked_for_inclusion[] = $id;
+ }
+
/**
* Unmarks the script module so it will no longer be enqueued in the page.
*
@@ -208,10 +235,15 @@ public function add_hooks() {
*/
public function print_enqueued_script_modules() {
foreach ( $this->get_marked_for_enqueue() as $id => $script_module ) {
+ $src = $this->get_src( $id );
+ if ( null === $src ) {
+ continue;
+ }
+
wp_print_script_tag(
array(
'type' => 'module',
- 'src' => $this->get_src( $id ),
+ 'src' => $src,
'id' => $id . '-js-module',
)
);
@@ -228,11 +260,16 @@ public function print_enqueued_script_modules() {
*/
public function print_script_module_preloads() {
foreach ( $this->get_dependencies( array_keys( $this->get_marked_for_enqueue() ), array( 'static' ) ) as $id => $script_module ) {
+ $src = $this->get_src( $id );
+ if ( null === $src ) {
+ continue;
+ }
+
// Don't preload if it's marked for enqueue.
if ( true !== $script_module['enqueue'] ) {
echo sprintf(
'',
- esc_url( $this->get_src( $id ) ),
+ esc_url( $src ),
esc_attr( $id . '-js-modulepreload' )
);
}
@@ -262,14 +299,29 @@ public function print_import_map() {
*
* @since 6.5.0
*
- * @return array Array with an `imports` key mapping to an array of script module identifiers and their respective
- * URLs, including the version query.
+ * @return array Array with an `imports` key mapping to an array of script
+ * module identifiers and their respective URLs, including
+ * the version query.
*/
private function get_import_map(): array {
- $imports = array();
- foreach ( $this->get_dependencies( array_keys( $this->get_marked_for_enqueue() ) ) as $id => $script_module ) {
- $imports[ $id ] = $this->get_src( $id );
+ $imports = array();
+ $script_module_ids = array_merge( $this->marked_for_inclusion, array_keys( $this->get_marked_for_enqueue() ) );
+
+ foreach ( $this->get_dependencies( $script_module_ids ) as $id => $script_module ) {
+ $src = $this->get_src( $id );
+ if ( null === $src ) {
+ continue;
+ }
+ $imports[ $id ] = $src;
}
+ foreach ( $this->marked_for_inclusion as $id ) {
+ $src = $this->get_src( $id );
+ if ( null === $src ) {
+ continue;
+ }
+ $imports[ $id ] = $src;
+ }
+
return array( 'imports' => $imports );
}
@@ -335,11 +387,11 @@ function ( $dependency_script_modules, $id ) use ( $import_types ) {
* @since 6.5.0
*
* @param string $id The script module identifier.
- * @return string The script module src with a version if relevant.
+ * @return string|null The script module src with a version if relevant.
*/
- private function get_src( string $id ): string {
+ private function get_src( string $id ): ?string {
if ( ! isset( $this->registered[ $id ] ) ) {
- return '';
+ return null;
}
$script_module = $this->registered[ $id ];
diff --git a/tests/phpunit/tests/script-modules/wpScriptModules.php b/tests/phpunit/tests/script-modules/wpScriptModules.php
index 85f9599f0dac3..f60666c6b8c52 100644
--- a/tests/phpunit/tests/script-modules/wpScriptModules.php
+++ b/tests/phpunit/tests/script-modules/wpScriptModules.php
@@ -34,7 +34,7 @@ public function set_up() {
*
* @return array Enqueued script module URLs, keyed by script module identifier.
*/
- public function get_enqueued_script_modules() {
+ private function get_enqueued_script_modules() {
$script_modules_markup = get_echo( array( $this->script_modules, 'print_enqueued_script_modules' ) );
$p = new WP_HTML_Tag_Processor( $script_modules_markup );
$enqueued_script_modules = array();
@@ -54,10 +54,12 @@ public function get_enqueued_script_modules() {
*
* @return array Import map entry URLs, keyed by script module identifier.
*/
- public function get_import_map() {
+ private function get_import_map() {
$import_map_markup = get_echo( array( $this->script_modules, 'print_import_map' ) );
preg_match( '/