Skip to content

Commit

Permalink
Inspect wp_scripts and extract module deps from scripts on the page
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed Dec 19, 2024
1 parent 6b56e6a commit d1053c4
Showing 1 changed file with 29 additions and 30 deletions.
59 changes: 29 additions & 30 deletions src/wp-includes/class-wp-script-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,6 @@ 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.
*
Expand Down Expand Up @@ -160,21 +148,6 @@ 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.
*
Expand Down Expand Up @@ -299,13 +272,39 @@ public function print_import_map() {
*
* @since 6.5.0
*
* @global WP_Dependencies $wp_scripts
*
* @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();
$script_module_ids = array_merge( $this->marked_for_inclusion, array_keys( $this->get_marked_for_enqueue() ) );
global $wp_scripts;
$script_module_ids = array();
if ( $wp_scripts instanceof WP_Dependencies ) {
foreach ( $wp_scripts->registered as $dependency ) {
$handle = $dependency->handle;

if (
! $wp_scripts->query( $handle, 'done' ) &&
! $wp_scripts->query( $handle, 'to_do' ) &&
! $wp_scripts->query( $handle, 'enqueued' )
) {
continue;
}

$module_deps = $wp_scripts->get_data( $handle, 'module_deps' );
if ( ! is_array( $module_deps ) ) {
continue;
}

foreach ( $module_deps as $id ) {
$script_module_ids[] = $id;
}
}
}

$script_module_ids = array_merge( $script_module_ids, array_keys( $this->get_marked_for_enqueue() ) );

foreach ( $this->get_dependencies( $script_module_ids ) as $id => $script_module ) {
$src = $this->get_src( $id );
Expand All @@ -314,7 +313,7 @@ private function get_import_map(): array {
}
$imports[ $id ] = $src;
}
foreach ( $this->marked_for_inclusion as $id ) {
foreach ( $script_module_ids as $id ) {
$src = $this->get_src( $id );
if ( null === $src ) {
continue;
Expand Down

0 comments on commit d1053c4

Please sign in to comment.