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

Add support for @wordpress/scripts, block.json and create block #82

Merged
merged 46 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
6cea812
add create-block templates for create-block script
stevenslack Nov 29, 2022
5790ede
add create-block, scripts, and run update dependencies
stevenslack Nov 29, 2022
f617b72
add copy-webpack-plugin
stevenslack Nov 29, 2022
8cc37a4
update dependencies
stevenslack Nov 29, 2022
b17784b
use @alleyinteractive/stylelint-config for stylelint configuration
stevenslack Nov 29, 2022
aed60e9
add blocks directory
stevenslack Nov 29, 2022
2ba7a44
add tsconfig.json configuration file
stevenslack Nov 29, 2022
6091461
update readme with dependency update info
stevenslack Nov 29, 2022
9c13eab
add blocks directory to be removed in configure file
stevenslack Nov 29, 2022
18260a2
move slotfills to entries
stevenslack Nov 29, 2022
2e329dc
move src files to inc directory
stevenslack Nov 29, 2022
f8ababb
update entries directory to src
stevenslack Nov 29, 2022
f6a191e
update the composer file to reference the inc directory
stevenslack Nov 29, 2022
32c5862
add jest configuration
stevenslack Nov 29, 2022
96f3431
update eslint config for typescript, update editorconfig to include ts
stevenslack Nov 29, 2022
8a7816b
remove unused assets
stevenslack Nov 30, 2022
b6baf2c
remove jest config in package.json remove babel config
stevenslack Nov 30, 2022
36bb440
refine asset helper functions and examples
stevenslack Nov 30, 2022
407ee8c
correct namespaces and package name in create block
stevenslack Nov 30, 2022
7ae9d45
Merge branch 'feature/wp-scripts' into feature/wp-scripts-develop
stevenslack Nov 30, 2022
26ded45
update dependencies, remove stylelint
stevenslack Nov 30, 2022
1ebb1a6
move load_scripts to assets.php file
stevenslack Nov 30, 2022
8f4657e
ensure css files are output in src directory
stevenslack Dec 1, 2022
d602ba6
Merge branch 'develop' into feature/wp-scripts
stevenslack Dec 1, 2022
ab50a03
fix jest to work with Typescript
stevenslack Dec 1, 2022
5a3261c
phpcs fix
stevenslack Dec 1, 2022
0f2c38f
allow linting and tests to pass without files
stevenslack Dec 1, 2022
5d6f924
update configure file to remove correct files
stevenslack Dec 1, 2022
e486439
Small phpcs adjustment, heading block
srtfisher Dec 1, 2022
e59bf3c
move js entries from src back to entries, add render to block.json
stevenslack Dec 1, 2022
0c3388d
Merge branch 'feature/wp-scripts-SRC' into feature/wp-scripts
stevenslack Dec 1, 2022
8b2796c
add changes for render template in block json for wp 6.1
stevenslack Dec 1, 2022
b21373a
update block configuration for 6.1 scaffolding
stevenslack Dec 1, 2022
98b25a8
use constant for glob includes
stevenslack Dec 1, 2022
5c2d706
Merge branch 'develop' into feature/wp-scripts
stevenslack Dec 1, 2022
7a0b6b0
revert src to entries for deleting front end assets
stevenslack Dec 1, 2022
266ca6d
entry assets to src assets
stevenslack Dec 1, 2022
6773eb0
correct path for phpcs exclusion, update comment
stevenslack Dec 1, 2022
62c8bc9
replace instances of wp-starter-plugin strings
stevenslack Dec 2, 2022
0478007
replace package_name with plugin slug
stevenslack Dec 2, 2022
60271bf
remove ESLint disabling rule for props spreading
stevenslack Dec 2, 2022
3acee62
update block directory README
stevenslack Dec 2, 2022
354174a
update docs for including example CSS
stevenslack Dec 2, 2022
ead4540
update base README
stevenslack Dec 2, 2022
9d9cd54
Merge branch 'develop' into feature/wp-scripts
stevenslack Dec 2, 2022
9ff8d6f
Merge remote-tracking branch 'origin/develop' into feature/wp-scripts
srtfisher Dec 2, 2022
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
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ insert_final_newline = true
indent_style = tab
indent_size = 4

[*.{js,jsx,scss,css,json,yaml,yml,feature,xml}]
[*.{ts,tsx,js,jsx,scss,css,json,yaml,yml,feature,xml}]
indent_style = space
indent_size = 2

Expand Down
10 changes: 7 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
"jest": true,
"node": true
},
"extends": ["airbnb", "airbnb/hooks"],
"parser": "@babel/eslint-parser",
"extends": ["airbnb", "airbnb-typescript", "airbnb/hooks"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaFeatures": {
"globalReturn": true,
"impliedStrict": true,
"jsx": true
},
"project": "./tsconfig.json",
"sourceType": "module"
},
"rules": {
Expand All @@ -26,7 +27,10 @@
"message": "Ternaries must be used instead of || in JSX expressions to avoid the potential for accidental output. Use, for example, {thing1 ? thing1 : thing2}.",
"selector": ":matches(JSXElement, JSXFragment) > JSXExpressionContainer > LogicalExpression[operator='||']"
}
]
],
"react/jsx-props-no-spreading": "off",
"react/react-in-jsx-scope": "off",
"react/require-default-props": "off"
},
"settings": {
"import/resolver": "webpack"
Expand Down
3 changes: 3 additions & 0 deletions .phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
<!-- Disable commenting rules for example code included in the plugin. -->
<rule ref="Squiz.PHP.CommentedOutCode.Found">
<exclude-pattern>src/assets.php</exclude-pattern>
<exclude-pattern>blocks</exclude-pattern>
<exclude-pattern>build</exclude-pattern>
<exclude-pattern>entries</exclude-pattern>
</rule>

<rule ref="Squiz.Commenting.InlineComment">
Expand Down
25 changes: 2 additions & 23 deletions .stylelintrc.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
{
"extends": "stylelint-config-sass-guidelines",
"rules": {
"max-nesting-depth": [
3,
{
"ignoreAtRules": [
"each",
"media",
"supports",
"include"
]
}
],
"selector-class-pattern": [
"^[a-z0-9\\-_]+$",
{
"message":
"Selector should be written in lowercase with hyphens and/or underscores (selector-class-pattern)"
}
],
"selector-no-qualifying-type": null
}
}
"extends": "@alleyinteractive/stylelint-config"
}
40 changes: 29 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ for use when submoduling a plugin. The built branches will include Composer
dependencies and any compiled front-end assets (if using them).

The plugin supports front-end assets which can be enqueued inside
`src/assets.php`. For plugins that don't require front-end assets, the
configuration script below will prompt you to delete the front-end files if you
don't wish to use them.
`src/assets.php` or from within an entry points `index.php` file. For plugins that don't require front-end assets, the configuration script below will prompt you to delete the front-end files if you don't wish to use them.

## Getting Started

Expand Down Expand Up @@ -74,24 +72,44 @@ Run `npm run lint` to run ESLint against all JavaScript files. Linting will also
happen when running development or production builds.

Run `composer test` to run tests against PHPUnit and the PHP code in the plugin.
## Updating Dependencies

To update `@wordpress` dependencies, simply execute:
### The `entries` directory and entry points
All directories created in the `entries` directory can serve as entry points and will be compiled with [@wordpress/scripts](https://github.com/WordPress/gutenberg/blob/trunk/packages/scripts/README.md#scripts) into the `build` directory with an accompanied `index.asset.php` asset map.

#### Enqueuing Entry Points

You can also include an `index.php` file in the entry point directory for enqueueing or registering a script. This file will then be moved to the build directory and will be auto-loaded with the `load_scripts()` function in the `functions.php` file. Alternatively, if a script is to be enqueued elsewhere there are helper functions in the `src/assets.php` file for getting the assets.

### Scaffold a block with `create-block`

Use the `create-block` command to create custom blocks with [`@wordpress/create-block`](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-create-block/) and follow the prompts to generate all the block assets in the `blocks/` directory.
Block registration, script creation, etc will be scaffolded from the `bin/create-block/templates/block/` templates. Run `npm run build` to compile and build the custom block. Blocks are enqueued using the `load_scripts()` function in `src/assets.php`.

### Updating WP Dependencies

Updates the [WordPress dependency packages](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-scripts/#packages-update) used in the project to their latest version.

To update `@wordpress` dependencies to their latest version use the packages-update command:

```sh
npm run packages-update
```

This script provides the following custom options:

- `--dist-tag` – allows specifying a custom dist-tag when updating npm packages. Defaults to `latest`. This is especially useful when using [`@wordpress/dependency-extraction-webpack-plugin`](https://www.npmjs.com/package/@wordpress/dependency-extraction-webpack-plugin). It lets installing the npm dependencies at versions used by the given WordPress major version for local testing, etc. Example:

```sh
npm run update-dependencies WPVERSION
npm run packages-update --dist-tag=wp-WPVERSION`
```

Where `WPVERSION` is the version of WordPress you are targeting. The version
must include both the major and patch version (e.g., `5.9.3`). For example:
must include both the major and minor version (e.g., `6.1`). For example:

```sh
npm run update-dependencies 5.9.3
npm run packages-update --dist-tag=wp-6.1`
```

The versions are drawn from tags on
[wordpress-develop](https://github.com/WordPress/wordpress-develop/tags).
<!--/front-end-->
## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
Expand Down
14 changes: 14 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* This babel config is used by Jest to ensure that tests
* will run correctly in Typescript and JavaScript files.
*
* Jest supports TypeScript, via Babel.
*
* @see https://jestjs.io/docs/getting-started#using-typescript
*/
module.exports = {
presets: [
'@babel/preset-env',
'@wordpress/babel-preset-default',
],
};
9 changes: 0 additions & 9 deletions babel.config.json

This file was deleted.

24 changes: 24 additions & 0 deletions bin/create-block/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const path = require('path');

/**
* Custom Variables and templates for scaffolding blocks.
* @see https://github.com/WordPress/gutenberg/blob/trunk/packages/create-block/docs/external-template.md#external-project-templates
*/
module.exports = {
defaultValues: {
namespace: 'create-wordpress-plugin',
plugin: false,
description: '',
dashicon: 'palmtree',
category: 'widgets',
editorScript: 'file:index.js',
editorStyle: 'file:index.css',
style: ['file:style-index.css'],
render: 'file:render.php',
},
variants: {
static: {},
dynamic: {},
},
blockTemplatesPath: path.join(__dirname, 'templates', 'block'),
};
38 changes: 38 additions & 0 deletions bin/create-block/templates/block/edit.jsx.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
Copy link
Member

Choose a reason for hiding this comment

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

We should split out a ticket to make these .tsx files that are generated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created an issue for this: #90

* Retrieves the translation of text.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-i18n/
*/
import { __ } from '@wordpress/i18n';

/**
* React hook that is used to mark the block wrapper element.
* It provides all the necessary props like the class name.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops
*/
import { useBlockProps } from '@wordpress/block-editor';

/**
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
* Those files can contain any CSS code that gets applied to the editor.
*
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css
*/
import './index.scss';

/**
* The edit function describes the structure of your block in the context of the
* editor. This represents what the editor will render when the block is used.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit
*
* @return {WPElement} Element to render.
*/
export default function Edit() {
return (
<p {...useBlockProps()}>
{ __('Block Title – hello from the editor!', 'create-wordpress-plugin') }
</p>
);
}
42 changes: 42 additions & 0 deletions bin/create-block/templates/block/index.js.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Registers a new block provided a unique name and an object defining its behavior.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/
*/
import { registerBlockType } from '@wordpress/blocks';

/**
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
* All files containing `style` keyword are bundled together. The code used
* gets applied both to the front of your site and to the editor.
*
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css
*/
import './style.scss';

/**
* Internal dependencies
*/
import edit from './edit';
{{#isStaticVariant}}
import save from './save';
{{/isStaticVariant}}
import metadata from './block.json';
{{#isAnotherVariant}}
console.log('it worked');
{{/isAnotherVariant}}

/**
* Every block starts by registering a new block type definition.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/
*/
registerBlockType(
metadata.name,
{
edit,
{{#isStaticVariant}}
save,
{{/isStaticVariant}}
},
);
22 changes: 22 additions & 0 deletions bin/create-block/templates/block/index.php.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Block Name: {{title}}.
*
* @package create-wordpress-plugin
*/

/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function {{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init() {
// Register the block by passing the location of block.json.
register_block_type(
__DIR__
);

}
add_action( 'init', '{{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init' );
13 changes: 13 additions & 0 deletions bin/create-block/templates/block/index.scss.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* The following styles get applied inside the editor only.
*
* All imported CSS files are bundled into one chunk named after the entry point,
* which defaults to index.js, and thus the file created becomes index.css.
* This is for styles used only in the editor.
*
* Replace them with your own styles or remove the file completely.
*/

.wp-block-{{namespace}}-{{slug}} {
border: 1px dotted #f00;
}
17 changes: 17 additions & 0 deletions bin/create-block/templates/block/render.php.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{{#isDynamicVariant}}
<?php
/**
* All of the parameters passed to the function where this file is being required are accessible in this scope:
*
* @param array $attributes The array of attributes for this block.
* @param string $content Rendered block output. ie. <InnerBlocks.Content />.
* @param WP_Block $block_instance The instance of the WP_Block class that represents the block being rendered.
*
* @package create-wordpress-plugin
*/

?>
<p <?php echo wp_kses_data( get_block_wrapper_attributes() ); ?>>
<?php esc_html_e( '{{title}} – hello from a dynamic block!', 'create-wordpress-plugin' ); ?>
</p>
{{/isDynamicVariant}}
26 changes: 26 additions & 0 deletions bin/create-block/templates/block/save.jsx.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{{#isStaticVariant}}
/**
* React hook that is used to mark the block wrapper element.
* It provides all the necessary props like the class name.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops
*/
import { useBlockProps } from '@wordpress/block-editor';

/**
* The save function defines the way in which the different attributes should
* be combined into the final markup, which is then serialized by the block
* editor into `post_content`.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#save
*
* @return {WPElement} Element to render.
*/
export default function save() {
return (
<p { ...useBlockProps.save() }>
{ '{{title}} – hello from the saved content!' }
</p>
);
}
{{/isStaticVariant}}
18 changes: 18 additions & 0 deletions bin/create-block/templates/block/style.scss.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* The following styles get applied both on the front of your site
* and in the editor.
*
* Imported style.css file(s) (applies to SASS and SCSS extensions)
* get bundled into one style-index.css file that is meant to be
* used both on the front-end and in the editor.
*
* Replace them with your own styles or remove the file completely.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#block-styles
*/

.wp-block-{{namespace}}-{{slug}} {
background-color: #21759b;
color: #fff;
padding: 2px;
}
Loading