-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Scripts: Add create-block
script for bootstrapping a minimal block development plugin.
#18650
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
const { kebabCase } = require( 'lodash' ); | ||
const { copy } = require( 'fs-extra' ); | ||
const replaceInFile = require( 'replace-in-file' ); | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
const { getArgsFromCLI, fromTemplatesRoot } = require( '../utils' ); | ||
|
||
const blockTitle = getArgsFromCLI()[ 0 ]; | ||
if ( ! blockTitle ) { | ||
process.stdout.write( 'No block name provided.' ); | ||
process.exit( 1 ); | ||
} | ||
const blockName = kebabCase( blockTitle ); | ||
const pluginName = `${ blockName }-plugin`; | ||
const pluginPath = `./${ pluginName }` | ||
|
||
;( async () => { | ||
await copy( fromTemplatesRoot( 'block-plugin' ), pluginPath ); | ||
await replaceInFile( { | ||
files: `${ pluginPath }/**`, | ||
from: [ | ||
/block(_|-)plugin\1block\1title/g, | ||
/block(_|-)plugin\1block\1name/g, | ||
/block(_|-)plugin\1name/g, | ||
], | ||
to: [ blockTitle, blockName, pluginName ], | ||
} ); | ||
} )(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Dependencies | ||
/node_modules/ | ||
|
||
# Build | ||
/build/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "block-plugin-name", | ||
"version": "1.0.0", | ||
"main": "plugin.php", | ||
"scripts": { | ||
"start": "wp-env start", | ||
"dev": "wp-scripts start", | ||
"build": "wp-scripts build" | ||
}, | ||
"license": "GPL-2.0-or-later", | ||
"devDependencies": { | ||
"@wordpress/env": "^0.1.0", | ||
"@wordpress/scripts": "^6.0.0" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
/** | ||
* Plugin Name: block_plugin_name | ||
* Version: 1.0.0 | ||
* License: GPL2+ | ||
* License URI: https://www.gnu.org/licenses/gpl-2.0.txt | ||
* | ||
* @package block_plugin_name | ||
*/ | ||
|
||
/** | ||
* Enqueue editor and front end assets. | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
function block_plugin_name_enqueue_block_assets() { | ||
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. Testing this PR
The function name is being generated as:
Instead of kebabCase, the
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. Oops, should be fixed now. Thanks for catching this! |
||
wp_enqueue_style( | ||
'block_plugin_name-css', | ||
plugins_url( 'src/style.css', __FILE__ ), | ||
array(), | ||
filemtime( plugin_dir_path( __FILE__ ) . 'src/style.css' ) | ||
); | ||
} | ||
add_action( 'enqueue_block_assets', 'block_plugin_name_enqueue_block_assets' ); | ||
|
||
/** | ||
* Enqueue editor assets. | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
function block_plugin_name_enqueue_block_editor_assets() { | ||
wp_enqueue_script( | ||
'block_plugin_name-js', | ||
plugins_url( 'build/index.js', __FILE__ ), | ||
array( 'wp-blocks', 'wp-element' ), | ||
filemtime( plugin_dir_path( __FILE__ ) . 'build/index.js' ) | ||
); | ||
wp_enqueue_style( | ||
'block_plugin_name-editor-css', | ||
plugins_url( 'src/editor.css', __FILE__ ), | ||
array(), | ||
filemtime( plugin_dir_path( __FILE__ ) . 'src/editor.css' ) | ||
); | ||
} | ||
add_action( 'enqueue_block_editor_assets', 'block_plugin_name_enqueue_block_editor_assets' ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* Styles exclusively for the editor. */ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* global wp */ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
const { registerBlockType } = wp.blocks; | ||
|
||
registerBlockType( 'block-plugin-name/block-plugin-block-name', { | ||
title: 'block-plugin-block-title', | ||
category: 'common', | ||
edit: () => <div>Editor</div>, | ||
save: () => <div>Front End</div>, | ||
} ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* Styles shared between the editor and the front end. */ |
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.
If the plugin is called "My Fancy Block" does it append
-plugin
to the folder created?If not, then:
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.
Yes, "My Fancy Block" is the block name.
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.
Why append
-plugin
to the plugin name? Why not justmy-fancy-block
for the slug?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.
So that they are not the same.
Is there a convention for this?
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.
With
create-react-app
whatever name I use is converted to kebab-case, no extra-app
or-plugin
is appended to the name.And for the most part if I would expect the name to be what name I gave, if I gave "My Fancy Block Plugin" then I would expect the
-plugin
included in the slugThere 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.
Is there any concerns where having the plugin name and plugin slug the same that would be of concern?
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.
Yes, because you are naming the app, and the app only.
You are not naming the plugin, you are naming the block, and the plugin name gets generated from that.
No, I think it's just a style thing. I don't have a strong opinion on it.