@wordpress/create-block 3.6.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/README.md +17 -2
- package/lib/check-system-requirements.js +17 -13
- package/lib/index.js +97 -44
- package/lib/init-block.js +18 -15
- package/lib/init-package-json.js +10 -7
- package/lib/output.js +8 -6
- package/lib/prompts.js +9 -4
- package/lib/scaffold.js +55 -29
- package/lib/templates/block/edit.js.mustache +3 -3
- package/lib/templates/block/index.js.mustache +5 -0
- package/lib/templates/block/save.js.mustache +5 -13
- package/lib/templates/block/template.php.mustache +5 -0
- package/lib/templates/es5/$slug.php.mustache +20 -0
- package/lib/templates/es5/index.js.mustache +7 -5
- package/lib/templates/es5/template.php.mustache +5 -0
- package/lib/templates/plugin/$slug.php.mustache +27 -0
- package/lib/templates.js +47 -13
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,17 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## 4.0.0 (2022-08-24)
|
|
6
|
+
|
|
7
|
+
### Breaking Change
|
|
8
|
+
|
|
9
|
+
- Increase the minimum Node.js version to 14 and minimum npm version to 6.14.4 ([#43141](https://github.com/WordPress/gutenberg/pull/43141)).
|
|
10
|
+
|
|
11
|
+
### New Feature
|
|
12
|
+
|
|
13
|
+
- Add `--no-plugin` flag to allow scaffolding of a block in an existing plugin ([#41642](https://github.com/WordPress/gutenberg/pull/41642))
|
|
14
|
+
- Introduce the `--variant` flag to allow selection of a variant as defined in the template ([#41289](https://github.com/WordPress/gutenberg/pull/41289), [#43481](https://github.com/WordPress/gutenberg/pull/43481)).
|
|
15
|
+
|
|
5
16
|
## 3.6.0 (2022-07-13)
|
|
6
17
|
|
|
7
18
|
### Enhancement
|
package/README.md
CHANGED
|
@@ -20,7 +20,7 @@ $ cd todo-list
|
|
|
20
20
|
$ npm start
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
-
_(requires `node` version `
|
|
23
|
+
_(requires `node` version `14.0.0` or above, and `npm` version `6.14.4` or above)_
|
|
24
24
|
|
|
25
25
|
It creates a WordPress plugin that you need to [install manually](https://wordpress.org/support/article/managing-plugins/#manual-plugin-installation).
|
|
26
26
|
|
|
@@ -41,6 +41,7 @@ Options:
|
|
|
41
41
|
```bash
|
|
42
42
|
-V, --version output the version number
|
|
43
43
|
-t, --template <name> project template type name; allowed values: "static" (default), "es5", the name of an external npm package, or the path to a local directory
|
|
44
|
+
--no-plugin scaffold block files only
|
|
44
45
|
--namespace <value> internal namespace for the block name
|
|
45
46
|
--title <value> display title for the block and the WordPress plugin
|
|
46
47
|
--short-description <value> short description for the block and the WordPress plugin
|
|
@@ -49,6 +50,7 @@ Options:
|
|
|
49
50
|
--no-wp-scripts disable integration with `@wordpress/scripts` package
|
|
50
51
|
--wp-env enable integration with `@wordpress/env` package
|
|
51
52
|
-h, --help output usage information
|
|
53
|
+
--variant choose a block variant as defined by the template
|
|
52
54
|
```
|
|
53
55
|
|
|
54
56
|
More examples:
|
|
@@ -71,12 +73,25 @@ $ npx @wordpress/create-block --template my-template-package
|
|
|
71
73
|
$ npx @wordpress/create-block --template ./path/to/template-directory
|
|
72
74
|
```
|
|
73
75
|
|
|
74
|
-
4.
|
|
76
|
+
4. Generating a dynamic block based on the built-in template.
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
$ npx @wordpress/create-block --variant dynamic
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
5. Help – you need to use `npx` to output usage information.
|
|
75
83
|
|
|
76
84
|
```bash
|
|
77
85
|
$ npx @wordpress/create-block --help
|
|
78
86
|
```
|
|
79
87
|
|
|
88
|
+
5. No plugin mode – it is also possible to scaffold only block files into the current directory.
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
$ npx @wordpress/create-block --no-plugin
|
|
93
|
+
```
|
|
94
|
+
|
|
80
95
|
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. In most cases, we recommended pairing blocks with WordPress plugins rather than themes, because only using plugin ensures that all blocks still work when your theme changes.
|
|
81
96
|
|
|
82
97
|
## Available Commands
|
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
const inquirer = require( 'inquirer' );
|
|
5
5
|
const checkSync = require( 'check-node-version' );
|
|
6
6
|
const tools = require( 'check-node-version/tools' );
|
|
7
|
-
const { forEach } = require( 'lodash' );
|
|
8
7
|
const { promisify } = require( 'util' );
|
|
9
8
|
|
|
10
9
|
/**
|
|
@@ -21,13 +20,15 @@ async function checkSystemRequirements( engines ) {
|
|
|
21
20
|
log.error( 'Minimum system requirements not met!' );
|
|
22
21
|
log.info( '' );
|
|
23
22
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
Object.entries( result.versions ).forEach(
|
|
24
|
+
( [ name, { isSatisfied, wanted } ] ) => {
|
|
25
|
+
if ( ! isSatisfied ) {
|
|
26
|
+
log.error(
|
|
27
|
+
`Error: Wanted ${ name } version ${ wanted.raw } (${ wanted.range })`
|
|
28
|
+
);
|
|
29
|
+
}
|
|
29
30
|
}
|
|
30
|
-
|
|
31
|
+
);
|
|
31
32
|
|
|
32
33
|
log.info( '' );
|
|
33
34
|
log.error( 'The program may not complete correctly if you continue.' );
|
|
@@ -46,13 +47,16 @@ async function checkSystemRequirements( engines ) {
|
|
|
46
47
|
log.error( 'Cancelled.' );
|
|
47
48
|
log.info( '' );
|
|
48
49
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
50
|
+
Object.entries( result.versions ).forEach(
|
|
51
|
+
( [ name, { isSatisfied, wanted } ] ) => {
|
|
52
|
+
if ( ! isSatisfied ) {
|
|
53
|
+
log.info(
|
|
54
|
+
tools[ name ].getInstallInstructions( wanted.raw )
|
|
55
|
+
);
|
|
56
|
+
}
|
|
54
57
|
}
|
|
55
|
-
|
|
58
|
+
);
|
|
59
|
+
|
|
56
60
|
process.exit( 1 );
|
|
57
61
|
}
|
|
58
62
|
}
|
package/lib/index.js
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
* External dependencies
|
|
3
3
|
*/
|
|
4
4
|
const inquirer = require( 'inquirer' );
|
|
5
|
+
const { capitalCase } = require( 'change-case' );
|
|
5
6
|
const program = require( 'commander' );
|
|
6
|
-
const { pickBy, startCase } = require( 'lodash' );
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Internal dependencies
|
|
@@ -34,8 +34,8 @@ program
|
|
|
34
34
|
.arguments( '[slug]' )
|
|
35
35
|
.option(
|
|
36
36
|
'-t, --template <name>',
|
|
37
|
-
'project template type name; allowed values: "
|
|
38
|
-
'
|
|
37
|
+
'project template type name; allowed values: "standard", "es5", the name of an external npm package, or the path to a local directory',
|
|
38
|
+
'standard'
|
|
39
39
|
)
|
|
40
40
|
.option( '--namespace <value>', 'internal namespace for the block name' )
|
|
41
41
|
.option(
|
|
@@ -57,10 +57,13 @@ program
|
|
|
57
57
|
'disable integration with `@wordpress/scripts` package'
|
|
58
58
|
)
|
|
59
59
|
.option( '--wp-env', 'enable integration with `@wordpress/env` package' )
|
|
60
|
+
.option( '--no-plugin', 'scaffold only block files' )
|
|
61
|
+
.option( '--variant <variant>', 'the variant of the template to use' )
|
|
60
62
|
.action(
|
|
61
63
|
async (
|
|
62
64
|
slug,
|
|
63
65
|
{
|
|
66
|
+
plugin,
|
|
64
67
|
category,
|
|
65
68
|
namespace,
|
|
66
69
|
shortDescription: description,
|
|
@@ -68,81 +71,131 @@ program
|
|
|
68
71
|
title,
|
|
69
72
|
wpScripts,
|
|
70
73
|
wpEnv,
|
|
74
|
+
variant,
|
|
71
75
|
}
|
|
72
76
|
) => {
|
|
73
77
|
await checkSystemRequirements( engines );
|
|
74
78
|
try {
|
|
75
79
|
const pluginTemplate = await getPluginTemplate( templateName );
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
80
|
+
const availableVariants = Object.keys(
|
|
81
|
+
pluginTemplate.variants
|
|
82
|
+
);
|
|
83
|
+
if ( variant && ! availableVariants.includes( variant ) ) {
|
|
84
|
+
if ( ! availableVariants.length ) {
|
|
85
|
+
throw new CLIError(
|
|
86
|
+
`"${ variant }" variant was selected. This template does not have any variants!`
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
throw new CLIError(
|
|
90
|
+
`"${ variant }" is not a valid variant for this template. Available variants are: ${ availableVariants.join(
|
|
91
|
+
', '
|
|
92
|
+
) }.`
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const optionsValues = Object.fromEntries(
|
|
97
|
+
Object.entries( {
|
|
98
|
+
plugin,
|
|
79
99
|
category,
|
|
80
100
|
description,
|
|
81
101
|
namespace,
|
|
82
102
|
title,
|
|
83
103
|
wpScripts,
|
|
84
104
|
wpEnv,
|
|
85
|
-
},
|
|
86
|
-
( value ) => value !== undefined
|
|
105
|
+
} ).filter( ( [ , value ] ) => value !== undefined )
|
|
87
106
|
);
|
|
88
107
|
|
|
89
108
|
if ( slug ) {
|
|
109
|
+
const defaultValues = getDefaultValues(
|
|
110
|
+
pluginTemplate,
|
|
111
|
+
variant
|
|
112
|
+
);
|
|
90
113
|
const answers = {
|
|
91
114
|
...defaultValues,
|
|
92
115
|
slug,
|
|
93
116
|
// Transforms slug to title as a fallback.
|
|
94
|
-
title:
|
|
117
|
+
title: capitalCase( slug ),
|
|
95
118
|
...optionsValues,
|
|
96
119
|
};
|
|
97
120
|
await scaffold( pluginTemplate, answers );
|
|
98
121
|
} else {
|
|
99
122
|
log.info( '' );
|
|
100
123
|
log.info(
|
|
101
|
-
|
|
124
|
+
plugin
|
|
125
|
+
? "Let's customize your WordPress plugin with blocks:"
|
|
126
|
+
: "Let's add a new block to your existing WordPress plugin:"
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
if ( ! variant && availableVariants.length > 1 ) {
|
|
130
|
+
const result = await inquirer.prompt( {
|
|
131
|
+
type: 'list',
|
|
132
|
+
name: 'variant',
|
|
133
|
+
message:
|
|
134
|
+
'The template variant to use for this block:',
|
|
135
|
+
choices: availableVariants,
|
|
136
|
+
} );
|
|
137
|
+
variant = result.variant;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const defaultValues = getDefaultValues(
|
|
141
|
+
pluginTemplate,
|
|
142
|
+
variant
|
|
102
143
|
);
|
|
103
144
|
|
|
104
145
|
const filterOptionsProvided = ( { name } ) =>
|
|
105
146
|
! Object.keys( optionsValues ).includes( name );
|
|
106
|
-
const blockPrompts = getPrompts(
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
147
|
+
const blockPrompts = getPrompts(
|
|
148
|
+
pluginTemplate,
|
|
149
|
+
[
|
|
150
|
+
'slug',
|
|
151
|
+
'namespace',
|
|
152
|
+
'title',
|
|
153
|
+
'description',
|
|
154
|
+
'dashicon',
|
|
155
|
+
'category',
|
|
156
|
+
],
|
|
157
|
+
variant
|
|
158
|
+
).filter( filterOptionsProvided );
|
|
114
159
|
const blockAnswers = await inquirer.prompt( blockPrompts );
|
|
115
160
|
|
|
116
|
-
const pluginAnswers =
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
161
|
+
const pluginAnswers = plugin
|
|
162
|
+
? await inquirer
|
|
163
|
+
.prompt( {
|
|
164
|
+
type: 'confirm',
|
|
165
|
+
name: 'configurePlugin',
|
|
166
|
+
message:
|
|
167
|
+
'Do you want to customize the WordPress plugin?',
|
|
168
|
+
default: false,
|
|
169
|
+
} )
|
|
170
|
+
.then( async ( { configurePlugin } ) => {
|
|
171
|
+
if ( ! configurePlugin ) {
|
|
172
|
+
return {};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const pluginPrompts = getPrompts(
|
|
176
|
+
pluginTemplate,
|
|
177
|
+
[
|
|
178
|
+
'pluginURI',
|
|
179
|
+
'version',
|
|
180
|
+
'author',
|
|
181
|
+
'license',
|
|
182
|
+
'licenseURI',
|
|
183
|
+
'domainPath',
|
|
184
|
+
'updateURI',
|
|
185
|
+
],
|
|
186
|
+
variant
|
|
187
|
+
).filter( filterOptionsProvided );
|
|
188
|
+
const result = await inquirer.prompt(
|
|
189
|
+
pluginPrompts
|
|
190
|
+
);
|
|
191
|
+
return result;
|
|
192
|
+
} )
|
|
193
|
+
: {};
|
|
128
194
|
|
|
129
|
-
const pluginPrompts = getPrompts( pluginTemplate, [
|
|
130
|
-
'pluginURI',
|
|
131
|
-
'version',
|
|
132
|
-
'author',
|
|
133
|
-
'license',
|
|
134
|
-
'licenseURI',
|
|
135
|
-
'domainPath',
|
|
136
|
-
'updateURI',
|
|
137
|
-
] ).filter( filterOptionsProvided );
|
|
138
|
-
const result = await inquirer.prompt(
|
|
139
|
-
pluginPrompts
|
|
140
|
-
);
|
|
141
|
-
return result;
|
|
142
|
-
} );
|
|
143
195
|
await scaffold( pluginTemplate, {
|
|
144
196
|
...defaultValues,
|
|
145
197
|
...optionsValues,
|
|
198
|
+
variant,
|
|
146
199
|
...blockAnswers,
|
|
147
200
|
...pluginAnswers,
|
|
148
201
|
} );
|
package/lib/init-block.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* External dependencies
|
|
3
3
|
*/
|
|
4
|
-
const { omitBy } = require( 'lodash' );
|
|
5
4
|
const { dirname, join } = require( 'path' );
|
|
6
5
|
const makeDir = require( 'make-dir' );
|
|
7
6
|
const { writeFile } = require( 'fs' ).promises;
|
|
@@ -15,6 +14,7 @@ const { writeOutputTemplate } = require( './output' );
|
|
|
15
14
|
async function initBlockJSON( {
|
|
16
15
|
$schema,
|
|
17
16
|
apiVersion,
|
|
17
|
+
plugin,
|
|
18
18
|
slug,
|
|
19
19
|
namespace,
|
|
20
20
|
title,
|
|
@@ -33,13 +33,15 @@ async function initBlockJSON( {
|
|
|
33
33
|
info( '' );
|
|
34
34
|
info( 'Creating a "block.json" file.' );
|
|
35
35
|
|
|
36
|
-
const outputFile =
|
|
36
|
+
const outputFile = plugin
|
|
37
|
+
? join( process.cwd(), slug, folderName, 'block.json' )
|
|
38
|
+
: join( process.cwd(), slug, 'block.json' );
|
|
37
39
|
await makeDir( dirname( outputFile ) );
|
|
38
40
|
await writeFile(
|
|
39
41
|
outputFile,
|
|
40
42
|
JSON.stringify(
|
|
41
|
-
|
|
42
|
-
{
|
|
43
|
+
Object.fromEntries(
|
|
44
|
+
Object.entries( {
|
|
43
45
|
$schema,
|
|
44
46
|
apiVersion,
|
|
45
47
|
name: namespace + '/' + slug,
|
|
@@ -54,8 +56,7 @@ async function initBlockJSON( {
|
|
|
54
56
|
editorScript,
|
|
55
57
|
editorStyle,
|
|
56
58
|
style,
|
|
57
|
-
},
|
|
58
|
-
( value ) => ! value
|
|
59
|
+
} ).filter( ( [ , value ] ) => !! value )
|
|
59
60
|
),
|
|
60
61
|
null,
|
|
61
62
|
'\t'
|
|
@@ -65,15 +66,17 @@ async function initBlockJSON( {
|
|
|
65
66
|
|
|
66
67
|
module.exports = async function ( outputTemplates, view ) {
|
|
67
68
|
await Promise.all(
|
|
68
|
-
Object.keys( outputTemplates ).map(
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
join( view.folderName, outputFile ),
|
|
73
|
-
view
|
|
74
|
-
)
|
|
75
|
-
)
|
|
76
|
-
);
|
|
69
|
+
Object.keys( outputTemplates ).map( async ( outputFile ) => {
|
|
70
|
+
const pathName = view.plugin
|
|
71
|
+
? join( view.folderName, outputFile )
|
|
72
|
+
: join( process.cwd(), view.slug, outputFile );
|
|
77
73
|
|
|
74
|
+
await writeOutputTemplate(
|
|
75
|
+
outputTemplates[ outputFile ],
|
|
76
|
+
pathName,
|
|
77
|
+
view
|
|
78
|
+
);
|
|
79
|
+
} )
|
|
80
|
+
);
|
|
78
81
|
await initBlockJSON( view );
|
|
79
82
|
};
|
package/lib/init-package-json.js
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
* External dependencies
|
|
3
3
|
*/
|
|
4
4
|
const { command } = require( 'execa' );
|
|
5
|
-
const { isEmpty, omitBy } = require( 'lodash' );
|
|
6
5
|
const npmPackageArg = require( 'npm-package-arg' );
|
|
7
6
|
const { join } = require( 'path' );
|
|
8
7
|
const writePkg = require( 'write-pkg' );
|
|
@@ -24,6 +23,7 @@ module.exports = async ( {
|
|
|
24
23
|
npmDependencies,
|
|
25
24
|
npmDevDependencies,
|
|
26
25
|
customScripts,
|
|
26
|
+
isDynamicVariant,
|
|
27
27
|
} ) => {
|
|
28
28
|
const cwd = join( process.cwd(), slug );
|
|
29
29
|
|
|
@@ -32,8 +32,8 @@ module.exports = async ( {
|
|
|
32
32
|
|
|
33
33
|
await writePkg(
|
|
34
34
|
cwd,
|
|
35
|
-
|
|
36
|
-
{
|
|
35
|
+
Object.fromEntries(
|
|
36
|
+
Object.entries( {
|
|
37
37
|
name: slug,
|
|
38
38
|
version,
|
|
39
39
|
description,
|
|
@@ -43,19 +43,22 @@ module.exports = async ( {
|
|
|
43
43
|
main: wpScripts && 'build/index.js',
|
|
44
44
|
scripts: {
|
|
45
45
|
...( wpScripts && {
|
|
46
|
-
build:
|
|
46
|
+
build: isDynamicVariant
|
|
47
|
+
? 'wp-scripts build --webpack-copy-php'
|
|
48
|
+
: 'wp-scripts build',
|
|
47
49
|
format: 'wp-scripts format',
|
|
48
50
|
'lint:css': 'wp-scripts lint-style',
|
|
49
51
|
'lint:js': 'wp-scripts lint-js',
|
|
50
52
|
'packages-update': 'wp-scripts packages-update',
|
|
51
53
|
'plugin-zip': 'wp-scripts plugin-zip',
|
|
52
|
-
start:
|
|
54
|
+
start: isDynamicVariant
|
|
55
|
+
? 'wp-scripts start --webpack-copy-php'
|
|
56
|
+
: 'wp-scripts start',
|
|
53
57
|
} ),
|
|
54
58
|
...( wpEnv && { env: 'wp-env' } ),
|
|
55
59
|
...customScripts,
|
|
56
60
|
},
|
|
57
|
-
},
|
|
58
|
-
isEmpty
|
|
61
|
+
} ).filter( ( [ , value ] ) => !! value )
|
|
59
62
|
)
|
|
60
63
|
);
|
|
61
64
|
|
package/lib/output.js
CHANGED
|
@@ -13,13 +13,15 @@ const writeOutputAsset = async ( inputFile, outputFile, view ) => {
|
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
const writeOutputTemplate = async ( inputFile, outputFile, view ) => {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
outputFile.replace( /\$slug/g, view.slug )
|
|
20
|
-
);
|
|
16
|
+
const outputFilePath = view.plugin
|
|
17
|
+
? join( view.slug, outputFile.replace( /\$slug/g, view.slug ) )
|
|
18
|
+
: outputFile;
|
|
21
19
|
await makeDir( dirname( outputFilePath ) );
|
|
22
|
-
|
|
20
|
+
// If the rendered template is empty, don't write it. This is how we can conditionally add template files.
|
|
21
|
+
const renderedFile = render( inputFile, view );
|
|
22
|
+
if ( renderedFile.trim().length ) {
|
|
23
|
+
writeFile( outputFilePath, renderedFile );
|
|
24
|
+
}
|
|
23
25
|
};
|
|
24
26
|
|
|
25
27
|
module.exports = {
|
package/lib/prompts.js
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Capitalizes the first letter in a string.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} str The string whose first letter the function will capitalize.
|
|
5
|
+
*
|
|
6
|
+
* @return {string} Capitalized string.
|
|
3
7
|
*/
|
|
4
|
-
const
|
|
8
|
+
const upperFirst = ( [ firstLetter, ...rest ] ) =>
|
|
9
|
+
firstLetter.toUpperCase() + rest.join( '' );
|
|
5
10
|
|
|
6
11
|
// Block metadata.
|
|
7
12
|
const slug = {
|
|
8
13
|
type: 'input',
|
|
9
14
|
name: 'slug',
|
|
10
15
|
message:
|
|
11
|
-
'The block slug used for identification (also the
|
|
16
|
+
'The block slug used for identification (also the output folder name):',
|
|
12
17
|
validate( input ) {
|
|
13
18
|
if ( ! /^[a-z][a-z0-9\-]*$/.test( input ) ) {
|
|
14
19
|
return 'Invalid block slug specified. Block slug can contain only lowercase alphanumeric characters or dashes, and start with a letter.';
|
|
@@ -56,7 +61,7 @@ const dashicon = {
|
|
|
56
61
|
message:
|
|
57
62
|
'The dashicon to make it easier to identify your block (optional):',
|
|
58
63
|
validate( input ) {
|
|
59
|
-
if (
|
|
64
|
+
if ( input.length && ! /^[a-z][a-z0-9\-]*$/.test( input ) ) {
|
|
60
65
|
return 'Invalid dashicon name specified. Visit https://developer.wordpress.org/resource/dashicons/ to discover available names.';
|
|
61
66
|
}
|
|
62
67
|
|
package/lib/scaffold.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* External dependencies
|
|
3
3
|
*/
|
|
4
|
-
const {
|
|
4
|
+
const { pascalCase, snakeCase } = require( 'change-case' );
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Internal dependencies
|
|
@@ -10,7 +10,7 @@ const initBlock = require( './init-block' );
|
|
|
10
10
|
const initPackageJSON = require( './init-package-json' );
|
|
11
11
|
const initWPScripts = require( './init-wp-scripts' );
|
|
12
12
|
const initWPEnv = require( './init-wp-env' );
|
|
13
|
-
const { code, info, success } = require( './log' );
|
|
13
|
+
const { code, info, success, error } = require( './log' );
|
|
14
14
|
const { writeOutputAsset, writeOutputTemplate } = require( './output' );
|
|
15
15
|
|
|
16
16
|
module.exports = async (
|
|
@@ -18,6 +18,7 @@ module.exports = async (
|
|
|
18
18
|
{
|
|
19
19
|
$schema,
|
|
20
20
|
apiVersion,
|
|
21
|
+
plugin,
|
|
21
22
|
namespace,
|
|
22
23
|
slug,
|
|
23
24
|
title,
|
|
@@ -42,22 +43,39 @@ module.exports = async (
|
|
|
42
43
|
editorScript,
|
|
43
44
|
editorStyle,
|
|
44
45
|
style,
|
|
46
|
+
variantVars,
|
|
45
47
|
}
|
|
46
48
|
) => {
|
|
47
49
|
slug = slug.toLowerCase();
|
|
48
50
|
namespace = namespace.toLowerCase();
|
|
51
|
+
/**
|
|
52
|
+
* --no-plugin relies on the used template supporting the [blockTemplatesPath property](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-create-block/#blocktemplatespath).
|
|
53
|
+
* If the blockOutputTemplates object has no properties, we can assume that there was a custom --template passed that
|
|
54
|
+
* doesn't support it.
|
|
55
|
+
*/
|
|
56
|
+
if ( ! plugin && Object.keys( blockOutputTemplates ) < 1 ) {
|
|
57
|
+
error(
|
|
58
|
+
'No block files found in the template. Please ensure that the template supports the blockTemplatesPath property.'
|
|
59
|
+
);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
49
62
|
|
|
50
63
|
info( '' );
|
|
51
|
-
info(
|
|
64
|
+
info(
|
|
65
|
+
plugin
|
|
66
|
+
? `Creating a new WordPress plugin in the ${ slug } directory.`
|
|
67
|
+
: `Creating a new block in the ${ slug } directory.`
|
|
68
|
+
);
|
|
52
69
|
|
|
53
70
|
const view = {
|
|
54
71
|
$schema,
|
|
55
72
|
apiVersion,
|
|
73
|
+
plugin,
|
|
56
74
|
namespace,
|
|
57
75
|
namespaceSnakeCase: snakeCase( namespace ),
|
|
58
76
|
slug,
|
|
59
77
|
slugSnakeCase: snakeCase( slug ),
|
|
60
|
-
slugPascalCase:
|
|
78
|
+
slugPascalCase: pascalCase( slug ),
|
|
61
79
|
title,
|
|
62
80
|
description,
|
|
63
81
|
dashicon,
|
|
@@ -81,18 +99,21 @@ module.exports = async (
|
|
|
81
99
|
editorScript,
|
|
82
100
|
editorStyle,
|
|
83
101
|
style,
|
|
102
|
+
...variantVars,
|
|
84
103
|
};
|
|
85
104
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
105
|
+
if ( plugin ) {
|
|
106
|
+
await Promise.all(
|
|
107
|
+
Object.keys( pluginOutputTemplates ).map(
|
|
108
|
+
async ( outputFile ) =>
|
|
109
|
+
await writeOutputTemplate(
|
|
110
|
+
pluginOutputTemplates[ outputFile ],
|
|
111
|
+
outputFile,
|
|
112
|
+
view
|
|
113
|
+
)
|
|
114
|
+
)
|
|
115
|
+
);
|
|
116
|
+
}
|
|
96
117
|
|
|
97
118
|
await Promise.all(
|
|
98
119
|
Object.keys( outputAssets ).map(
|
|
@@ -107,21 +128,26 @@ module.exports = async (
|
|
|
107
128
|
|
|
108
129
|
await initBlock( blockOutputTemplates, view );
|
|
109
130
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
131
|
+
if ( plugin ) {
|
|
132
|
+
await initPackageJSON( view );
|
|
133
|
+
if ( wpScripts ) {
|
|
134
|
+
await initWPScripts( view );
|
|
135
|
+
}
|
|
115
136
|
|
|
116
|
-
|
|
117
|
-
|
|
137
|
+
if ( wpEnv ) {
|
|
138
|
+
await initWPEnv( view );
|
|
139
|
+
}
|
|
118
140
|
}
|
|
119
141
|
|
|
120
142
|
info( '' );
|
|
143
|
+
|
|
121
144
|
success(
|
|
122
|
-
|
|
145
|
+
plugin
|
|
146
|
+
? `Done: WordPress plugin ${ title } bootstrapped in the ${ slug } directory.`
|
|
147
|
+
: `Done: Block "${ title }" bootstrapped in the ${ slug }directory.`
|
|
123
148
|
);
|
|
124
|
-
|
|
149
|
+
|
|
150
|
+
if ( plugin && wpScripts ) {
|
|
125
151
|
info( '' );
|
|
126
152
|
info( 'You can run several commands inside:' );
|
|
127
153
|
info( '' );
|
|
@@ -145,18 +171,18 @@ module.exports = async (
|
|
|
145
171
|
info( '' );
|
|
146
172
|
code( ' $ npm run packages-update' );
|
|
147
173
|
info( ' Updates WordPress packages to the latest version.' );
|
|
174
|
+
info( '' );
|
|
175
|
+
info( 'To enter the directory type:' );
|
|
176
|
+
info( '' );
|
|
177
|
+
code( ` $ cd ${ slug }` );
|
|
148
178
|
}
|
|
149
|
-
|
|
150
|
-
info( 'To enter the directory type:' );
|
|
151
|
-
info( '' );
|
|
152
|
-
code( ` $ cd ${ slug }` );
|
|
153
|
-
if ( wpScripts ) {
|
|
179
|
+
if ( plugin && wpScripts ) {
|
|
154
180
|
info( '' );
|
|
155
181
|
info( 'You can start development with:' );
|
|
156
182
|
info( '' );
|
|
157
183
|
code( ' $ npm start' );
|
|
158
184
|
}
|
|
159
|
-
if ( wpEnv ) {
|
|
185
|
+
if ( plugin && wpEnv ) {
|
|
160
186
|
info( '' );
|
|
161
187
|
info( 'You can start WordPress with:' );
|
|
162
188
|
info( '' );
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Retrieves the translation of text.
|
|
3
3
|
*
|
|
4
|
-
* @see https://developer.wordpress.org/block-editor/packages/packages-i18n/
|
|
4
|
+
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-i18n/
|
|
5
5
|
*/
|
|
6
6
|
import { __ } from '@wordpress/i18n';
|
|
7
7
|
|
|
@@ -9,7 +9,7 @@ import { __ } from '@wordpress/i18n';
|
|
|
9
9
|
* React hook that is used to mark the block wrapper element.
|
|
10
10
|
* It provides all the necessary props like the class name.
|
|
11
11
|
*
|
|
12
|
-
* @see https://developer.wordpress.org/block-editor/packages/packages-block-editor/#
|
|
12
|
+
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops
|
|
13
13
|
*/
|
|
14
14
|
import { useBlockProps } from '@wordpress/block-editor';
|
|
15
15
|
|
|
@@ -25,7 +25,7 @@ import './editor.scss';
|
|
|
25
25
|
* The edit function describes the structure of your block in the context of the
|
|
26
26
|
* editor. This represents what the editor will render when the block is used.
|
|
27
27
|
*
|
|
28
|
-
* @see https://developer.wordpress.org/block-editor/
|
|
28
|
+
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit
|
|
29
29
|
*
|
|
30
30
|
* @return {WPElement} Element to render.
|
|
31
31
|
*/
|
|
@@ -18,7 +18,9 @@ import './style.scss';
|
|
|
18
18
|
* Internal dependencies
|
|
19
19
|
*/
|
|
20
20
|
import Edit from './edit';
|
|
21
|
+
{{#isStaticVariant}}
|
|
21
22
|
import save from './save';
|
|
23
|
+
{{/isStaticVariant}}
|
|
22
24
|
import metadata from './block.json';
|
|
23
25
|
|
|
24
26
|
/**
|
|
@@ -31,8 +33,11 @@ registerBlockType( metadata.name, {
|
|
|
31
33
|
* @see ./edit.js
|
|
32
34
|
*/
|
|
33
35
|
edit: Edit,
|
|
36
|
+
{{#isStaticVariant}}
|
|
37
|
+
|
|
34
38
|
/**
|
|
35
39
|
* @see ./save.js
|
|
36
40
|
*/
|
|
37
41
|
save,
|
|
42
|
+
{{/isStaticVariant}}
|
|
38
43
|
} );
|
|
@@ -1,15 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
* Retrieves the translation of text.
|
|
3
|
-
*
|
|
4
|
-
* @see https://developer.wordpress.org/block-editor/packages/packages-i18n/
|
|
5
|
-
*/
|
|
6
|
-
import { __ } from '@wordpress/i18n';
|
|
7
|
-
|
|
1
|
+
{{#isStaticVariant}}
|
|
8
2
|
/**
|
|
9
3
|
* React hook that is used to mark the block wrapper element.
|
|
10
4
|
* It provides all the necessary props like the class name.
|
|
11
5
|
*
|
|
12
|
-
* @see https://developer.wordpress.org/block-editor/packages/packages-block-editor/#
|
|
6
|
+
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops
|
|
13
7
|
*/
|
|
14
8
|
import { useBlockProps } from '@wordpress/block-editor';
|
|
15
9
|
|
|
@@ -18,17 +12,15 @@ import { useBlockProps } from '@wordpress/block-editor';
|
|
|
18
12
|
* be combined into the final markup, which is then serialized by the block
|
|
19
13
|
* editor into `post_content`.
|
|
20
14
|
*
|
|
21
|
-
* @see https://developer.wordpress.org/block-editor/
|
|
15
|
+
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#save
|
|
22
16
|
*
|
|
23
17
|
* @return {WPElement} Element to render.
|
|
24
18
|
*/
|
|
25
19
|
export default function save() {
|
|
26
20
|
return (
|
|
27
21
|
<p { ...useBlockProps.save() }>
|
|
28
|
-
{
|
|
29
|
-
'{{title}} – hello from the saved content!',
|
|
30
|
-
'{{textdomain}}'
|
|
31
|
-
) }
|
|
22
|
+
{ '{{title}} – hello from the saved content!' }
|
|
32
23
|
</p>
|
|
33
24
|
);
|
|
34
25
|
}
|
|
26
|
+
{{/isStaticVariant}}
|
|
@@ -75,7 +75,27 @@ function {{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init() {
|
|
|
75
75
|
'editor_script' => '{{namespace}}-{{slug}}-block-editor',
|
|
76
76
|
'editor_style' => '{{namespace}}-{{slug}}-block-editor',
|
|
77
77
|
'style' => '{{namespace}}-{{slug}}-block',
|
|
78
|
+
{{#isDynamicVariant}}
|
|
79
|
+
'render_callback' => '{{namespaceSnakeCase}}_{{slugSnakeCase}}_render_callback',
|
|
80
|
+
{{/isDynamicVariant}}
|
|
78
81
|
)
|
|
79
82
|
);
|
|
80
83
|
}
|
|
81
84
|
add_action( 'init', '{{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init' );
|
|
85
|
+
{{#isDynamicVariant}}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Render callback function.
|
|
89
|
+
*
|
|
90
|
+
* @param array $attributes The block attributes.
|
|
91
|
+
* @param string $content The block content.
|
|
92
|
+
* @param WP_Block $block Block instance.
|
|
93
|
+
*
|
|
94
|
+
* @return string The rendered output.
|
|
95
|
+
*/
|
|
96
|
+
function {{namespaceSnakeCase}}_{{slugSnakeCase}}_render_callback( $attributes, $content, $block ) {
|
|
97
|
+
ob_start();
|
|
98
|
+
require plugin_dir_path( __FILE__ ) . '/template.php';
|
|
99
|
+
return ob_get_clean();
|
|
100
|
+
}
|
|
101
|
+
{{/isDynamicVariant}}
|
|
@@ -16,14 +16,14 @@
|
|
|
16
16
|
/**
|
|
17
17
|
* Retrieves the translation of text.
|
|
18
18
|
*
|
|
19
|
-
* @see https://developer.wordpress.org/block-editor/packages/packages-i18n/
|
|
19
|
+
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-i18n/
|
|
20
20
|
*/
|
|
21
21
|
var __ = wp.i18n.__;
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
24
|
* This hook is used to mark the block wrapper element.
|
|
25
25
|
*
|
|
26
|
-
* @see https://developer.wordpress.org/block-editor/packages/packages-block-editor/#
|
|
26
|
+
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops
|
|
27
27
|
*/
|
|
28
28
|
var useBlockProps = wp.blockEditor.useBlockProps;
|
|
29
29
|
|
|
@@ -84,7 +84,7 @@
|
|
|
84
84
|
* The edit function describes the structure of your block in the context of the editor.
|
|
85
85
|
* This represents what the editor will render when the block is used.
|
|
86
86
|
*
|
|
87
|
-
* @see https://developer.wordpress.org/block-editor/
|
|
87
|
+
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit
|
|
88
88
|
*
|
|
89
89
|
* @return {WPElement} Element to render.
|
|
90
90
|
*/
|
|
@@ -95,12 +95,13 @@
|
|
|
95
95
|
__( '{{title}} – hello from the editor!', '{{textdomain}}' )
|
|
96
96
|
);
|
|
97
97
|
},
|
|
98
|
+
{{#isStaticVariant}}
|
|
98
99
|
|
|
99
100
|
/**
|
|
100
101
|
* The save function defines the way in which the different attributes should be combined
|
|
101
102
|
* into the final markup, which is then serialized by the block editor into `post_content`.
|
|
102
103
|
*
|
|
103
|
-
* @see https://developer.wordpress.org/block-editor/
|
|
104
|
+
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#save
|
|
104
105
|
*
|
|
105
106
|
* @return {WPElement} Element to render.
|
|
106
107
|
*/
|
|
@@ -108,9 +109,10 @@
|
|
|
108
109
|
return el(
|
|
109
110
|
'p',
|
|
110
111
|
useBlockProps.save(),
|
|
111
|
-
|
|
112
|
+
'{{title}} – hello from the saved content!',
|
|
112
113
|
);
|
|
113
114
|
},
|
|
115
|
+
{{/isStaticVariant}}
|
|
114
116
|
} );
|
|
115
117
|
}(
|
|
116
118
|
window.wp
|
|
@@ -38,6 +38,33 @@
|
|
|
38
38
|
* @see https://developer.wordpress.org/reference/functions/register_block_type/
|
|
39
39
|
*/
|
|
40
40
|
function {{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init() {
|
|
41
|
+
{{#isStaticVariant}}
|
|
41
42
|
register_block_type( __DIR__ . '/build' );
|
|
43
|
+
{{/isStaticVariant}}
|
|
44
|
+
{{#isDynamicVariant}}
|
|
45
|
+
register_block_type(
|
|
46
|
+
__DIR__ . '/build',
|
|
47
|
+
array(
|
|
48
|
+
'render_callback' => '{{namespaceSnakeCase}}_{{slugSnakeCase}}_render_callback',
|
|
49
|
+
)
|
|
50
|
+
);
|
|
51
|
+
{{/isDynamicVariant}}
|
|
42
52
|
}
|
|
43
53
|
add_action( 'init', '{{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init' );
|
|
54
|
+
{{#isDynamicVariant}}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Render callback function.
|
|
58
|
+
*
|
|
59
|
+
* @param array $attributes The block attributes.
|
|
60
|
+
* @param string $content The block content.
|
|
61
|
+
* @param WP_Block $block Block instance.
|
|
62
|
+
*
|
|
63
|
+
* @return string The rendered output.
|
|
64
|
+
*/
|
|
65
|
+
function {{namespaceSnakeCase}}_{{slugSnakeCase}}_render_callback( $attributes, $content, $block ) {
|
|
66
|
+
ob_start();
|
|
67
|
+
require plugin_dir_path( __FILE__ ) . 'build/template.php';
|
|
68
|
+
return ob_get_clean();
|
|
69
|
+
}
|
|
70
|
+
{{/isDynamicVariant}}
|
package/lib/templates.js
CHANGED
|
@@ -6,7 +6,6 @@ const glob = require( 'fast-glob' );
|
|
|
6
6
|
const { resolve } = require( 'path' );
|
|
7
7
|
const { existsSync } = require( 'fs' );
|
|
8
8
|
const { mkdtemp, readFile } = require( 'fs' ).promises;
|
|
9
|
-
const { fromPairs } = require( 'lodash' );
|
|
10
9
|
const npmPackageArg = require( 'npm-package-arg' );
|
|
11
10
|
const { tmpdir } = require( 'os' );
|
|
12
11
|
const { join } = require( 'path' );
|
|
@@ -25,7 +24,7 @@ const predefinedPluginTemplates = {
|
|
|
25
24
|
slug: 'example-static-es5',
|
|
26
25
|
title: 'Example Static (ES5)',
|
|
27
26
|
description:
|
|
28
|
-
'Example
|
|
27
|
+
'Example block scaffolded with Create Block tool – no build step required.',
|
|
29
28
|
dashicon: 'smiley',
|
|
30
29
|
wpScripts: false,
|
|
31
30
|
editorScript: 'file:./index.js',
|
|
@@ -33,18 +32,31 @@ const predefinedPluginTemplates = {
|
|
|
33
32
|
style: 'file:./style.css',
|
|
34
33
|
},
|
|
35
34
|
templatesPath: join( __dirname, 'templates', 'es5' ),
|
|
35
|
+
variants: {
|
|
36
|
+
static: {},
|
|
37
|
+
dynamic: {
|
|
38
|
+
slug: 'example-dynamic-es5',
|
|
39
|
+
title: 'Example Dynamic (ES5)',
|
|
40
|
+
},
|
|
41
|
+
},
|
|
36
42
|
},
|
|
37
|
-
|
|
43
|
+
standard: {
|
|
38
44
|
defaultValues: {
|
|
39
45
|
slug: 'example-static',
|
|
40
46
|
title: 'Example Static',
|
|
41
|
-
description:
|
|
42
|
-
'Example static block scaffolded with Create Block tool.',
|
|
47
|
+
description: 'Example block scaffolded with Create Block tool.',
|
|
43
48
|
dashicon: 'smiley',
|
|
44
49
|
supports: {
|
|
45
50
|
html: false,
|
|
46
51
|
},
|
|
47
52
|
},
|
|
53
|
+
variants: {
|
|
54
|
+
static: {},
|
|
55
|
+
dynamic: {
|
|
56
|
+
slug: 'example-dynamic',
|
|
57
|
+
title: 'Example Dynamic',
|
|
58
|
+
},
|
|
59
|
+
},
|
|
48
60
|
},
|
|
49
61
|
};
|
|
50
62
|
|
|
@@ -53,7 +65,7 @@ const getOutputTemplates = async ( outputTemplatesPath ) => {
|
|
|
53
65
|
cwd: outputTemplatesPath,
|
|
54
66
|
dot: true,
|
|
55
67
|
} );
|
|
56
|
-
return
|
|
68
|
+
return Object.fromEntries(
|
|
57
69
|
await Promise.all(
|
|
58
70
|
outputTemplatesFiles.map( async ( outputTemplateFile ) => {
|
|
59
71
|
const outputFile = outputTemplateFile.replace(
|
|
@@ -75,7 +87,7 @@ const getOutputAssets = async ( outputAssetsPath ) => {
|
|
|
75
87
|
cwd: outputAssetsPath,
|
|
76
88
|
dot: true,
|
|
77
89
|
} );
|
|
78
|
-
return
|
|
90
|
+
return Object.fromEntries(
|
|
79
91
|
await Promise.all(
|
|
80
92
|
outputAssetFiles.map( async ( outputAssetFile ) => {
|
|
81
93
|
const outputAsset = await readFile(
|
|
@@ -99,8 +111,9 @@ const externalTemplateExists = async ( templateName ) => {
|
|
|
99
111
|
const configToTemplate = async ( {
|
|
100
112
|
pluginTemplatesPath,
|
|
101
113
|
blockTemplatesPath,
|
|
102
|
-
defaultValues = {},
|
|
103
114
|
assetsPath,
|
|
115
|
+
defaultValues = {},
|
|
116
|
+
variants = {},
|
|
104
117
|
...deprecated
|
|
105
118
|
} ) => {
|
|
106
119
|
if ( defaultValues === null || typeof defaultValues !== 'object' ) {
|
|
@@ -127,9 +140,10 @@ const configToTemplate = async ( {
|
|
|
127
140
|
blockOutputTemplates: blockTemplatesPath
|
|
128
141
|
? await getOutputTemplates( blockTemplatesPath )
|
|
129
142
|
: {},
|
|
130
|
-
defaultValues,
|
|
131
|
-
outputAssets: assetsPath ? await getOutputAssets( assetsPath ) : {},
|
|
132
143
|
pluginOutputTemplates: await getOutputTemplates( pluginTemplatesPath ),
|
|
144
|
+
outputAssets: assetsPath ? await getOutputAssets( assetsPath ) : {},
|
|
145
|
+
defaultValues,
|
|
146
|
+
variants,
|
|
133
147
|
};
|
|
134
148
|
};
|
|
135
149
|
|
|
@@ -198,7 +212,7 @@ const getPluginTemplate = async ( templateName ) => {
|
|
|
198
212
|
}
|
|
199
213
|
};
|
|
200
214
|
|
|
201
|
-
const getDefaultValues = ( pluginTemplate ) => {
|
|
215
|
+
const getDefaultValues = ( pluginTemplate, variant ) => {
|
|
202
216
|
return {
|
|
203
217
|
$schema: 'https://schemas.wp.org/trunk/block.json',
|
|
204
218
|
apiVersion: 2,
|
|
@@ -217,11 +231,13 @@ const getDefaultValues = ( pluginTemplate ) => {
|
|
|
217
231
|
editorStyle: 'file:./index.css',
|
|
218
232
|
style: 'file:./style-index.css',
|
|
219
233
|
...pluginTemplate.defaultValues,
|
|
234
|
+
...pluginTemplate.variants?.[ variant ],
|
|
235
|
+
variantVars: getVariantVars( pluginTemplate.variants, variant ),
|
|
220
236
|
};
|
|
221
237
|
};
|
|
222
238
|
|
|
223
|
-
const getPrompts = ( pluginTemplate, keys ) => {
|
|
224
|
-
const defaultValues = getDefaultValues( pluginTemplate );
|
|
239
|
+
const getPrompts = ( pluginTemplate, keys, variant ) => {
|
|
240
|
+
const defaultValues = getDefaultValues( pluginTemplate, variant );
|
|
225
241
|
return keys.map( ( promptName ) => {
|
|
226
242
|
return {
|
|
227
243
|
...prompts[ promptName ],
|
|
@@ -230,6 +246,24 @@ const getPrompts = ( pluginTemplate, keys ) => {
|
|
|
230
246
|
} );
|
|
231
247
|
};
|
|
232
248
|
|
|
249
|
+
const getVariantVars = ( variants, variant ) => {
|
|
250
|
+
const variantVars = {};
|
|
251
|
+
const variantNames = Object.keys( variants );
|
|
252
|
+
if ( variantNames.length === 0 ) {
|
|
253
|
+
return variantVars;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const currentVariant = variant ?? variantNames[ 0 ];
|
|
257
|
+
for ( const variantName of variantNames ) {
|
|
258
|
+
const key =
|
|
259
|
+
variantName.charAt( 0 ).toUpperCase() + variantName.slice( 1 );
|
|
260
|
+
variantVars[ `is${ key }Variant` ] =
|
|
261
|
+
currentVariant === variantName ?? false;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return variantVars;
|
|
265
|
+
};
|
|
266
|
+
|
|
233
267
|
module.exports = {
|
|
234
268
|
getPluginTemplate,
|
|
235
269
|
getDefaultValues,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/create-block",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "Generates PHP, JS and CSS code for registering a block for a WordPress plugin.",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"url": "https://github.com/WordPress/gutenberg/issues"
|
|
21
21
|
},
|
|
22
22
|
"engines": {
|
|
23
|
-
"node": ">=
|
|
24
|
-
"npm": ">=6.
|
|
23
|
+
"node": ">=14",
|
|
24
|
+
"npm": ">=6.14.4"
|
|
25
25
|
},
|
|
26
26
|
"files": [
|
|
27
27
|
"lib"
|
|
@@ -33,12 +33,12 @@
|
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@wordpress/lazy-import": "^1.4.2",
|
|
35
35
|
"chalk": "^4.0.0",
|
|
36
|
+
"change-case": "^4.1.2",
|
|
36
37
|
"check-node-version": "^4.1.0",
|
|
37
38
|
"commander": "^9.2.0",
|
|
38
39
|
"execa": "^4.0.2",
|
|
39
40
|
"fast-glob": "^3.2.7",
|
|
40
41
|
"inquirer": "^7.1.0",
|
|
41
|
-
"lodash": "^4.17.21",
|
|
42
42
|
"make-dir": "^3.0.0",
|
|
43
43
|
"mustache": "^4.0.0",
|
|
44
44
|
"npm-package-arg": "^8.1.5",
|
|
@@ -48,5 +48,5 @@
|
|
|
48
48
|
"publishConfig": {
|
|
49
49
|
"access": "public"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "171b87c7465b93e685e081c5f57f153507363c95"
|
|
52
52
|
}
|