Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Create Block] Introduce the transformer property. #55423

Merged
merged 5 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/create-block/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### New Feature

- Add new `transformer` property to external templates to allow customization of any values being passed from cli or the template.[#55423](https://github.com/WordPress/gutenberg/pull/55423)

## 4.27.0 (2023-10-05)

## 4.26.0 (2023-09-20)
Expand Down
55 changes: 31 additions & 24 deletions packages/create-block/lib/scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,52 +49,31 @@ module.exports = async (
customPackageJSON,
customBlockJSON,
example,
transformer,
}
) => {
slug = slug.toLowerCase();
namespace = namespace.toLowerCase();
/**
* --no-plugin relies on the used template supporting the [blockTemplatesPath property](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-create-block/#blocktemplatespath).
* If the blockOutputTemplates object has no properties, we can assume that there was a custom --template passed that
* doesn't support it.
*/
if ( ! plugin && Object.keys( blockOutputTemplates ) < 1 ) {
error(
'No block files found in the template. Please ensure that the template supports the blockTemplatesPath property.'
);
return;
}

info( '' );
info(
plugin
? `Creating a new WordPress plugin in the ${ slug } directory.`
: `Creating a new block in the ${ slug } directory.`
);

const view = {
const transformedValues = transformer( {
$schema,
apiVersion,
plugin,
namespace,
namespaceSnakeCase: snakeCase( namespace ),
slug,
slugSnakeCase: snakeCase( slug ),
slugPascalCase: pascalCase( slug ),
title,
description,
dashicon,
category,
attributes,
supports,
version,
author,
pluginURI,
license,
licenseURI,
textdomain: slug,
domainPath,
updateURI,
version,
wpScripts,
wpEnv,
npmDependencies,
Expand All @@ -106,12 +85,40 @@ module.exports = async (
style,
render,
viewScript,
variantVars,
customPackageJSON,
customBlockJSON,
example,
textdomain: slug,
} );

const view = {
...transformedValues,
namespaceSnakeCase: snakeCase( transformedValues.slug ),
slugSnakeCase: snakeCase( transformedValues.slug ),
slugPascalCase: pascalCase( transformedValues.slug ),
...variantVars,
};

/**
* --no-plugin relies on the used template supporting the [blockTemplatesPath property](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-create-block/#blocktemplatespath).
* If the blockOutputTemplates object has no properties, we can assume that there was a custom --template passed that
* doesn't support it.
*/
if ( ! plugin && Object.keys( blockOutputTemplates ) < 1 ) {
error(
'No block files found in the template. Please ensure that the template supports the blockTemplatesPath property.'
);
return;
}

info( '' );
info(
plugin
? `Creating a new WordPress plugin in the ${ view.slug } directory.`
: `Creating a new block in the ${ view.slug } directory.`
);

if ( plugin ) {
await Promise.all(
Object.keys( pluginOutputTemplates ).map(
Expand Down
1 change: 1 addition & 0 deletions packages/create-block/lib/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ const getDefaultValues = ( pluginTemplate, variant ) => {
editorScript: 'file:./index.js',
editorStyle: 'file:./index.css',
style: 'file:./style-index.css',
transformer: ( view ) => view,
...pluginTemplate.defaultValues,
...pluginTemplate.variants?.[ variant ],
variantVars: getVariantVars( pluginTemplate.variants, variant ),
Expand Down
Loading