Okay I found a quick and dirty solution for the viewScript.
My webpack.config.js:
/**
* `@wordpress/scripts` path-based name multi-block Webpack configuration.
* @see https://wordpress.stackexchange.com/questions/390282
*/
// Native Depedencies.
const path = require("path");
// Third-Party Dependencies.
const CopyPlugin = require("copy-webpack-plugin");
const config = require("@wordpress/scripts/config/webpack.config.js");
/**
* Resolve a series of path parts relative to `./src`.
* @param string[] path_parts An array of path parts.
* @returns string A normalized path, relative to `./src`.
**/
const resolveSource = (...path_parts) =>
path.resolve(process.cwd(), "src", ...path_parts);
/**
* Resolve a block name to the path to it's main `index.js` entry-point.
* @param string name The name of the block.
* @returns string A normalized path to the block's entry-point file.
**/
const resolveBlockEntry = (name) => resolveSource("blocks", name, "index.js");
/**
* Resolve a block name to the path to it's script `frontend.js`
* @param string name The name of the block.
* @returns string A normalized path to the block's viewScript file.
**/
const resolveBlockViewScript = (name) =>
resolveSource("blocks", name, "frontend.js");
config.entry = {
"admin/index": resolveSource("admin", "index.js"),
"blocks/accordion/index": resolveBlockEntry("accordion"),
"blocks/accordion-item/index": resolveBlockEntry("accordion-item"),
"blocks/accordion/frontend": resolveBlockViewScript("accordion"),
};
// Add a CopyPlugin to copy over block.json files.
config.plugins.push(
new CopyPlugin({
patterns: [
{
context: "src",
from: `blocks/*/block.json`,
},
],
})
);
module.exports = config;
The Output:
build
-- admin
- index.asset.php
- index.js
- style-index.css
-- blocks
-- accordion
- block.json
- frontend.asset.php
- frontend.js
- index.asset.php
- index.css
- index.js
-- accordion-item
- block.json
- index.asset.php
- index.css
- index.js
- style-index.css
This works for me. I am open for improvements, though, since this is an annoyance to maintain when adding new blocks.