-
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
Docs: Fundamentals of Block Development - File structure of a block #56551
Merged
juanmaguitar
merged 6 commits into
trunk
from
fundamentals-block-development/file-structure-of-a-block-2
Nov 28, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
63dda71
Add file structure documentation for custom blocks
juanmaguitar 2acb945
Update docs/getting-started/fundamentals-block-development/file-struc…
juanmaguitar 6751926
Removed not clear sentence
juanmaguitar cfe47f6
Merge branch 'fundamentals-block-development/file-structure-of-a-bloc…
juanmaguitar 36485c2
Merge branch 'trunk' into fundamentals-block-development/file-structu…
juanmaguitar d70b72a
Merge branch 'trunk' into fundamentals-block-development/file-structu…
juanmaguitar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
docs/getting-started/fundamentals-block-development/file-structure-of-a-block.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# File structure of a block | ||
|
||
It is recommended to **register blocks within plugins** to ensure they stay available when a theme gets switched. With the `create-block` tool you can quickly scaffold the structure of the files required to create a plugin that registers a block. | ||
|
||
The files generated by this tool are a good reference of the files that can be involved in the definition and registration of a block. | ||
|
||
[![Open File Structure of a Block Diagram in excalidraw](https://developer.wordpress.org/files/2023/11/file-structure-block.png)](https://excalidraw.com/#json=YYpeR-kY1ZMhFKVZxGhMi,mVZewfwNAh_oL-7bj4gmdw "Open File Structure of a Block Diagram in excalidraw") | ||
|
||
### `<plugin-file>.php` | ||
|
||
A block is usually added to the block editor using a WordPress plugin. In the main PHP file of the plugin the block is usually registered on the server side. | ||
|
||
<div class="callout callout-info"> | ||
For more on creating a WordPress plugin see <a href="https://developer.wordpress.org/plugins/plugin-basics/">Plugin Basics</a>, and <a href="https://developer.wordpress.org/plugins/plugin-basics/header-requirements/">Plugin Header requirements</a> for explanation and additional fields you can include in your plugin header. | ||
</div> | ||
|
||
### `package.json` | ||
|
||
[`package.json`](https://docs.npmjs.com/cli/v10/configuring-npm/package-json) is a configuration file for a Node.js project. In this file you define the NPM dependencies of the block and the scripts used for local work. | ||
|
||
### `src` folder | ||
|
||
In a standard project you'll place your block files in the `src` folder. By default, the build process with `wp-scripts `will take files from this folder and will generate the bundled files in the `build` folder. | ||
|
||
### `block.json` | ||
|
||
This file contains the metadata of the block, and it's used to simplify the definition and registration of the block both in the client and on the server. | ||
|
||
Among other data it provides properties to define the paths of the files involved in the block's behaviour, output and style. If there's a build process involved, this `block.json` along with the generated files are placed into a destination folder (usually the `build` folder) so the paths provided target to the bundled versions of these files. | ||
|
||
The most relevant properties that can be defined in a `block.json` to set the files involved in the block's behaviour, output or style are: | ||
- The `editorScript` property, usually set with the path of a bundled `index.js` file (output build from `src/index.js`). | ||
- The `style` property, usually set with the path of a bundled `style-index.css` file (output build from `src/style.(css|scss|sass)`). | ||
- The `editorStyle` property, usually set with the path of a bundled `index.css` (output build from `src/editor.(css|scss|sass)`). | ||
- The `render` property, usually set with the path of a bundled `render.php` (output copied from `src/render.php`). | ||
- The `viewScript` property, usually set with the path of a bundled `view.js` (output copied from `src/view.php`). | ||
|
||
[![Open Build Output Diagram in excalidraw](https://developer.wordpress.org/files/2023/11/file-structure-build-output.png)](https://excalidraw.com/#json=c22LROgcG4JkD-7SkuE-N,rQW_ViJBq0Yk3qhCgqD6zQ "Open Build Output Diagram in excalidraw") | ||
|
||
### `index.js` | ||
|
||
The `index.js` file (or any other file defined in the `editorScript` property of `block.json`) is the entry point file for javascript that should only get loaded in the editor. It is responsible for calling the `registerBlockType` function to register the block on the client. In a standard structure it imports the `edit.js` and `save.js` files to get functions required in block registration. | ||
|
||
### `edit.js` | ||
|
||
The `edit.js` commonly gets used to contain the React component that gets used in the editor for our block. It usually exports a single component that then gets passed to the `edit` property of the `registerBlockType` function in the `index.js` file. | ||
|
||
### `save.js` | ||
|
||
The `save.js` is similar to the `edit.js` file in that it exports a single React component. This component generates the static HTML markup that gets saved to the Database. | ||
|
||
### `style.(css|scss|sass)` | ||
|
||
A `style` file with any of the extensions `.css`, `.scss` or `.sass`, contains the styles of the block that will be loaded in both the editor and the frontend. In the build process this file is converted into `style-index.css` which is usually defined at `style` property in `block.json` | ||
|
||
<div class="callout callout-info"> | ||
The webpack config used internally by <code>wp-scripts</code> includes a <a href="https://webpack.js.org/loaders/css-loader/">css-loader</a> chained with <a herf="https://webpack.js.org/loaders/postcss-loader/">postcss-loader</a> and <a href="https://webpack.js.org/loaders/sass-loader/">sass-loader</a> that allows it to process CSS, SASS or SCSS files. Check <a href="https://developer.wordpress.org/block-editor/reference-guides/packages/packages-scripts/#default-webpack-config">Default webpack config</a> for more info | ||
</div> | ||
|
||
|
||
### `editor.(css|scss|sass)` | ||
|
||
An `editor` file with any of the extensions `.css`, `.scss` or `.sass`, contains the additional styles applied to the block only in the editor’s context. In the build process this file is converted into `index.css` which is usually defined at `editorStyle` property in `block.json` | ||
|
||
### `render.php` | ||
|
||
The `render.php` file (or any other file defined in the `render` property of `block.json`) defines the server side process that returns the markup for the block when there is a request from the frontend. If this file is defined, it will take precedence over any other ways to render the block's markup for the frontend. | ||
|
||
### `view.js` | ||
|
||
The `view.js` file (or any other file defined in the `viewScript` property of `block.json`) will be loaded in the front-end when the block is displayed. | ||
|
||
### `build` folder | ||
|
||
In a standard project, the `build` folder contains the generated files in the build process triggered by the `build` or `start` commands of `wp-scripts`. | ||
|
||
<div class="callout callout-tip"> | ||
You can use <code>webpack-src-dir</code> and <code>output-path</code> option of <code>wp-scripts</code> build commands to <a href="https://developer.wordpress.org/block-editor/reference-guides/packages/packages-scripts/#automatic-block-json-detection-and-the-source-code-directory">customize the entry and output points</a> | ||
</div> | ||
|
||
## Additional resources | ||
|
||
- [Metadata in block.json](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/) | ||
- [`wp-scripts build`](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-scripts/#build) | ||
- [`wp-scripts start`](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-scripts/#start) | ||
- [How webpack and WordPress packages interact](https://developer.wordpress.org/news/2023/04/how-webpack-and-wordpress-packages-interact/) | Developer Blog |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
save
is a standard JavaScript function. It can be created as a component but under the hood only the render method is called as a JS function. Looking at scaffolded files, you'll notice thatsave
is lowercase and React components are written in uppercase.This may be pedantic but it's worth mentioning.