Skip to content

Commit

Permalink
Rework, rename, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed Dec 16, 2024
1 parent 79b1ec5 commit 3d8deff
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 14 deletions.
37 changes: 27 additions & 10 deletions src/wp-includes/class-wp-script-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,20 @@ class WP_Script_Modules {
private $enqueued_before_registered = array();

/**
* Holds script module identifiers that have been requested for exposure.
* Holds script module identifiers that have been marked for inclusion in the import map.
*
* "Exposed" indicates that the script module should be exposed in the
* import map regardless of whether it is a dependency of another script
* module.
* A script module that appears here should be include in the import map regardless of
* whether it is a dependency of another script module.
*
* The values in this array are always `null`. The presence of a Module IDs
* as an array key marks the script module for inclusion in the import map.
* Different values are reserved for possible future use.
*
* @since 6.8.0
* @var array<string, true>
*
* @var array<string, null>
*/
private $exposed = array();
private $marked_for_inclusion = array();

/**
* Tracks whether the @wordpress/a11y script module is available.
Expand Down Expand Up @@ -161,14 +165,18 @@ public function enqueue( string $id, string $src = '', array $deps = array(), $v
}

/**
* Marks the script module so it will be exposed in the import map.
* Marks the script module for inclusion in the import map.
*
* Script Modules can rely on the script module dependency graph to include script modules
* in the import map. This method makes it possible to mark a script module for inclusion
* in the import map without relying on the script module dependency graph.
*
* @since 6.8.0
*
* @param string $id The identifier of the script module.
*/
public function expose( string $id ) {
$this->exposed[ $id ] = true;
public function include_in_import_map( string $id ) {
$this->marked_for_inclusion[ $id ] = null;
}

/**
Expand Down Expand Up @@ -301,14 +309,23 @@ public function print_import_map() {
*/
private function get_import_map(): array {
$imports = array();
$script_module_ids = array_unique( array_keys( $this->exposed ) + array_keys( $this->get_marked_for_enqueue() ) );
$script_module_ids = array_unique( array_keys( $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 );
}

Expand Down
32 changes: 28 additions & 4 deletions tests/phpunit/tests/script-modules/wpScriptModules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -54,18 +54,20 @@ 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( '/<script type="importmap" id="wp-importmap">.*?(\{.*\}).*?<\/script>/s', $import_map_markup, $import_map_string );
return json_decode( $import_map_string[1], true )['imports'];
return isset( $import_map_string[1] )
? json_decode( $import_map_string[1], true )['imports']
: array();
}

/**
* Gets a list of preloaded script modules.
*
* @return array Preloaded script module URLs, keyed by script module identifier.
*/
public function get_preloaded_script_modules() {
private function get_preloaded_script_modules() {
$preloaded_markup = get_echo( array( $this->script_modules, 'print_script_module_preloads' ) );
$p = new WP_HTML_Tag_Processor( $preloaded_markup );
$preloaded_script_modules = array();
Expand Down Expand Up @@ -906,4 +908,26 @@ public static function data_invalid_script_module_data(): array {
'string' => array( 'string' ),
);
}

/**
* @ticket 61500
*/
public function test_added_module_appears_in_importmap() {
$this->script_modules->register( 'dependency', '/dep.js' );
$this->script_modules->register( 'example', '/example.js', array( 'dependency' ) );

// Nothing printed now.
$this->assertSame( array(), $this->get_enqueued_script_modules() );
$this->assertSame( array(), $this->get_preloaded_script_modules() );
$this->assertSame( array(), $this->get_import_map() );

// After including, the importmap should be populated.
$this->script_modules->include_in_import_map( 'example' );
$this->assertSame( array(), $this->get_enqueued_script_modules() );
$this->assertSame( array(), $this->get_preloaded_script_modules() );

$import_map = $this->get_import_map();
$this->assertArrayHasKey( 'example', $import_map );
$this->assertArrayHasKey( 'dependency', $import_map );
}
}

0 comments on commit 3d8deff

Please sign in to comment.