From 2f28969e4b2547b5441a250bbce9c7e8aa34198f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20Zi=C3=B3=C5=82kowski?= Date: Fri, 20 Dec 2024 11:13:54 +0100 Subject: [PATCH 1/3] Create Block: Optimize the default template for multiple blocks case --- 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 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/create-block/CHANGELOG.md b/packages/create-block/CHANGELOG.md index e109e36ccbd798..bec05feb86dfbc 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. ### Internal diff --git a/packages/create-block/lib/scaffold.js b/packages/create-block/lib/scaffold.js index bc7cb3b8bfcd32..5ec92948c71106 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 db78ee80aa429a..8de9899e2fd51a 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 75666af3a850b2..3fb731f6c5babf 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' ); From e53dc3bc7422a5af9f1519f153ff7b282454207f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20Zi=C3=B3=C5=82kowski?= Date: Fri, 20 Dec 2024 11:15:11 +0100 Subject: [PATCH 2/3] Add changelog entry --- packages/create-block/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/create-block/CHANGELOG.md b/packages/create-block/CHANGELOG.md index bec05feb86dfbc..f08fbc15f4e618 100644 --- a/packages/create-block/CHANGELOG.md +++ b/packages/create-block/CHANGELOG.md @@ -5,7 +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. +- 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 From a499ab204ef710aeab26c0d409bdab2d24b7a1ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20Zi=C3=B3=C5=82kowski?= Date: Fri, 20 Dec 2024 11:22:44 +0100 Subject: [PATCH 3/3] Update the integration test to reflect changes --- bin/test-create-block.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/test-create-block.sh b/bin/test-create-block.sh index 99b7e8e6082604..7df3b214af042d 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