npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gutenblock

v1.0.2

Published

- [Install](#install) - [Comparison with competition](#comparison-with-other-tooling) - [Future Plans](#future-plans) - [Usage](#usage) - [Creating a Block](#creating-a-block)

Downloads

12

Readme

Contents

Install

npm install gutenblock -g

This is a Gutenberg plugin creator + reusable inspector components with hot loading and code splits built in.

Quickstart

If you have never done WordPress development, getting started couldn't be easier.

When you add docker on the end of the watch command, it will bring up WordPress for you. Simply create an account, install the Gutenberg plugin, and activate the blocks plugin. You're all set.

Comparison with other tooling

Currently, there is only one tool out there to help create blocks (that I have found so far). It's called Create Guten Block. This library was inspired by it. I've added what I consider to be good defaults that everyone would want when creating blocks. These features are not included in other libraries by default:

  • Auto Block registration
  • Helper utlities
  • Automatic code splitting
  • Hot reloading (without page reload)
  • Custom webpack config without ejection

Auto Block registration

No need to call registerBlockType for WordPress. Our loader does this for you.

Helper utilities

Currently, when editing things in gutenberg you make components like this:

const { RichText } = wp.editor;

export default ({ setAttributes, attributes }) => (
  <div>
    <RichText
      tagName="h1"
      value={attributes.title}
      placeholder="Title"
      onChange={title => setAttributes({ title })}
    />
  </div>
);

With Gutenblock, we created a higher order context that is loaded into all edit components. This means you can import our abstracted inputs:

import { RichText } from 'gutenblock-controls';

const Edit = () => (
  <div>
    <RichText name="description" />
  </div>
);

We've included a Select MediaSelect Input Inspector Repeat and other form fields to help you build blocks faster. A repeat component will handle the hard work of letting users add infinite items to an array of form fields, replacing items, and deleting them.

The name field is the key in your gutenberg attributes defined in block.js. You can create your own inputs that connect and get access to setAttributes and attributes, no longer needing to pass them all over in your components. See the example

Code splitting

If you have many blocks, you don't want Gutenberg to load all of that JS when it initializes. With this plugin, your edit blocks will only be loaded in once they are dragged out to the canvas.

Hot reloading

Every edit block is hooked into react-hot-loader with our loader so that updates won't need a full page load. Full reloads can make development much slower when Gutenberg has a lot of content on the page at once.

Custom Webpack

Add a gutenblock.config.js file in your blocks folder. It looks like this:

const path = require('path');

module.exports = webpack => ({
  //customize gutenblock options if needed
  gutenblock: {
    devHost: 'localhost',
    devPort: 8080,
    //build asset output path relative to the plugin directory
    outputPath: '/test',
    //when building the plugin, gutenblock will default to the folder name inside wp-content, if you have a different wp-content folder you can change it here
    publicPath: `/app/plugins/blocks/test/`,
  },
  resolve: {
    alias: {
      shared: path.resolve(__dirname, '../src/shared'),
    },
  },
  plugins: [new webpack.EnvironmentPlugin(['NODE_ENV'])],
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [require.resolve('style-loader'), require.resolve('css-loader')],
      },
    ],
  },
});

The configuration is the exact same as webpack with one extra piece: pass babelOptions with plugins and presets like a babelrc has to customize the babel loader.

If you choose to extend the configuration, down the road a future webpack release may require you to make changes and update your configuration. If you do not extend anything, you'll never have to update any configuration in order to upgrade gutenblock!

Future plans

  • Automatic i18n
  • Complicated examples (tabs component, loading in data from WordPress)
  • Test coverage
  • Batch updates when updating nested tabs that cause lots of rerenders in Gutenberg

Usage

gutenblock init will scaffold out a WordPress plugin for you.

gutenblock watch inside the folder will start development mode. Copy the blocks folder into your plugins directory, or use docker

gutenblock build production build the plugin. After, edit line 35 of yourplugin/src/init.php to point to the production assets. All set to publish!

Creating a block

Inside src you will create blocks matching the example one.

All blocks need a block.js and edit.js.

./src/paragraph/block.js

//Optionally use a save block for static rendering on the WordPress frontend

import Save from './save';

const Block = {
  title: 'Paragraph',
  icon: 'shield-alt',
  category: 'common',
  attributes: {
    body: {
      type: 'string',
    },
  },
  save: Save,
};

./src/paragraph/edit.js

import { RichText } from 'gutenblock-controls';

const Edit = () => (
  <RichText tagName="p" name="body" style={{ color: 'black' }} />
);

./src/paragraph/save.js

export default ({ attributes }) => <p>{attributes.body}</p>;

Side note: We don't use save blocks at Crossfield. This is because we fetch WordPress pages and posts via the api and render the blocks using a custom react frontend. Sadly, if you use save blocks, they will not be code split. This is a limitation of the gutenberg editor not supporting awaiting to render the save method.

No registering blocks, importing them into a root folder. It's all done for you.

Now we can run gutenblock watch inside our plugin folder. Inside WordPress the components will hot reload as you edit, thanks to react-hot-loader

You can read more about the Block API