Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scripts: Add build-blocks-manifest command #65866

Merged
merged 8 commits into from
Oct 17, 2024

Conversation

mreishus
Copy link
Contributor

@mreishus mreishus commented Oct 3, 2024

What?

Adds a new wp-scripts build-blocks-manifest command.

Why?

To help plugin authors take advantage of the new function wp_register_block_metadata_collection() offered in https://core.trac.wordpress.org/changeset/59132 . It requires all block.json files to be compiled into one php manifest file, which this command does.

How?

Looks for block.json files, keys them off of the parent directory they belong to, then passes to json2php (new dependency alert) to write to a php file.

Testing Instructions

  • In the Gutenberg repo: Run npm run build:blocks-manifest and inspect the build/blocks-manifest.php file.
  • As wp-scripts: I'm not sure, this is my first time working on this, but I hope it will 'just work' due to the PR following the conventions.

Testing Instructions for Keyboard

Screenshots or screencast

@mreishus
Copy link
Contributor Author

mreishus commented Oct 3, 2024

Tag @felixarntz ^
Is this what you had in mind? This is my first time working in wp-scripts.

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mreishus Thanks for jumping on this to create the PR, it looks great to me so far. Kudos for already adding such extensive documentation! ❤️

I'm personally not the best candidate to review the JS command implementation, would be great to get reviews from people that were previously more involved in @wordpress/scripts. Maybe @gziolo you can help here?

const { getArgFromCLI } = require( '../utils' );

// Set default paths
const defaultInputDir = 'build';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the default be build or src? I can see why you chose build, but a counter argument would be that this means you always have to run npm run build before you run npm run build:blocks-manifest.

To my knowledge, the block.json files are copied as is from src to build, so in that case I think src is a better default. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think I was influenced from my recent work in the WooCommerce plugin. That repo generates block.json files as a part of its main build process that are in directories named differently than their source values in assets/. Over there, the only way I could generate the right information was to pull from build/ while also writing to build/. That may be atypical though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was about to move it to src here, but I found that in order to keep the npm run build:blocks-manifest command working in the root, I'd have to look through 4 src directories:

./packages/blocks/src/
./packages/widgets/src/
./packages/block-library/src/
./packages/edit-widgets/src/

We don't have a way to specify multiple input dirs. It may be the build->build after is a useful pattern for monorepos?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is interesting. When you use wp-scripts build then block.json files get copied from src to build and sometimes they even can get modified:

transform( content, absoluteFrom ) {
const convertExtension = ( path ) => {
return path.replace( /\.m?(j|t)sx?$/, '.js' );
};
if ( basename( absoluteFrom ) === 'block.json' ) {
const blockJson = JSON.parse( content.toString() );
[
getBlockJsonScriptFields( blockJson ),
getBlockJsonModuleFields( blockJson ),
].forEach( ( fields ) => {
if ( fields ) {
for ( const [
key,
value,
] of Object.entries( fields ) ) {
if ( Array.isArray( value ) ) {
blockJson[ key ] =
value.map( convertExtension );
} else if (
typeof value === 'string'
) {
blockJson[ key ] =
convertExtension( value );
}
}
}
} );
return JSON.stringify( blockJson, null, 2 );
}

In effect, I think it's a valid approach to source the block.json from the build folder to put them in the build folder. In the long run, these probably should be an option on the wp-scripts build --use-manifest. However, we can get there in steps.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That all makes sense, let's keep using build for now.

@mreishus mreishus marked this pull request as ready for review October 7, 2024 14:36
Copy link

github-actions bot commented Oct 7, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: mreishus <mreishus@git.wordpress.org>
Co-authored-by: gziolo <gziolo@git.wordpress.org>
Co-authored-by: felixarntz <flixos90@git.wordpress.org>
Co-authored-by: sirreal <jonsurrell@git.wordpress.org>
Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@gziolo gziolo added [Tool] WP Scripts /packages/scripts [Type] New API New API to be used by plugin developers or package users. labels Oct 8, 2024
package.json Outdated
@@ -272,6 +272,7 @@
"scripts": {
"build": "npm run build:packages && wp-scripts build",
"build:analyze-bundles": "npm run build -- --webpack-bundle-analyzer",
"build:blocks-manifest": "wp-scripts build-blocks-manifest",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could go next in another PR to make it all simpler to land 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be removed for now as it isn’t connected anywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, removed here: e24747e

@gziolo gziolo requested review from sirreal and jsnajdr October 8, 2024 12:39
Copy link
Member

@sirreal sirreal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks reasonable to me. It would be good to add a CHANGELOG entry.

A basic test would be nice, it could be a snapshot of the expected result. DEWP has some testing like this that could serve as inspiration:

https://github.com/WordPress/gutenberg/blob/trunk/packages/dependency-extraction-webpack-plugin/test/build.js

Copy link
Member

@sirreal sirreal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this out in Gutenberg and it seems to work well. I did list a couple nice-to-haves in my previous review but nothing blocking.

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Giving this a 2nd approval, pending the minor feedback. Thanks @mreishus!

@mreishus mreishus force-pushed the add/blocks-metadata branch from f52f2f9 to 2693402 Compare October 9, 2024 19:08
@mreishus
Copy link
Contributor Author

mreishus commented Oct 9, 2024

  • added snapshot test 389b128
  • rebased and solved lockfile conflict (by dropping the npm install commit and making a new one)
  • added changelog 840742a

@mreishus mreishus requested a review from sirreal October 9, 2024 20:53
const blockJsonFiles = glob( './**/block.json', {
cwd: path.resolve( process.cwd(), inputDir ),
absolute: true,
} );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script should first check if the build directory exists and abort with error if it doesn't.

It makes sense to run this script only if the blocks were already built and the build directory was created. If you forgot to run the build first, you should be told about it, instead of silently creating an empty manifest file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Check e673f31

@mreishus mreishus requested a review from jsnajdr October 10, 2024 21:51
@mreishus mreishus force-pushed the add/blocks-metadata branch from e673f31 to a92f4a1 Compare October 15, 2024 13:36
@felixarntz
Copy link
Member

@jsnajdr Could you please take another look at this? Ideally, we could merge this before Thursday, as I am currently working on a dev note post for the change for WordPress 6.7, and it would make sense to have this available to reference for developers already in that post.

See https://make.wordpress.org/core/?p=115864&preview=1&_ppp=848634d99d for the public preview of that dev note. @gziolo @mreishus Could you please also review my draft and let me know your feedback?

@gziolo
Copy link
Member

gziolo commented Oct 16, 2024

Ideally, we could merge this before Thursday, as I am currently working on a dev note post for the change for WordPress 6.7, and it would make sense to have this available to reference for developers already in that post.

Merging this PR is not enough to publish it to npm. The next package release will happen next week on Wednesday. Let me know if you would like to publish it separately on demand earlier.

Could you please also review my draft and let me know your feedback?

I'm checking the draft next.

@gziolo
Copy link
Member

gziolo commented Oct 16, 2024

See https://make.wordpress.org/core/?p=115864&preview=1&_ppp=848634d99d for the public preview of that dev note.

I'm not sure what is the best place for feedback. I can start here and move it elsewhere upon request:

  • It needs to be clarified wp_register_block_metadata_collection() is an additional step on top of the existing registration process. In effect, I would include the snippet illustrating how the blocks get registered using:
    wp_register_block_metadata_collection(
        WP_PLUGIN_DIR . '/my-block-library/build',
        WP_PLUGIN_DIR . '/my-block-library/build/blocks-manifest.php'
    );
    register_block_type( WP_PLUGIN_DIR . '/my-block-library/build/pricing' );
    register_block_type( WP_PLUGIN_DIR . '/my-block-library/build/services', array( 'render_callback' => 'my_block_library_render_callback' ) ); // It should still be possible to pass additional settings, so might be worth highlighting.
    register_block_type( WP_PLUGIN_DIR . '/my-block-library/build/testimonial/block.json' ); // Does it work with full path, too?
    
  • As noted in the code snippet, some notes about the path defined to the block.json file when using the collection would be helpful.
  • Totally optional, illustrate that passing additional settings when registering the block is still compatible with the metadata collection.

@mreishus
Copy link
Contributor Author

register_block_type( WP_PLUGIN_DIR . '/my-block-library/build/testimonial/block.json' ); // Does it work with full path, too?

Indeed, it works with full paths as well.

For the draft, it looks good! I agree with the "clarify it's an additional step" from @gziolo. For myself, I have no suggestions, other than optionally, you may want to also include https://make.wordpress.org/core/2022/10/07/improved-php-performance-for-core-blocks-registration/ when showing previous context.

Also, I'm sure you guys already know this, but just to be un-ambiguous, I don't have the ability to merge this PR. :)

@gziolo
Copy link
Member

gziolo commented Oct 17, 2024

Everything works as intended:

Screenshot 2024-10-17 at 13 13 51

@gziolo gziolo enabled auto-merge (squash) October 17, 2024 11:19
@gziolo
Copy link
Member

gziolo commented Oct 17, 2024

Once PR lands in trunk, I will cherry pick to wp/latest and trigger npm publishing.

Copy link
Member

@jsnajdr jsnajdr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, thanks @mreishus for implementing the suggestion to abort right away when the build directory doesn't exist.

@gziolo gziolo merged commit 76cf075 into WordPress:trunk Oct 17, 2024
62 checks passed
@github-actions github-actions bot added this to the Gutenberg 19.6 milestone Oct 17, 2024
gziolo added a commit that referenced this pull request Oct 17, 2024
* scripts: Add build-blocks-manifest command

* Remove package.json entry

* add test

* npm install in root to update lockfile

* Add changelog entry

* error on empty and missing dirs, update tests

* Update CHANGELOG.md

---------

Co-authored-by: Greg Ziółkowski <grzegorz@gziolo.pl>

Co-authored-by: mreishus <mreishus@git.wordpress.org>
Co-authored-by: gziolo <gziolo@git.wordpress.org>
Co-authored-by: felixarntz <flixos90@git.wordpress.org>
Co-authored-by: sirreal <jonsurrell@git.wordpress.org>
Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org>
@gziolo
Copy link
Member

gziolo commented Oct 17, 2024

Publishing to npm with https://github.com/WordPress/gutenberg/actions/runs/11385894200.

@mreishus mreishus deleted the add/blocks-metadata branch October 17, 2024 14:13
karthick-murugan pushed a commit to karthick-murugan/gutenberg that referenced this pull request Nov 13, 2024
* scripts: Add build-blocks-manifest command

* Remove package.json entry

* add test

* npm install in root to update lockfile

* Add changelog entry

* error on empty and missing dirs, update tests

* Update CHANGELOG.md

---------

Co-authored-by: Greg Ziółkowski <grzegorz@gziolo.pl>

Co-authored-by: mreishus <mreishus@git.wordpress.org>
Co-authored-by: gziolo <gziolo@git.wordpress.org>
Co-authored-by: felixarntz <flixos90@git.wordpress.org>
Co-authored-by: sirreal <jonsurrell@git.wordpress.org>
Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Tool] WP Scripts /packages/scripts [Type] New API New API to be used by plugin developers or package users.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants