From ec0317e9a9543e8b0afc04d29a178d84fc2ba79d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=B3=C5=82kowski?= Date: Thu, 2 Jan 2025 12:59:00 +0100 Subject: [PATCH] Create Block: Optimize the default template for multiple blocks case (#68175) * Create Block: Optimize the default template for multiple blocks case * Add changelog entry * Update the integration test to reflect changes Co-authored-by: gziolo Co-authored-by: sirreal --- bin/test-create-block.sh | 4 ++-- packages/create-block/CHANGELOG.md | 1 + packages/create-block/lib/scaffold.js | 5 ++--- packages/create-block/lib/templates.js | 1 + .../create-block/lib/templates/plugin/$slug.php.mustache | 2 +- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/bin/test-create-block.sh b/bin/test-create-block.sh index 99b7e8e608260..7df3b214af042 100755 --- a/bin/test-create-block.sh +++ b/bin/test-create-block.sh @@ -56,7 +56,7 @@ if [ "$expected" -ne "$actual" ]; then exit 1 fi expected=7 -actual=$( find src -maxdepth 1 -type f | wc -l ) +actual=$( find src -maxdepth 2 -type f | wc -l ) if [ "$expected" -ne "$actual" ]; then error "Expected $expected files in the \`src\` directory, but found $actual." exit 1 @@ -70,7 +70,7 @@ status "Building block..." status "Verifying build..." expected=9 -actual=$( find build -maxdepth 1 -type f | wc -l ) +actual=$( find build -maxdepth 2 -type f | wc -l ) if [ "$expected" -ne "$actual" ]; then error "Expected $expected files in the \`build\` directory, but found $actual." exit 1 diff --git a/packages/create-block/CHANGELOG.md b/packages/create-block/CHANGELOG.md index e109e36ccbd79..f08fbc15f4e61 100644 --- a/packages/create-block/CHANGELOG.md +++ b/packages/create-block/CHANGELOG.md @@ -5,6 +5,7 @@ ### Enhancement - Add support for custom `textdomain` property for the scaffolded block ([#57197](https://github.com/WordPress/gutenberg/pull/57197)). +- Update the default template to scaffold a block in its subfolder to make it easier to update to multiple blocks in a single plugin ([#68175](https://github.com/WordPress/gutenberg/pull/68175)). ### Internal diff --git a/packages/create-block/lib/scaffold.js b/packages/create-block/lib/scaffold.js index bc7cb3b8bfcd3..5ec92948c7110 100644 --- a/packages/create-block/lib/scaffold.js +++ b/packages/create-block/lib/scaffold.js @@ -58,13 +58,12 @@ module.exports = async ( } ) => { slug = slug.toLowerCase(); - namespace = namespace.toLowerCase(); const rootDirectory = join( process.cwd(), targetDir || slug ); const transformedValues = transformer( { $schema, apiVersion, plugin, - namespace, + namespace: namespace.toLowerCase(), slug, title, description, @@ -84,7 +83,7 @@ module.exports = async ( npmDependencies, npmDevDependencies, customScripts, - folderName, + folderName: folderName.replace( /\$slug/g, slug ), editorScript, editorStyle, style, diff --git a/packages/create-block/lib/templates.js b/packages/create-block/lib/templates.js index db78ee80aa429..8de9899e2fd51 100644 --- a/packages/create-block/lib/templates.js +++ b/packages/create-block/lib/templates.js @@ -59,6 +59,7 @@ const predefinedPluginTemplates = { }, viewScript: 'file:./view.js', example: {}, + folderName: './src/$slug', }, variants: { static: {}, diff --git a/packages/create-block/lib/templates/plugin/$slug.php.mustache b/packages/create-block/lib/templates/plugin/$slug.php.mustache index 75666af3a850b..3fb731f6c5bab 100644 --- a/packages/create-block/lib/templates/plugin/$slug.php.mustache +++ b/packages/create-block/lib/templates/plugin/$slug.php.mustache @@ -42,6 +42,6 @@ if ( ! defined( 'ABSPATH' ) ) { * @see https://developer.wordpress.org/reference/functions/register_block_type/ */ function {{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init() { - register_block_type( __DIR__ . '/build' ); + register_block_type( __DIR__ . '/build/{{slug}}' ); } add_action( 'init', '{{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init' );