Skip to content

enqueuing a block via block.json's editorScript property doesn't work in a theme #40744

@jakeparis

Description

@jakeparis

Description

In a theme I'm building, I am attempting to use the editorScript key in block.json to automatically enqueue my block's assets.

However, that is resulting in messed up asset urls like: https://example.loc/wp-content/plugins/shared/httpd/htdocs/wp-content/themes/jmc/build/blocks/myblock/index.js, where the script path is concatenated after the plugins url.

After some investigation, it seems the problem is that register_block_type() function uses register_block_script_handle(), which, in wp-includes/blocks.php:112 is calling plugins_url( $script_path, $metadata['file'] );

Is there really no way to use this method to queue up blocks in a theme based on a block.json?

Step-by-step reproduction instructions

Here's my block.json.

block.json

{
  ...
	"editorScript": "file:./index.js",
	"editorStyle": "file:./index.css",
	"viewScript": "file:./public.js",
	"style": "file:./public.css"
 }

I'm loading all the block.json files in a loop like so:

registration.php

foreach ( glob( get_stylesheet_directory() . "/build/blocks/*/block.json" ) as $file ) {
    register_block_type( $file );
}

My src directory is structured like so:

themedir
| - src
| - | - blocks
| - | - | - jake
| - | - | - | - block.json
| - | - | - | - index.js
| - | - | - | - index.scss
                 ...
| - build
| - | - blocks
| - | - | - jake
| - | - | - | - block.json
| - | - | - | - index.js
| - | - | - | - index.css
                 ...

Screenshots, screen recording, code snippet

No response

Environment info

No response

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Feature] Block APIAPI that allows to express the block paradigm.[Feature] ThemesQuestions or issues with incorporating or styling blocks in a theme.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions