Skip to content

Commit 3224849

Browse files
committed
Add "expose" logic to script modules
This allows for script modules to be exposed in the import map regardless of being a dependency or enqueued. This will enable scripts to depend on script modules by requesting they be exposed.
1 parent 6a07d7a commit 3224849

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

src/wp-includes/class-wp-script-modules.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ class WP_Script_Modules {
3030
*/
3131
private $enqueued_before_registered = array();
3232

33+
/**
34+
* Holds script module identifiers that have been requested for exposure.
35+
*
36+
* "Exposed" indicates that the script module should be exposed in the
37+
* import map regardless of whether it is a dependency of another script
38+
* module.
39+
*
40+
* @since 6.8.0
41+
* @var array<string, true>
42+
*/
43+
private $exposed = array();
44+
3345
/**
3446
* Tracks whether the @wordpress/a11y script module is available.
3547
*
@@ -148,6 +160,17 @@ public function enqueue( string $id, string $src = '', array $deps = array(), $v
148160
}
149161
}
150162

163+
/**
164+
* Marks the script module so it will be exposed in the import map.
165+
*
166+
* @since 6.8.0
167+
*
168+
* @param string $id The identifier of the script module.
169+
*/
170+
public function expose( string $id ) {
171+
$this->exposed[ $id ] = true;
172+
}
173+
151174
/**
152175
* Unmarks the script module so it will no longer be enqueued in the page.
153176
*
@@ -262,12 +285,14 @@ public function print_import_map() {
262285
*
263286
* @since 6.5.0
264287
*
265-
* @return array Array with an `imports` key mapping to an array of script module identifiers and their respective
266-
* URLs, including the version query.
288+
* @return array Array with an `imports` key mapping to an array of script
289+
* module identifiers and their respective URLs, including
290+
* the version query.
267291
*/
268292
private function get_import_map(): array {
269-
$imports = array();
270-
foreach ( $this->get_dependencies( array_keys( $this->get_marked_for_enqueue() ) ) as $id => $script_module ) {
293+
$imports = array();
294+
$script_module_ids = array_unique( array_keys( $this->exposed ) + array_keys( $this->get_marked_for_enqueue() ) );
295+
foreach ( $this->get_dependencies( $script_module_ids ) as $id => $script_module ) {
271296
$imports[ $id ] = $this->get_src( $id );
272297
}
273298
return array( 'imports' => $imports );

0 commit comments

Comments
 (0)