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
Allow classic scripts to depend on modules #8024
Open
sirreal
wants to merge
17
commits into
WordPress:trunk
Choose a base branch
from
sirreal:scripts/allow-script-module-dependency
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
39a11a8
Add "expose" logic to script modules
sirreal 65fd1df
Make get_src return nullable
sirreal 843e1e0
Rework, rename, add tests
sirreal 2e180cf
Remove uniqueness checks
sirreal 7fc42d2
Replace excessively prescriptive comment
sirreal 5144a90
Add test for correct merge with regular dependencies
sirreal d83ea4b
Fix overwriting array keys
sirreal 9125683
Working proposal with caveats
sirreal 5ddc2dd
Revert preveious approach at module dependencies
sirreal 9e976bb
Inspect wp_scripts and extract module deps from scripts on the page
sirreal 591e3b5
Fix regression printing enqueued modules in importmap
sirreal d1d8c43
Check for WP_Scripts instance
sirreal 10c3a02
Fix possible unset module id in script modules
sirreal a444ffa
Fix lint
sirreal b0f579b
Update and improve tests with classic scripts
sirreal 8366f45
Improve variable name and array push of many values
sirreal 45cacbb
Remove a loop from importmap checking
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
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -208,10 +208,15 @@ public function add_hooks() { | |||||||||||||||||||||
*/ | ||||||||||||||||||||||
public function print_enqueued_script_modules() { | ||||||||||||||||||||||
foreach ( $this->get_marked_for_enqueue() as $id => $script_module ) { | ||||||||||||||||||||||
$src = $this->get_src( $id ); | ||||||||||||||||||||||
if ( null === $src ) { | ||||||||||||||||||||||
continue; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
wp_print_script_tag( | ||||||||||||||||||||||
array( | ||||||||||||||||||||||
'type' => 'module', | ||||||||||||||||||||||
'src' => $this->get_src( $id ), | ||||||||||||||||||||||
'src' => $src, | ||||||||||||||||||||||
'id' => $id . '-js-module', | ||||||||||||||||||||||
) | ||||||||||||||||||||||
); | ||||||||||||||||||||||
|
@@ -228,11 +233,16 @@ public function print_enqueued_script_modules() { | |||||||||||||||||||||
*/ | ||||||||||||||||||||||
public function print_script_module_preloads() { | ||||||||||||||||||||||
foreach ( $this->get_dependencies( array_keys( $this->get_marked_for_enqueue() ), array( 'static' ) ) as $id => $script_module ) { | ||||||||||||||||||||||
$src = $this->get_src( $id ); | ||||||||||||||||||||||
if ( null === $src ) { | ||||||||||||||||||||||
continue; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
// Don't preload if it's marked for enqueue. | ||||||||||||||||||||||
if ( true !== $script_module['enqueue'] ) { | ||||||||||||||||||||||
echo sprintf( | ||||||||||||||||||||||
'<link rel="modulepreload" href="%s" id="%s">', | ||||||||||||||||||||||
esc_url( $this->get_src( $id ) ), | ||||||||||||||||||||||
esc_url( $src ), | ||||||||||||||||||||||
esc_attr( $id . '-js-modulepreload' ) | ||||||||||||||||||||||
); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
@@ -262,14 +272,53 @@ public function print_import_map() { | |||||||||||||||||||||
* | ||||||||||||||||||||||
* @since 6.5.0 | ||||||||||||||||||||||
* | ||||||||||||||||||||||
* @return array Array with an `imports` key mapping to an array of script module identifiers and their respective | ||||||||||||||||||||||
* URLs, including the version query. | ||||||||||||||||||||||
* @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 { | ||||||||||||||||||||||
global $wp_scripts; | ||||||||||||||||||||||
|
||||||||||||||||||||||
$imports = array(); | ||||||||||||||||||||||
foreach ( $this->get_dependencies( array_keys( $this->get_marked_for_enqueue() ) ) as $id => $script_module ) { | ||||||||||||||||||||||
$imports[ $id ] = $this->get_src( $id ); | ||||||||||||||||||||||
|
||||||||||||||||||||||
$classic_script_dependencies = array(); | ||||||||||||||||||||||
if ( $wp_scripts instanceof WP_Scripts ) { | ||||||||||||||||||||||
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; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
Comment on lines
+291
to
+297
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. Is it sufficient to just check
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
$module_deps = $wp_scripts->get_data( $handle, 'module_deps' ); | ||||||||||||||||||||||
if ( ! $module_deps ) { | ||||||||||||||||||||||
continue; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
foreach ( $module_deps as $id ) { | ||||||||||||||||||||||
$src = $this->get_src( $id ); | ||||||||||||||||||||||
if ( null === $src ) { | ||||||||||||||||||||||
continue; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
$imports[ $id ] = $src; | ||||||||||||||||||||||
$classic_script_dependencies[] = $id; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
foreach ( $this->get_dependencies( array_merge( $classic_script_dependencies, array_keys( $this->get_marked_for_enqueue() ) ) ) as $id => $script_module ) { | ||||||||||||||||||||||
$src = $this->get_src( $id ); | ||||||||||||||||||||||
if ( null === $src ) { | ||||||||||||||||||||||
continue; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
$imports[ $id ] = $src; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
return array( 'imports' => $imports ); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
@@ -335,11 +384,11 @@ function ( $dependency_script_modules, $id ) use ( $import_types ) { | |||||||||||||||||||||
* @since 6.5.0 | ||||||||||||||||||||||
* | ||||||||||||||||||||||
* @param string $id The script module identifier. | ||||||||||||||||||||||
* @return string The script module src with a version if relevant. | ||||||||||||||||||||||
* @return string|null The script module src with a version if relevant. | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
private function get_src( string $id ): string { | ||||||||||||||||||||||
private function get_src( string $id ): ?string { | ||||||||||||||||||||||
if ( ! isset( $this->registered[ $id ] ) ) { | ||||||||||||||||||||||
return ''; | ||||||||||||||||||||||
return null; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
$script_module = $this->registered[ $id ]; | ||||||||||||||||||||||
|
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
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.
This is similar to what
wp_dependencies_unique_hosts
does:wordpress-develop/src/wp-includes/general-template.php
Lines 3724 to 3726 in ef76060
I'm not confident this is the most optimal way to get all classic scripts in the enqueued dependency graph so would appreciate scrutiny or domain knowledge on that.