-
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
base: trunk
Are you sure you want to change the base?
Allow classic scripts to depend on modules #8024
Conversation
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
if ( | ||
! $wp_scripts->query( $handle, 'done' ) && | ||
! $wp_scripts->query( $handle, 'to_do' ) && | ||
! $wp_scripts->query( $handle, 'enqueued' ) | ||
) { | ||
continue; | ||
} |
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.
Is it sufficient to just check enqueued
?
if ( | |
! $wp_scripts->query( $handle, 'done' ) && | |
! $wp_scripts->query( $handle, 'to_do' ) && | |
! $wp_scripts->query( $handle, 'enqueued' ) | |
) { | |
continue; | |
} | |
if ( ! $wp_scripts->query( $handle, 'enqueued' ) ) { | |
continue; | |
} |
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN:
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
@luisherranz @youknowriad @swissspidy @gziolo I'd love to have your review on this approach to allowing classic scripts to depend on script modules. |
if ( $wp_scripts instanceof WP_Scripts ) { | ||
foreach ( $wp_scripts->registered as $dependency ) { |
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
foreach ( array( $wp_scripts, $wp_styles ) as $dependencies ) { | |
if ( $dependencies instanceof WP_Dependencies && ! empty( $dependencies->queue ) ) { | |
foreach ( $dependencies->queue as $handle ) { |
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.
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.
The + operator merges arrays with key overwriting. array_merge renumbers numeric arrays, the desired behavior in this case.
This works, but it only works as long as scripts are printed before the importmap is printed. That is undesireable sometimes. An alternate approach may be to invert this scheme and have script modules inspect enqueued and printed scripts before printing the importmap.
7a6c553
to
45cacbb
Compare
This change proposes allowing a module dependencies to classic scripts.
Classic scripts can add module dependencies like
array( 'type' => 'module', 'id' => 'example-module-id' )
, where classic script dependencies are simple strings, e.g.'wp-i18n'
.In order for a classic script to have access to a script module, the script module must appear in the importmap so that it can be accessed by its module identifier from the classic script in a dynamic import.
For a classic script to depend on a module, it must declare that dependency and must be enqueued before the importmap is printed. When the script modules import map is printed, it inspects enqueued scripts to search for module dependencies and includes those dependencies in the import map. Import maps must be printed before any modulepreloads or any script module tags, and only one can be printed (although the specification has changed to allow multiple import maps).
The implementation overrides the
WP_Dependencies::add
method inWP_Scripts
.add
is is the main method that handles script registration.The subclassed implementation is used to partition the provided dependencies array into classic script dependencies and script module dependencies.
The classic script dependencies are passed on to
WP_Dependencies::add
along with the rest of the arguments. The module dependencies are attached tothe script as extra data
module_deps
.When the script modules importmap is printed, it scans through the registered classic scripts for enqueued scripts with
module_deps
and adds thosemodules to the import map.
Script modules in the import map will be available for classic scripts to use via dynamic
import()
.The dependencies look like this:
Builds on #8009.The implementation here is now independent.Trac ticket: https://core.trac.wordpress.org/ticket/61500
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.