This is designed to be a minimal, fast build environment for making WordPress Gutenberg blocks. Our priorities are to:
- Reduce time to development by making block generation and enqueuing fast
- Have a build that requires minimal enqueuing and minimal file size
- Clone into
pluginsfolder of your WordPress instance - Activate
Guty blocks 2in your WordPress admin - In the terminal, go to the plugin folder
guty-blocks-2 - Run
npm install
npm run build will run the webpack build once.
npm run watch will run the webpack watcher and build when files are changed.
- In the terminal, go to the plugin folder
guty-blocks-2 - Run
node generate - After you follow the prompts your block source files will be in
assets/src/{block name} - Make sure you either run
npm run buildornpm run watch - Enjoy your block!
You will need to delete the block folder and remove the import statements from index.js (and index.view.js if it included a view script).
Running npm run build:prod will build files as usual but will include js minification. A todo item for me is to include CSS minification as well.
-
Instead of creating a php enqueuing function for each individual block, we import individual blocks into one
index.jsfile and onestyle.cssfile. -
Anything inside of a
*.editor.cssfile is inlined in the block using webpack style-loader. It is never built into the dist folder, and it will not be seen on the front end view of the page. Anything in*.view.cssfiles will be included in the single css file. This still results in the intended effect of editor styles overwriting view styles, and view styles being in both the editor and the view of the page. -
React and ReactDOM are treated as external dependencies, meaning that they can be referenced in webpack builds, but are not actually included in the index.js build. This is so we don't duplicate React when we run the blocks. As a result file sizes for these built blocks are much smaller.
Sometimes there is a need to run a script on the viewing of a page. The build block with view script sets up a {blockname}.view.js file that will only be run on the front end of the site. Note that this only provides a place to run scripts that will run in the view, and the actual searching of the dom and attaching functionality is completely written by you.
- Fixed lodash dependency issue
- Added
npm run build:prodto allow for js minification if desired
- Removed some erroneous lines in
index.jsandindex.view.jsthat were causing build errors
- Include RichText
- Include support for align
- Don't include default styles ins css and don't include css className in edit or view
- Updated readme regarding view blocks
- Adding generate-template-files (? not sure why it wasn't installing before?)
Thanks to Robert a.k.a codeBelt for creating generate-template-files which was a great way to set up block template generation!