[wp-trac] [WordPress Trac] #60647: Script Modules: Allow modules to depend on existing WordPress scripts
WordPress Trac
noreply at wordpress.org
Wed Apr 3 11:26:37 UTC 2024
#60647: Script Modules: Allow modules to depend on existing WordPress scripts
-----------------------------+-------------------------
Reporter: jonsurrell | Owner: jonsurrell
Type: feature request | Status: assigned
Priority: normal | Milestone: 6.6
Component: Script Loader | Version: 6.5
Severity: normal | Resolution:
Keywords: has-patch | Focuses: javascript
-----------------------------+-------------------------
Comment (by jonsurrell):
There's an additional difficulty with some scripts when trying to use them
as modules. [https://github.com/WordPress/wordpress-
develop/blob/6bb85810cc0899b90d4f9da224b9437160145159/src/wp-includes
/script-loader.php#L345-L509 Some scripts depend on special initialization
as inline scripts]. There's no obvious way to do this with modules without
sacrificing some of the benefits of modules.
For example, `wp-api-fetch` script is initialized with an inline script
like this:
{{{#!javascript
wp.apiFetch.nonceMiddleware = wp.apiFetch.createNonceMiddleware( nonce );
// …more setup code
}}}
Where `nonce` is a server generated nonce for the REST API. This can be
achieved in the same way with modules by using `script type=module` tag
and importing `@wordpress/api-fetch`, but the module will be necessarily
required which is undesirable. Ideally the module is only downloaded and
initialized if and when it's needed.
A better solution may be to modify `wp-api-fetch` so that rather than the
server injecting imperative code to set it up, instead it searches for
some global variables and performs its own setup/initialization when
necessary.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/60647#comment:11>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list