[create-block] Updates variant handling for missing directory files #73986
+98
−28
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What?
Fixes external template variants not loading files in
@wordpress/create-blockWhen using an external npm template (like
@woocommerce/create-woo-extension) with the--variantflag, variant-specific template files were not being scaffolded, resulting in empty directories. This only occurred when pulling the templates from NPM, if you try to pull a template from your local machine (i.e.path/to/woocommerce/create-woo-extension), it doesn't have the same issue because the files are persistent on your machine.Note: this PR was drafted with the help of Claude Code.
Related: #70855
Why?
External templates are downloaded to a temporary directory, loaded via
require(), then the temp directory is immediately deleted. The bug occurred because:rimraf()This made it impossible to use variants with external templates, severely limiting the extensibility of
@wordpress/create-block.How?
The fix ensures all variant template files are loaded before the temporary directory is deleted:
configToTemplate()to eagerly process all variant template paths and return avariantTemplatesobject containing pre-loaded template contentTesting Instructions
Test 1: External template with custom variant
npx path/to/local/gutenberg/packages/create-block -t @woocommerce/create-woo-extension --variant=sql-modification test-variantcomposer.json,includes/Admin/Setup.php,tests/Test.php, etc.)src/directory contains variant-specific JS filesBefore: Directory would be created but most files missing, variant directories empty
After: All 14 plugin template files and 2 block template files created correctly
Test 2: External template with default variant
npx path/to/local/gutenberg/packages/create-block -t @woocommerce/create-woo-extension test-defaultTest 3: Built-in template variants (regression test)
npx path/to/local/gutenberg/packages/create-block -t es5 --variant=dynamic test-builtinrender.phpTesting Instructions for Keyboard
Not applicable - this is a CLI tool with no UI changes.
Screenshots or screencast
Before:
After: