How to add an additional dependency to a block index.asset.php file

Actually, you do not need to worry about adding the Masonry’s script handle to the dependencies array in the asset file (index.asset.php).

All you needed to do to make your Masonry code work properly is by using the script property to add the masonry (and imagesloaded) as dependencies for your editor and view/front-end scripts.

"editorScript": "file:./index.js",
"script": [
    "imagesloaded",
    "masonry"
],
"viewScript": "file:./view.js",

I don’t know and haven’t checked further why adding masonry to the editorScript and viewScript did not work, but the above (trick) worked well for me.


For completeness though, I created a simple Webpack plugin which basically modifies the asset source before it is written to the asset file, but (hopefully) after the Dependency Extraction Webpack Plugin added the asset to the compilation queue.

Here’s the plugin which I placed it in the root project folder.

And here’s my custom Webpack config file, i.e. webpack.config.js.

BTW, thanks for sharing about the Gutenberg Test Iframed Masonry Block. 🙂 Happy coding!


Update 21 Feb 2024

Regarding the strikethrough text above:

tech