[wp-trac] [WordPress Trac] #60647: Script Modules: Allow modules to depend on existing WordPress scripts

WordPress Trac noreply at wordpress.org
Fri Apr 5 12:24:46 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 youknowriad):

 We've discussed this issue a bit with @jonsurrell while no solutions
 emerged, I wanted to add some opinion here.

 First, I'm actually not convinced at all that we should allow modules to
 depend on scripts (or to use scripts for that matter). I'm not convinced
 that the use-cases are too big for existing scripts. I do see folks
 wanting to use apiFetch or internationalization (probably a different one
 than wp-i18n), date maybe but these are fairly small scripts.

 I think it's probably a mistake to try to use scripts in modules for
 different reasons:

  - A lot of these scripts are initialized server-side using inline scripts
 that are contextual. If I load wp-api-fetch in the editor or the
 dashboard, I won't get the same inline scripts. For modules, the opposite
 is true, the modules is always the same. This property of modules is a
 good thing in terms of architecture (separation between server and client)
 and making modules depend on scripts would be hard (which inline scripts
 to use) and create the issue of making the modules context dependent.
  - A lot of scripts have sub dependencies: polyfills, not certain all of
 this history is needed for the modules.
  - Add scripts as dependencies of modules, meaning that these scripts will
 always be printed in the page even if the module is not needed (will be
 async loaded later), which means the main selling point of modules becomes
 mute. We'll be shooting ourselves in the foot.

 For these reasons, I think it's not worth it to try to load scripts in
 modules. I think we'd better try having build scripts that creates a
 script and a module for some selected packages. And I can even see some
 differences between the script and the module version of something. For
 instance configuring the translations shouldn't be done in the same way to
 wp-i18n, we'd have to load contextual translations instead (lazy load them
 as needed rather than print them server side...)

 While this doesn't bring a solution, I think my best advice here would be
 to not try to solve this in a generic way, instead we should focus on
 concrete use-cases. I see more value personally (for a start) to be able
 to use modules within scripts (lazy load modules). For example a very
 concrete use-case I always bring is to be able to load a "codemirror"
 module in the editor for the code view for instance. I'd like editPost to
 be able to do `import( '@wordpress/codemirror' )` when needed.

 There might be other use-cases we can try with as well, but I don't think
 we offer a generic solution until we address specific use-cases. The
 solution can start as experimental/private APIs...

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/60647#comment:15>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list