Skip to content

Commit f35c95c

Browse files
authored
Merge pull request #186 from alleyinteractive/feature/hmr-editor
Fix static/dynamic block variants, add prompt for block templates, HMR support for blocks.
2 parents dd998a1 + 5f47062 commit f35c95c

21 files changed

+441
-559
lines changed

bin/create-block/defaultValues.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Default variables for scaffolding blocks.
3+
*
4+
* @see https://github.com/WordPress/gutenberg/blob/trunk/packages/create-block/docs/external-template.md#defaultvalues
5+
*/
6+
module.exports = {
7+
namespace: 'create-wordpress-plugin',
8+
plugin: false,
9+
description: '',
10+
dashicon: 'palmtree',
11+
category: 'widgets',
12+
editorScript: 'file:index.ts',
13+
editorStyle: 'file:index.css',
14+
style: ['file:style-index.css'],
15+
};

bin/create-block/index.js

Lines changed: 68 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,72 @@
1+
#!/usr/bin/env node
2+
3+
const prompts = require('prompts');
14
const path = require('path');
5+
const { sync: spawn } = require('cross-spawn');
6+
7+
const fs = require('fs');
8+
9+
// The directory where the blocks will be created relative to the current working directory.
10+
const directoryName = 'blocks';
11+
12+
// Create the directory if it doesn't exist.
13+
if (!fs.existsSync(directoryName)) {
14+
fs.mkdirSync(directoryName);
15+
// eslint-disable-next-line no-console
16+
console.log(`Directory '${directoryName}' created successfully!`);
17+
// Navigate to the directory to create the block.
18+
process.chdir(directoryName);
19+
} else {
20+
process.chdir(directoryName);
21+
}
222

323
/**
4-
* Custom Variables and templates for scaffolding blocks.
5-
* @see https://github.com/WordPress/gutenberg/blob/trunk/packages/create-block/docs/external-template.md#external-project-templates
24+
* Prompts the user to select a block language (TypeScript or JavaScript)
25+
* and then create a block using the @wordpress/create-block package.
626
*/
7-
module.exports = {
8-
defaultValues: {
9-
namespace: 'create-wordpress-plugin',
10-
plugin: false,
11-
description: '',
12-
dashicon: 'palmtree',
13-
category: 'widgets',
14-
editorScript: 'file:index.ts',
15-
editorStyle: 'file:index.css',
16-
style: ['file:style-index.css'],
17-
},
18-
variants: {
19-
static: {},
20-
'static-javascript': {
21-
blockTemplatesPath: path.join(__dirname, 'templates', 'block', 'javascript'),
22-
},
23-
dynamic: {
24-
render: 'file:render.php',
25-
},
26-
'dynamic-javascript': {
27-
blockTemplatesPath: path.join(__dirname, 'templates', 'block', 'javascript'),
28-
render: 'file:render.php',
29-
},
30-
},
31-
blockTemplatesPath: path.join(__dirname, 'templates', 'block', 'typescript'),
32-
};
27+
(async () => {
28+
const response = await prompts({
29+
type: 'select',
30+
name: 'blockLanguage',
31+
message: 'Create a block in TypeScript or JavaScript?',
32+
choices: [
33+
{ title: 'TypeScript', value: 'typescript' },
34+
{ title: 'JavaScript', value: 'javascript' },
35+
],
36+
initial: 0,
37+
});
38+
39+
const language = response?.blockLanguage || null;
40+
41+
if (language) {
42+
// Set the block language as an environment variable
43+
// so it can be used in the selectTemplates.js file.
44+
process.env.blockLanguage = language;
45+
46+
// Create a block using the @wordpress/create-block package.
47+
const result = spawn(
48+
'npx',
49+
[
50+
'@wordpress/create-block',
51+
/**
52+
* This argument specifies an external npm package as a template.
53+
* In this case, the selectTemplates.js file is used as a the entry for the template.
54+
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-create-block/#template
55+
*/
56+
'--template',
57+
path.join(__dirname, 'selectTemplates.js'),
58+
/**
59+
* With this argument, the create-block package runs in
60+
* "No plugin mode" which only scaffolds block files into the current directory.
61+
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-create-block/#no-plugin
62+
*/
63+
'--no-plugin',
64+
],
65+
{ stdio: 'inherit' },
66+
);
67+
68+
process.exit(result.status);
69+
} else {
70+
process.exit(1);
71+
}
72+
})();

bin/create-block/selectTemplates.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const path = require('path');
2+
const defaultValues = require('./defaultValues');
3+
4+
const { blockLanguage } = process.env;
5+
6+
/**
7+
* Custom variants for scaffolding blocks.
8+
*
9+
* Currently there are only two variants:
10+
* - static: A block that scaffolds a save.js file
11+
* that saves the content and markup directly in the post content.
12+
* - dynamic: A block that scaffolds a render.php template
13+
* which can be used to render the block on the front-end.
14+
*
15+
* @see https://github.com/WordPress/gutenberg/blob/trunk/packages/create-block/docs/external-template.md#external-project-templates
16+
*/
17+
module.exports = {
18+
defaultValues,
19+
variants: {
20+
static: {
21+
blockTemplatesPath: path.join(__dirname, 'templates', blockLanguage),
22+
},
23+
dynamic: {
24+
blockTemplatesPath: path.join(__dirname, 'templates', blockLanguage),
25+
render: 'file:render.php',
26+
},
27+
},
28+
blockTemplatesPath: path.join(__dirname, 'templates', blockLanguage),
29+
};

bin/create-block/templates/block/javascript/render.php.mustache renamed to bin/create-block/templates/javascript/render.php.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212

1313
?>
1414
<p <?php echo wp_kses_data( get_block_wrapper_attributes() ); ?>>
15-
<?php esc_html_e( '{{title}} hello from a dynamic block!', 'create-wordpress-plugin' ); ?>
15+
<?php esc_html_e( '{{title}} - hello from a dynamic block!', 'create-wordpress-plugin' ); ?>
1616
</p>
1717
{{/isDynamicVariant}}

bin/create-block/templates/block/javascript/save.jsx.mustache renamed to bin/create-block/templates/javascript/save.jsx.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { useBlockProps } from '@wordpress/block-editor';
1919
export default function save() {
2020
return (
2121
<p {...useBlockProps.save()}>
22-
{'{{title}} hello from the saved content!'}
22+
{'{{title}} - hello from the saved content!'}
2323
</p>
2424
);
2525
}

bin/create-block/templates/block/typescript/edit.tsx.mustache renamed to bin/create-block/templates/typescript/edit.tsx.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import './index.scss';
3232
export default function Edit() {
3333
return (
3434
<p {...useBlockProps()}>
35-
{ __('Block Title hello from the editor!', 'create-wordpress-plugin') }
35+
{ __('Block Title - hello from the editor!', 'create-wordpress-plugin') }
3636
</p>
3737
);
3838
}

bin/create-block/templates/block/typescript/render.php.mustache renamed to bin/create-block/templates/typescript/render.php.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212

1313
?>
1414
<p <?php echo wp_kses_data( get_block_wrapper_attributes() ); ?>>
15-
<?php esc_html_e( '{{title}} hello from a dynamic block!', 'create-wordpress-plugin' ); ?>
15+
<?php esc_html_e( '{{title}} - hello from a dynamic block!', 'create-wordpress-plugin' ); ?>
1616
</p>
1717
{{/isDynamicVariant}}

bin/create-block/templates/block/typescript/save.tsx.mustache renamed to bin/create-block/templates/typescript/save.tsx.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { useBlockProps } from '@wordpress/block-editor';
1919
export default function save() {
2020
return (
2121
<p {...useBlockProps.save()}>
22-
{'{{title}} hello from the saved content!'}
22+
{'{{title}} - hello from the saved content!'}
2323
</p>
2424
);
2525
}

blocks/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ Custom blocks in this directory can be created by running the `create-block` scr
55
## Scaffold a block with `create-block`
66

77
1. In the root directory run `npm run create-block`
8-
2. Follow the prompts to create a custom block.
8+
2. Choose whether to create a block in TypeScript or JavaScript.
9+
3. Follow the prompts to create a custom block.
910

1011
There are 2 variants of blocks which you can create:
1112

@@ -53,7 +54,7 @@ The `index.php` contains the PHP block registration and will be autoloaded with
5354

5455
Block attributes should be defined in the `block.json` file. [Learn more about block.json in the block editor handbook.](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/)
5556

56-
Running `npm run build` will compile the JavaScript and copy the PHP files to a directory in the `build` folder using `@wordpress/scripts`. The blocks will be enqueued via `block.json` after block registration. The block `index.php` file will be read by the `load_scripts()` function found in the `function.php` file.
57+
Running `npm run build` will compile the JavaScript and copy the PHP files to a directory in the `build` folder using `@wordpress/scripts`. The blocks will be enqueued via `block.json` after block registration. The block `index.php` file will be read by the `load_scripts()` function found in the `src/assets.php` file.
5758

5859
## Customize the block scaffolding templates
5960

0 commit comments

Comments
 (0)