-
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
Enhance create-block package to support blocks manifest and relevant Core APIs by default #69446
base: trunk
Are you sure you want to change the base?
Conversation
…Core APIs by default.
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. |
Size Change: 0 B Total Size: 1.84 MB ℹ️ View Unchanged
|
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 is really cool enhancement. I like how it will help to work with multiple blocks in a single WordPress plugin. You just add a new folder with a block and everything magically happens. It also provides a perfect example of the usage for the best practices outlined in the dev note 👏🏻
My only question is how to ensure the manifest is also created and ideally stays up to date in the watch mode (npm start
). I offered a potential solution.
} | ||
} | ||
{{/wpScripts}} | ||
{{^wpScripts}} | ||
register_block_type( __DIR__ . '/build/{{slug}}' ); |
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'm glad we landed #68175 earlier, which made it easier to use the metadata collection.
( isDynamicVariant | ||
? 'wp-scripts build --webpack-copy-php' | ||
: 'wp-scripts build' ) + | ||
' && 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.
I'm wondering what we should do about start
which is more nuanced as it works in the watch mode, so it would have to run wp-scripts build-blocks-manifest
each time any block.json
file changes in the build
folder.
After consulting ChatGPT, it looks like the simplest approach would be to create a simple Webpack plugin that runs the post-processing script after each build (including the watch mode), example code that illustrates the idea:
class PostProcessPlugin {
apply(compiler) {
compiler.hooks.done.tap('PostProcessPlugin', (stats) => {
console.log('Running post-processing script...');
const { exec } = require('child_process');
exec('node postprocess.js', (error, stdout, stderr) => {
if (error) {
console.error(`Post-processing error: ${error.message}`);
return;
}
if (stderr) {
console.error(`Post-processing stderr: ${stderr}`);
return;
}
console.log(`Post-processing stdout: ${stdout}`);
});
});
}
}
module.exports = {
// Your Webpack config...
plugins: [new PostProcessPlugin()]
};
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's a great point, but it goes a bit over my head, as I'm not very familiar with the lower-level best practices for Webpack 🫤
Are you saying this would go in a Webpack file for the generated plugin? Or something we should build into wp-scripts
? Maybe we can have a configuration for whether to run the watching with or without support for 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.
Updating start
to mirror the changes applied in this PR for build
could be good enough to proceed.
We can explore separately how to improve that later. If someone changes block.json
during development and it wouldn't get picked up by the generated manifest file, they can always restart the command.
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.
SG, added in fa54d00
This PR enhances the
@wordpress/create-block
package to use the new WordPress 6.8 functionwp_register_block_types_from_metadata_collection()
(see https://core.trac.wordpress.org/ticket/62267).This ensures plugins generated by the tool follow the latest best practices for block type registration.
For a good summary including a code examples, see also the relevant dev note draft.
Related: #69445