Skip to content

Commit 70660d3

Browse files
authored
Merge pull request #82 from alleyinteractive/feature/wp-scripts
Add support for @wordpress/scripts, block.json and create block
2 parents fec28ff + 9ff8d6f commit 70660d3

30 files changed

+8734
-17379
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ insert_final_newline = true
88
indent_style = tab
99
indent_size = 4
1010

11-
[*.{js,jsx,scss,css,json,yaml,yml,feature,xml}]
11+
[*.{ts,tsx,js,jsx,scss,css,json,yaml,yml,feature,xml}]
1212
indent_style = space
1313
indent_size = 2
1414

.eslintrc.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
"jest": true,
66
"node": true
77
},
8-
"extends": ["airbnb", "airbnb/hooks"],
9-
"parser": "@babel/eslint-parser",
8+
"extends": ["airbnb", "airbnb-typescript", "airbnb/hooks"],
9+
"parser": "@typescript-eslint/parser",
1010
"parserOptions": {
1111
"ecmaFeatures": {
1212
"globalReturn": true,
1313
"impliedStrict": true,
1414
"jsx": true
1515
},
16+
"project": "./tsconfig.json",
1617
"sourceType": "module"
1718
},
1819
"rules": {
@@ -26,7 +27,10 @@
2627
"message": "Ternaries must be used instead of || in JSX expressions to avoid the potential for accidental output. Use, for example, {thing1 ? thing1 : thing2}.",
2728
"selector": ":matches(JSXElement, JSXFragment) > JSXExpressionContainer > LogicalExpression[operator='||']"
2829
}
29-
]
30+
],
31+
"react/jsx-props-no-spreading": "off",
32+
"react/react-in-jsx-scope": "off",
33+
"react/require-default-props": "off"
3034
},
3135
"settings": {
3236
"import/resolver": "webpack"

.phpcs.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
<!-- Disable commenting rules for example code included in the plugin. -->
4747
<rule ref="Squiz.PHP.CommentedOutCode.Found">
4848
<exclude-pattern>src/assets.php</exclude-pattern>
49+
<exclude-pattern>blocks</exclude-pattern>
50+
<exclude-pattern>build</exclude-pattern>
51+
<exclude-pattern>entries</exclude-pattern>
4952
</rule>
5053

5154
<rule ref="Squiz.Commenting.InlineComment">

.stylelintrc.json

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,3 @@
11
{
2-
"extends": "stylelint-config-sass-guidelines",
3-
"rules": {
4-
"max-nesting-depth": [
5-
3,
6-
{
7-
"ignoreAtRules": [
8-
"each",
9-
"media",
10-
"supports",
11-
"include"
12-
]
13-
}
14-
],
15-
"selector-class-pattern": [
16-
"^[a-z0-9\\-_]+$",
17-
{
18-
"message":
19-
"Selector should be written in lowercase with hyphens and/or underscores (selector-class-pattern)"
20-
}
21-
],
22-
"selector-no-qualifying-type": null
23-
}
24-
}
2+
"extends": "@alleyinteractive/stylelint-config"
3+
}

README.md

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ for use when submoduling a plugin. The built branches will include Composer
1111
dependencies and any compiled front-end assets (if using them).
1212

1313
The plugin supports front-end assets which can be enqueued inside
14-
`src/assets.php`. For plugins that don't require front-end assets, the
15-
configuration script below will prompt you to delete the front-end files if you
16-
don't wish to use them.
14+
`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.
1715

1816
## Getting Started
1917

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

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

79-
To update `@wordpress` dependencies, simply execute:
76+
### The `entries` directory and entry points
77+
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.
78+
79+
#### Enqueuing Entry Points
80+
81+
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.
82+
83+
### Scaffold a block with `create-block`
84+
85+
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.
86+
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`.
87+
88+
### Updating WP Dependencies
89+
90+
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.
91+
92+
To update `@wordpress` dependencies to their latest version use the packages-update command:
93+
94+
```sh
95+
npm run packages-update
96+
```
97+
98+
This script provides the following custom options:
99+
100+
- `--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:
80101

81102
```sh
82-
npm run update-dependencies WPVERSION
103+
npm run packages-update --dist-tag=wp-WPVERSION`
83104
```
84105

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

88109
```sh
89-
npm run update-dependencies 5.9.3
110+
npm run packages-update --dist-tag=wp-6.1`
90111
```
91112

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

97115
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

babel.config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* This babel config is used by Jest to ensure that tests
3+
* will run correctly in Typescript and JavaScript files.
4+
*
5+
* Jest supports TypeScript, via Babel.
6+
*
7+
* @see https://jestjs.io/docs/getting-started#using-typescript
8+
*/
9+
module.exports = {
10+
presets: [
11+
'@babel/preset-env',
12+
'@wordpress/babel-preset-default',
13+
],
14+
};

babel.config.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

bin/create-block/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const path = require('path');
2+
3+
/**
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
6+
*/
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.js',
15+
editorStyle: 'file:index.css',
16+
style: ['file:style-index.css'],
17+
render: 'file:render.php',
18+
},
19+
variants: {
20+
static: {},
21+
dynamic: {},
22+
},
23+
blockTemplatesPath: path.join(__dirname, 'templates', 'block'),
24+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Retrieves the translation of text.
3+
*
4+
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-i18n/
5+
*/
6+
import { __ } from '@wordpress/i18n';
7+
8+
/**
9+
* React hook that is used to mark the block wrapper element.
10+
* It provides all the necessary props like the class name.
11+
*
12+
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops
13+
*/
14+
import { useBlockProps } from '@wordpress/block-editor';
15+
16+
/**
17+
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
18+
* Those files can contain any CSS code that gets applied to the editor.
19+
*
20+
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css
21+
*/
22+
import './index.scss';
23+
24+
/**
25+
* The edit function describes the structure of your block in the context of the
26+
* editor. This represents what the editor will render when the block is used.
27+
*
28+
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit
29+
*
30+
* @return {WPElement} Element to render.
31+
*/
32+
export default function Edit() {
33+
return (
34+
<p {...useBlockProps()}>
35+
{ __('Block Title – hello from the editor!', 'create-wordpress-plugin') }
36+
</p>
37+
);
38+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Registers a new block provided a unique name and an object defining its behavior.
3+
*
4+
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/
5+
*/
6+
import { registerBlockType } from '@wordpress/blocks';
7+
8+
/**
9+
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
10+
* All files containing `style` keyword are bundled together. The code used
11+
* gets applied both to the front of your site and to the editor.
12+
*
13+
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css
14+
*/
15+
import './style.scss';
16+
17+
/**
18+
* Internal dependencies
19+
*/
20+
import edit from './edit';
21+
{{#isStaticVariant}}
22+
import save from './save';
23+
{{/isStaticVariant}}
24+
import metadata from './block.json';
25+
{{#isAnotherVariant}}
26+
console.log('it worked');
27+
{{/isAnotherVariant}}
28+
29+
/**
30+
* Every block starts by registering a new block type definition.
31+
*
32+
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/
33+
*/
34+
registerBlockType(
35+
metadata.name,
36+
{
37+
edit,
38+
{{#isStaticVariant}}
39+
save,
40+
{{/isStaticVariant}}
41+
},
42+
);

0 commit comments

Comments
 (0)