Skip to content

Commit

Permalink
Add suffixes to the ids and an id to the import map
Browse files Browse the repository at this point in the history
  • Loading branch information
luisherranz committed Jan 9, 2024
1 parent 0cb7f1b commit 834693d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/wp-includes/class-wp-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public function print_enqueued_modules() {
array(
'type' => 'module',
'src' => $this->get_versioned_src( $module ),
'id' => $module_id,
'id' => $module_id . '-js-module',
)
);
}
Expand All @@ -199,7 +199,7 @@ public function print_module_preloads() {
echo sprintf(
'<link rel="modulepreload" href="%s" id="%s">',
esc_url( $this->get_versioned_src( $module ) ),
esc_attr( $module_id )
esc_attr( $module_id . '-js-modulepreload' )
);
}
}
Expand All @@ -217,6 +217,7 @@ public function print_import_map() {
wp_json_encode( $import_map, JSON_HEX_TAG | JSON_HEX_AMP ),
array(
'type' => 'importmap',
'id' => 'wp-importmap',
)
);
}
Expand Down
8 changes: 5 additions & 3 deletions tests/phpunit/tests/modules/wpModules.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public function get_enqueued_modules() {
'import' => 'module',
)
) ) {
$enqueued_modules[ $p->get_attribute( 'id' ) ] = $p->get_attribute( 'src' );
$id = preg_replace( '/-js-module$/', '', $p->get_attribute( 'id' ) );
$enqueued_modules[ $id ] = $p->get_attribute( 'src' );
}

return $enqueued_modules;
Expand All @@ -56,7 +57,7 @@ public function get_enqueued_modules() {
*/
public function get_import_map() {
$import_map_markup = get_echo( array( $this->modules, 'print_import_map' ) );
preg_match( '/<script type="importmap">.*?(\{.*\}).*?<\/script>/s', $import_map_markup, $import_map_string );
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'];
}

Expand All @@ -76,7 +77,8 @@ public function get_preloaded_modules() {
'rel' => 'modulepreload',
)
) ) {
$preloaded_modules[ $p->get_attribute( 'id' ) ] = $p->get_attribute( 'href' );
$id = preg_replace( '/-js-modulepreload$/', '', $p->get_attribute( 'id' ) );
$preloaded_modules[ $id ] = $p->get_attribute( 'href' );
}

return $preloaded_modules;
Expand Down

0 comments on commit 834693d

Please sign in to comment.