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

Scripts: Add create-block script for bootstrapping a minimal block development plugin. #18650

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
179 changes: 177 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions packages/scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,20 @@ _Flags_:
- `--gpl2`: Validates against [GPLv2 license compatibility](https://www.gnu.org/licenses/license-list.en.html)
- `--ignore=a,b,c`: A comma-separated set of package names to ignore for validation. This is intended to be used primarily in cases where a dependency’s `license` field is malformed. It’s assumed that any `ignored` package argument would be manually vetted for compatibility by the project owner.

### `create-block`

Takes a new name for a block and creates a minimal plugin development environment for it in the current working directory.

_Example:_

```sh
$ wp-scripts create-block "My Fancy Block"
$ cd ./my-fancy-block-plugin
Copy link
Member

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:

Suggested change
$ cd ./my-fancy-block-plugin
$ cd ./my-fancy-block

Copy link
Contributor Author

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?

Yes, "My Fancy Block" is the block name.

Copy link
Member

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 just my-fancy-block for the slug?

Copy link
Contributor Author

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?

Copy link
Member

@ntwb ntwb Nov 21, 2019

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 slug

Copy link
Member

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 any concerns where having the plugin name and plugin slug the same that would be of concern?

Copy link
Contributor Author

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 snake-case, no extra -app or -plugin is appended to the name.

Yes, because you are naming the app, and the app only.

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 slug

You are not naming the plugin, you are naming the block, and the plugin name gets generated from that.

Is there any concerns where having the plugin name and plugin slug the same that would be of concern?

No, I think it's just a style thing. I don't have a strong opinion on it.

$ npm install
$ npm run start # Start WordPress instance.
$ npm run dev # Build and watch files.
```

### `env`

`env` is a family of scripts for setting up a local Docker-based development environment that plugin contributors can work in.
Expand Down
2 changes: 2 additions & 0 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"cross-spawn": "^5.1.0",
"decompress-zip": "^0.2.2",
"eslint": "^6.1.0",
"fs-extra": "^8.1.0",
"jest": "^24.7.1",
"jest-puppeteer": "^4.3.0",
"js-yaml": "^3.13.1",
Expand All @@ -53,6 +54,7 @@
"npm-package-json-lint": "^4.0.3",
"puppeteer": "^2.0.0",
"read-pkg-up": "^1.0.1",
"replace-in-file": "^4.2.0",
"request": "^2.88.0",
"resolve-bin": "^0.4.0",
"source-map-loader": "^0.2.4",
Expand Down
33 changes: 33 additions & 0 deletions packages/scripts/scripts/create-block.js
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 ],
} );
} )();
5 changes: 5 additions & 0 deletions packages/scripts/templates/block-plugin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Dependencies
/node_modules/

# Build
/build/
15 changes: 15 additions & 0 deletions packages/scripts/templates/block-plugin/package.json
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"
}
}
45 changes: 45 additions & 0 deletions packages/scripts/templates/block-plugin/plugin.php
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() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing this PR

Uncaught Error: syntax error, unexpected '-', expecting '(' in /wp-content/plugins/my-block-plugin/plugin.php on line 16

The function name is being generated as:

my-block-plugin_enqueue_block_assets()

Instead of kebabCase, the my-block-plugin needs to be snakeCase:

my_block_plugin_enqueue_block_assets()

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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' );
1 change: 1 addition & 0 deletions packages/scripts/templates/block-plugin/src/editor.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* Styles exclusively for the editor. */
12 changes: 12 additions & 0 deletions packages/scripts/templates/block-plugin/src/index.js
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>,
} );
1 change: 1 addition & 0 deletions packages/scripts/templates/block-plugin/src/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* Styles shared between the editor and the front end. */
4 changes: 4 additions & 0 deletions packages/scripts/utils/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const hasProjectFile = ( fileName ) =>
const fromConfigRoot = ( fileName ) =>
path.join( path.dirname( __dirname ), 'config', fileName );

const fromTemplatesRoot = ( fileName ) =>
path.join( path.dirname( __dirname ), 'templates', fileName );

const fromScriptsRoot = ( scriptName ) =>
path.join( path.dirname( __dirname ), 'scripts', `${ scriptName }.js` );

Expand All @@ -26,6 +29,7 @@ const hasScriptFile = ( scriptName ) =>

module.exports = {
fromConfigRoot,
fromTemplatesRoot,
fromScriptsRoot,
hasProjectFile,
hasScriptFile,
Expand Down
Loading