Table of Contents
The create-block package is a fantastic time-saving tool that allows you to quickly and consistently create custom blocks for your projects with a single command. At its core, it really only does one thing. It receives some values, processes those values, and then generates files with those values inserted into them.
In fact, the create-block tool relies on a template to tell it where to find the files it needs, where inside them it should insert the processed values, and to define any default values needed.
A default template provides a standard configuration for creating simple blocks. The real power of create-block becomes evident when you start creating external project templates.
External project templates provide a level of control that is not otherwise possible via interactive mode or the available command line options by allowing fine grain control over all aspects of the scaffolded files.
Templates can be passed to create-block via the --template
flag and the templates can be hosted on npmjs.com as is the case with the Getting Started guide for the new Interactivity API:
npx @wordpress/create-block@latest my-first-interactive-block --template @wordpress/create-block-interactive-template
Templates can also be referenced locally by passing the path to the directory where the template is located:
npx @wordpress/create-block@latest custom-template-block -–template ./path/to/files
This is a great way of not only creating and testing templates, but also leveraging them in projects where the code cannot be shared with the overall community.
Getting set up
To work with the create-block tool, you’ll need to have Node.js installed. For a primer on that, refer to the official documentation on setting up the Node.js development tools.
Once you have Node installed, clone the accompanying repository for this article:
git clone https://github.com/wptrainingteam/external-project-template.git
Then move into the directory:
cd external-project-template
The contents of the project should like like this: