Description
What problem does this address?
Part of #41236.
When registering a script via block.json, there is no way to specify a dependency. I understand that webpack uses a script to create index.asset.php with wordpress dependencies, but that doesn't work for other scripts. Our organization has some wordpress scripts which are core to our experience and relied on by other blocks/plugins. When I use block.json to register my block like "editorScript": [ "file:./index.js" ], , there's no way to declare those core scripts as dependencies. And doing it this way: "editorScript": [ "my-core-script", "file:./index.js" ], doesn't work either.
See this discussion also: #46589
What is your proposed solution?
Allow the existing WPDefinedAsset shape of input to be used along with the filepath method of registered a script. It might look like this:
"editorScript": [
{
"src": "file:./index.js",
"dependencies": [ "my-core-script" ],
"version": false,
}
],
// could also still use current method
"viewScript": [ "file:./index.j" ]
Activity
[-]I would like to be able to declare a script dependency in block.json's "editorScript" or "viewScript"[/-][+]I would like to be able to declare a script dependency when registering it in block.json'[/+][-]I would like to be able to declare a script dependency when registering it in block.json'[/-][+]I would like to be able to declare a script dependency when registering it in block.json[/+][-]I would like to be able to declare a script dependency when registering it in block.json[/-][+]Blocks: Allow customizations for assets in `block.json`[/+]gziolo commentedon Jan 30, 2023
@jakeparis, thank you for opening this proposal. Actually, it's something that has been on the Block API roadmap #41236 for quite some time, but it didn't have a related issue. We had a related issue #40447 that would get fixed as a consequence of implementing the syntax you outlined.
What's very interesting here is that nearly the same proposal was shared in https://core.trac.wordpress.org/ticket/54018#comment:5 by @ocean90. So that confirms that we were heading in the right direction to address the existing use cases.
lgladdy commentedon Dec 18, 2023
Big fan of this PR, ACF Blocks users often are confused by the requirement for an
.asset.phpfile to exist, especially as the documentation says "you may" but it will throw a_doing_it_wrongif it doesn't exist.gziolo commentedon Dec 20, 2023
I see a relevant issue open #57234. Let’s remove this limitation if it doesn’t work great with custom blocks.
lgladdy commentedon Dec 21, 2023
@gziolo Sounds good to me! I think with both these PRs merged we'll have covered off most use cases!
gziolo commentedon Dec 21, 2023
The other idea I shared in #57234 (comment), that instead of tweaking the format of
block.json, we could put a filter that allows modifying all the params that get passed towp_register_scriptwith the context of the block metadata available so plugins can apply all the necessary modifications using the full power of PHP and WP core.lgladdy commentedon Dec 21, 2023
Yeh, that new idea in #57234 (comment) would work too, and arguably is better than having the filter lower down the stack, so long as it prevents checking for the asset file completely if all the data is available at that point.
The thing I like about this PR is that it would allow you to build a simple block, regardless of type, without having to create one additional file (and indeed, one additional filesystem read - which can be performance-expensive on value hosting), whereas the filter would require additional code to make it work.
I think it's reasonable that if block.json contains the file path for the WPDefinedAsset, it should also be possible for the block.json to provide the attributes for that file.
colorful-tones commentedon Dec 21, 2023
I left some follow-up on #57234 (comment)
Overall, I think extending the
block.jsonto allow developers to customize asset definitions would be beneficial.Would this even be necessary or a hindrance to exploring extending the definitions in
block.json? If it is not entirely necessary then I would encourage us to consider logging a separate Issue and PR to explore whether developers would prefer this extra PHP way.I love this added performance consideration and impact, and great callout @lgladdy
gziolo commentedon Jan 3, 2024
I was thinking about it a bit more and I figured out that we could also combine two worlds together and optimize the way
wp-scripts buildwork today where you put for example in thesrcfolder the view script referenced in theblock.json:{ "viewScript": [ "file:./index.js" ] }Instead of creating the asset file on the disk, with the proposal implemented maybe we would be able to inline it in the
block.jsoncopied to thebuildfolder, so the stuff from above gets transformed to:{ "viewScript": [ { "file": "./index.js", "dependencies": [], "version": "abc123" } ] }For start, let's agree on the shape of the object passed to the
block.jsonfile. The asset file currently contains the following shape documented asWPDefinedAsset:In practice,
handlewas never implemented and integrated in WordPress core, but I have PR opened that would address it at least on the WP core side: WordPress/wordpress-develop#5118.Is it enough for start or should it be also possible to support other arguments that can be passed to the [
wp_register_script] call like:strategystring – Optional. If provided, may be either 'defer' or 'async'.in_footerbool – Optional. Whether to print the script in the footer. Default 'false'.colorful-tones commentedon Jan 3, 2024
This makes sense to me. This would be a modification to the
@wordpress/create-blockpackage only, correct? @gzioloI think
handle,dependencies,version,strategyandin_footerwould be a great start. 🥳jakeparis commentedon Jan 3, 2024
If at all possible, the other arguments available to
wp_register_script()should be available here as well, for the sake of consistency. So, I agree with the previous comment:handle,dependencies,version,strategyandin_footerwould be great.ArrayIterator commentedon May 29, 2024
I agreed!
On some case we don't need
xxxx.asset.phpagain!Because we have been declare about :
block.jsondeclare title icon etc.and then on editor (js) script, we also need a
registerType()with the name, icon, category etc.So? we should declare multiple files that should matches each others!
need 3 files for registering block via json:
block.json,asset.php&editor.js