-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Conversation
Tag @felixarntz ^ |
There was a problem hiding this 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'; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
gutenberg/packages/scripts/config/webpack.config.js
Lines 314 to 345 in 79940e0
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.
There was a problem hiding this comment.
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.
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
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", |
There was a problem hiding this comment.
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 😄
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this 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:
There was a problem hiding this 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.
There was a problem hiding this 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!
f52f2f9
to
2693402
Compare
const blockJsonFiles = glob( './**/block.json', { | ||
cwd: path.resolve( process.cwd(), inputDir ), | ||
absolute: true, | ||
} ); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Check e673f31
e673f31
to
a92f4a1
Compare
@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? |
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.
I'm checking the draft next. |
I'm not sure what is the best place for feedback. I can start here and move it elsewhere upon request:
|
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. :) |
Once PR lands in |
There was a problem hiding this 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.
* 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>
Publishing to npm with https://github.com/WordPress/gutenberg/actions/runs/11385894200. |
* 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>
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
npm run build:blocks-manifest
and inspect thebuild/blocks-manifest.php
file.Testing Instructions for Keyboard
Screenshots or screencast