mirrored from git://develop.git.wordpress.org/
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Script Modules: Add method to include modules in importmap #8009
Closed
sirreal
wants to merge
7
commits into
WordPress:trunk
from
sirreal:script-modules/new-method--add-to-importmap
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
77f1193
Add "expose" logic to script modules
sirreal 01cc5c4
Make get_src return nullable
sirreal a7ea9a9
Rework, rename, add tests
sirreal 31ec071
Remove uniqueness checks
sirreal 539e38b
Replace excessively prescriptive comment
sirreal 751887b
Add test for correct merge with regular dependencies
sirreal d30c9de
Fix overwriting array keys
sirreal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,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(); | ||
|
@@ -906,4 +908,72 @@ public static function data_invalid_script_module_data(): array { | |
'string' => array( 'string' ), | ||
); | ||
} | ||
|
||
/** | ||
* @ticket 61500 | ||
*/ | ||
public function test_included_module_appears_in_importmap() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In core, the best practice when using multiple assertions in a test is for each assertion to have a custom error message. Makes debugging easier. |
||
$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->assertCount( 2, $import_map ); | ||
$this->assertArrayHasKey( 'example', $import_map ); | ||
$this->assertArrayHasKey( 'dependency', $import_map ); | ||
} | ||
|
||
/** | ||
* @ticket 61500 | ||
*/ | ||
public function test_included_modules_concat_With_enqueued_dependencies() { | ||
$this->script_modules->register( 'dependency-enqueued', '/dep.js' ); | ||
$this->script_modules->register( | ||
'enqueued', | ||
'/example.js', | ||
array( | ||
array( | ||
'id' => 'dependency-enqueued', | ||
'import' => 'dynamic', | ||
), | ||
) | ||
); | ||
$this->script_modules->enqueue( 'enqueued' ); | ||
|
||
$this->script_modules->register( 'dependency', '/dep.js' ); | ||
$this->script_modules->register( 'example', '/example.js', array( 'dependency' ) ); | ||
|
||
// Only dependency-enqueued should be printed. | ||
$enqueued = $this->get_enqueued_script_modules(); | ||
$this->assertCount( 1, $enqueued ); | ||
$this->assertArrayHasKey( 'enqueued', $enqueued ); | ||
$this->assertSame( array(), $this->get_preloaded_script_modules() ); | ||
|
||
$import_map = $this->get_import_map(); | ||
$this->assertCount( 1, $import_map ); | ||
$this->assertArrayHasKey( 'dependency-enqueued', $import_map ); | ||
|
||
// After including, the importmap should be populated. | ||
$this->script_modules->include_in_import_map( 'example' ); | ||
|
||
$enqueued = $this->get_enqueued_script_modules(); | ||
$this->assertCount( 1, $enqueued ); | ||
$this->assertArrayHasKey( 'enqueued', $enqueued ); | ||
$this->assertSame( array(), $this->get_preloaded_script_modules() ); | ||
|
||
$import_map = $this->get_import_map(); | ||
$this->assertCount( 3, $import_map ); | ||
$this->assertArrayHasKey( 'dependency-enqueued', $import_map ); | ||
$this->assertArrayHasKey( 'dependency', $import_map ); | ||
$this->assertArrayHasKey( 'example', $import_map ); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this method name is too specific and tied to the implementation.
Originally I had used
expose()
and was also consideringmake_available()
. I'm wondering about a future where this may add some option arguments for preload, etc.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My first thought was
add_to_import_map()