-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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: Add support for external templates hosted on npm #22175
Changes from 15 commits
e324e8e
48c7ee1
14611a3
3714b20
b0b617f
e81dd27
db93598
4b55cdf
62811e3
11072bf
7036078
4b0b246
86f7327
083a990
ea9dc1b
5be44cc
22cdbb3
fac8b18
0a7169e
06a3051
574f212
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,7 @@ $ npm init @wordpress/block [options] [slug] | |
Options: | ||
``` | ||
-V, --version output the version number | ||
-t, --template <name> template type name, allowed values: "es5", "esnext" (default: "esnext") | ||
-t, --template <name> template name: "es5", "esnext" or the name of an external npm package (default: "esnext") | ||
--namespace <value> internal namespace for the block name | ||
--title <value> display title for the block | ||
--short-description <value> short description for the block | ||
|
@@ -65,9 +65,9 @@ More examples: | |
|
||
When you scaffold a block, you must provide at least a `slug` name, the `namespace` which usually corresponds to either the `theme` or `plugin` name, and the `category`. In most cases, we recommended pairing blocks with plugins rather than themes, because only using plugin ensures that all blocks still work when your theme changes. | ||
|
||
## Available Commands | ||
## Available Commands [ESNext template] | ||
|
||
Inside that bootstrapped directory _(it doesn't apply to `es5` template)_, you can run several commands: | ||
When bootstraped with the `esnext` templateyou can run several commands inside the directory: | ||
fabiankaegy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```bash | ||
$ npm start | ||
|
@@ -99,6 +99,53 @@ $ npm run packages-update | |
``` | ||
Updates WordPress packages to the latest version. [Learn more](/packages/scripts#packages-update). | ||
|
||
## External Templates | ||
Since version 0.12.0 it is possible to use external templates hosted on NPM. These templates need to contain `.mustache` files that will be used in the scaffolding and one `template.json` for the metadata. | ||
|
||
### Availabe Variables: | ||
fabiankaegy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- `namespace` | ||
- `slug` | ||
- `title` | ||
- `textdomain` | ||
- `description` | ||
- `category` | ||
- `dashicon` | ||
- `license` | ||
- `licenseURI` | ||
- `namespaceSnakeCase` | ||
- `slugSnakeCase` | ||
Comment on lines
+115
to
+116
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
### `template.json` | ||
```json | ||
{ | ||
"defaultValues": { | ||
"namespace": "create-block", | ||
"slug": "esnext-example", | ||
"title": "ESNext Example", | ||
"description": | ||
"Example block written with ESNext standard and JSX support – build step required.", | ||
"dashicon": "smiley", | ||
"category": "widgets", | ||
"author": "The WordPress Contributors", | ||
"license": "GPL-2.0-or-later", | ||
"licenseURI": "https://www.gnu.org/licenses/gpl-2.0.html", | ||
"version": "0.1.0" | ||
}, | ||
"outputFiles": [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the future PR: I'm very interested to find a way to automate the discovery of templates with {
"templatesFolder": "./my-folder"
} |
||
".editorconfig", | ||
".gitignore", | ||
"editor.css", | ||
"src/edit.js", | ||
"src/index.js", | ||
"src/save.js", | ||
"$slug.php", | ||
"style.css", | ||
"readme.txt" | ||
], | ||
"wpScriptsEnabled": true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the future PR: Thinking about it now when documented, it should be opt-out rather than opt-in, we want people to use |
||
} | ||
``` | ||
|
||
## WP-CLI | ||
|
||
Another way of making a developer’s life easier is to use [WP-CLI](https://wp-cli.org), which provides a command-line interface for many actions you might perform on the WordPress instance. One of the commands `wp scaffold block` was used as the baseline for this tool and ES5 template in particular. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,7 @@ program | |
) => { | ||
await checkSystemRequirements( engines ); | ||
try { | ||
const defaultValues = getDefaultValues( template ); | ||
const defaultValues = await getDefaultValues( template ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fact that all methods need to be converted to async here is a bit concerning. I'm coming to the conclusion that we should call |
||
const optionsValues = pickBy( { | ||
category, | ||
description: shortDescription, | ||
|
@@ -62,11 +62,13 @@ program | |
}; | ||
await scaffold( template, answers ); | ||
} else { | ||
const propmpts = getPrompts( template ).filter( | ||
const propmpts = await getPrompts( template ); | ||
fabiankaegy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const filteredPrompts = propmpts.filter( | ||
( { name } ) => | ||
! Object.keys( optionsValues ).includes( name ) | ||
); | ||
const answers = await inquirer.prompt( propmpts ); | ||
const answers = await inquirer.prompt( filteredPrompts ); | ||
|
||
await scaffold( template, { | ||
...defaultValues, | ||
...optionsValues, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more thing,
wpScriptsEnabled
enables those commands. Once we sort out the final shape of template definition we can update this section to reflect it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gziolo Yes the commands get enabled by
wpScriptsEnabled
but at the end it matters what the creator of the template has in theirscripts
in thepackage.json
.Thats why I changed it to only refer to the
esnext
template.