Skip to content

Create Block: Allow variants to define paths to plugin and block files. #67768

Description

@ryanwelcher

What problem does this address?

Currently when defining a custom template, here is a single directory used for the plugin files and another for the block files. Prior to the addition of variants, this was fine but given that a single template could have any number of variations it mean that potentially the files being used will require many nested mustache conditionals to render correctly.

For example, I am using a custom template that has a number of variants.

The plugin variant renders a very different set of files that doesn't include any block files. This requires wrapping all of the block files in a check to make sure the plugin variant is NOT active and then render the inner contents:

{{^isPluginVariant}}
/**
 * Registers a new block provided a unique name and an object defining its behavior.
 *
 * @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/
 */
import { registerBlockType } from '@wordpress/blocks';

/**
 * Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
 * All files containing `style` keyword are bundled together. The code used
 * gets applied both to the front of your site and to the editor.
 *
 * @see https://www.npmjs.com/package/@wordpress/scripts#using-css
 */
import './style.scss';

/**
 * Internal dependencies
 */
import Edit from './edit';
{{#isStaticVariant}}
import save from './save';
{{/isStaticVariant}}
import metadata from './block.json';

/**
 * Every block starts by registering a new block type definition.
 *
 * @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/
 */
registerBlockType( metadata.name, {
	/**
	 * @see ./edit.js
	 */
	edit: Edit,
	{{#isStaticVariant}}

	/**
	 * @see ./save.js
	 */
	save,
	{{/isStaticVariant}}
} );
{{/isPluginVariant}}

This becomes more complicated as the number of variants increases and leads to a very brittle developer experience.

What is your proposed solution?

I propose that we add the ability for variants to optionally define the path to block and plugin files.

{
    variants: {
        dynamic: {
            render: 'file:./render.php',
             blockFiles: join( __dirname, 'path/to/files' ),
             pluginFiles: join( __dirname, 'path/to/files' )
	},
},

With an optional set of files for each variant the template files become simpler and much easier to read and maintain.

Activity

  1. added
    [Type] Enhancement
    [Tool] Create Block
    Developer Experience
    on Dec 10, 2024
  2. gziolo commented on Dec 10, 2024

    @gziolo
    Member

    This proposal makes perfect sense. Would it be useful to allow overriding with null or undefined which would translate to skipping the part of scaffolding related to plugin or block?

  3. ryanwelcher commented on Dec 10, 2024

    @ryanwelcher
    ContributorAuthor

    Would it be useful to allow overriding with null or undefined which would translate to skipping the part of scaffolding related to plugin or block?

    I think that makes sense, there may be variants that don't do anything expect add a JS file for example. If we add that now it, it will be easier to accommodate the edge cases as we move forward.

    I'll get started on a PR for this.

  4. self-assigned this
    on Dec 10, 2024
  5. added
    [Status] In Progress
    on Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Developer Experience[Status] In Progress[Tool] Create Block[Type] Enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions