-
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
Blocks: Allow reading the script handle from asset files #5118
Conversation
Yes, this looks like a good feature: instead of always auto-generating script handle for a block, like:
we can declare a custom name for the handle in the I suppose the use case would be as follows below. Please confirm whether I understand it correctly. There's a plugin that registers three blocks, and wants to bundle all their scripts together. The file layout is like:
All the three
The The
If we didn't have the custom handle in
and would be enqueued and loaded three times. I can confirm that this is compatible with the async block loading prototype. As long as the registered block specifies all the asset handles correctly, they can be used to create the import map and loaded dynamically in the browser. How to build such "block libraries" with webpack and
Then the task for the build script would be: "collect all blocks in One thing I don't like about the
in the source |
This is an excellent summary what type of problems I’m trying to attack. I must admit that I didn’t think about the obvious and so popular scenario described where all editor files for individual blocks get bundled and enqueued together in the plugin. However, it’s an even better example to illustrate the limitations of the current implementation of scripts handling during block registration. I was thinking more about the cases where multiple blocks need to enqueue the same script, or when instead of bundling all blocks together, the runtime feature is used as a way to share code together with some vendor libraries.
That would be very convenient to give more power to I’m not sure if in all cases bundling editor scripts together is the expected intent though, but it would align with how the plugins often structure JavaScript code for blocks in the editor. What benefits you though of when describing the idea of bundling together several It should be a different story when talking about the front end though as we should rather aim to split the JavaScript as much as possible to ensure that it is only enqueued when essential for the block to work.
That's a great point and I fully agree that it's confusing, counterintuitive and far from what I'd like to see. The challenge is that you need to explain the idea of crafting the |
4cfdf59
to
038ff22
Compare
Related discussion on WordPress Slack where shared scripts would resolve the existing issue:
There is a very similar issue reported with how |
038ff22
to
df35a2e
Compare
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. |
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. |
The reasoning makes sense. I wanted to mention that we could potentially be smarter and check whether multiple blocks refer to the same file even If the handle is not specified and just use the same generated or uuid handle as well. |
@jsnajdr The remark about the build changing the path from "source" to "built" is relevant but I'd also compare it to "package.json". In "package.json", you don't define a path the "source" file, you only define a path to the "built" files (main, module, exports), so while it's indeed a bit confusing, I think not targeting the source file can be a good thing because you don't really know how the "source" file is written: is it typescript, it it jsx, .... so unless it's normalized, it's not entirely clear why you should point to it from a declarative file like block.json. |
I will explore that further as a follow-up, as that would be indeed a nice addition. The biggest question for me is how much processing would be required to detect the same paths because scripts are stored with handles as keys. The way how to handle |
Committed with https://core.trac.wordpress.org/changeset/57590. |
Trac ticket: https://core.trac.wordpress.org/ticket/60485
In the documentation for WPDefinedAsset definition for block metadata there is a note about handle:
We never implemented it, but it would be very useful for block authors. One of the limitations of the way scripts get registered for blocks is that whenever a single file needs to be shared between blocks, then it has to be registered using
wp_register_script
separately, and the script handle used needs to be passed to one of the script fields inblock.json
:editorScript
,script
orviewScript
. It would be great if we could pass the predictable script handle in the.asset.php
file so you could still use local paths inblock.json
and the rest would get automatically handled by WordPress core. In effect, the logic proposed would read the script handle from the asset file and ensure that all other occurrences reuse the script that is already registered under the same name. It would open the possibility to have firs-class support for shared chunks or runtimes generated by bundlers like webpack.My initial goal was to find a way to enable runtime chunk for
@wordpress/scripts
in development mode and automatically register it in WordPress core to ensure that React Fast Refresh works correctly with more than one block as summarized in WordPress/gutenberg#25188 (comment).However, I figured out that we could also use the exact mechanism with any shared chunk that @jsnajdr made possible in the first place with WordPress/gutenberg#41002 by improving asset file generation in the webpack plugin that handles dependencies for WP core.
Note: This work, if we agree it's the good direction, is going to require follow-up changes (not a blocker for this patch) to the
@wordpress/dependency-extraction-webpack-plugin
, examplebuild/shared-script.js
:My initial thinking is that we can auto-generate the script handle based on the combination of the name of the webpack config or the project, plus the sanitized path of the entry file relative to the
build
folder:'my-plugin' + '-' + 'shared-script'
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.