-
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
[WIP] Update/build individual blocks #6087
[WIP] Update/build individual blocks #6087
Conversation
Partially completed server-side registration for more core blocks. Some core blocks are still left. Only editor_script handles are being enqueued as of now.
Added initial support for server-side registration for core dynamic blocks like Reusable block, Categories block and Latest posts block
Registered image block on the server-side.
…h block WebPack was configured to bundle CSS of all blocks in one single file. Since we now need to enqueue each CSS separately, the configuration has now been updated to generate different CSS files for each block
Earlier, we were enqueuing a monolithic bundle for the CSS. Now, common structural CSS for each block is being enqueued separately. This is needed to ensure that we can later intelligently enqueue these assets (load CSS only for blocks present on the page)
Earlier, we were enqueuing a monolithic bundle for the editor-specific CSS. Now, editor specific CSS for each block is being enqueued separately.
Since editor-specific styles for individual blocks are enqueued separately now, there is no need to register the 'wp-edit-blocks' style handle now.
…ock's individual styles
…butes for individual blocks Since the Webpack configuration now builds individual blocks to /build/blocks/library/, relative paths of style, editor_style and editor_script attributes have been changed in server side code for individual blocks.
Since the Webpack configuration now builds scripts for core-data and viewport in build/ folder, the same have now been updated in client-assets.php
We don't need "setDefaultBlockName" in heading block's JavaScriot. So removed the import.
Removed import for registerCoreBlocks in editor/test/selectors.js since it's no longer used. Replaced name by 'core/paragraph' in paragraph/index.js since the name constant is no longer defined
Is it possible to automate the PHP files part to avoid so much boilerplate code? Given that all files are generated by Webpack and we know all the patterns how JS and CSS files are named based on the folder name, can we autodetect those files and register them behind the scenes? I provide an example for Beforefunction register_core_audio_block() {
wp_register_script( 'core-audio-block', gutenberg_url( '/build/blocks/library/audio.js' ) );
wp_register_style(
'core-audio-block',
gutenberg_url( '/build/blocks/library/audio.css' ),
array(),
filemtime( gutenberg_dir_path() . 'build/blocks/library/audio.css' )
);
wp_style_add_data( 'core-audio-block', 'rtl', 'replace' );
wp_register_style(
'core-audio-block-editor',
gutenberg_url( '/build/blocks/library/audio_editor.css' ),
array(),
filemtime( gutenberg_dir_path() . 'build/blocks/library/audio_editor.css' )
);
wp_style_add_data( 'core-audio-block-editor', 'rtl', 'replace' );
register_block_type( 'core/audio', array(
'style' => 'core-audio-block',
'editor_style' => 'core-audio-block-editor',
'editor_script' => 'core-audio-block',
) );
}
add_action( 'init', 'register_core_audio_block' ); Afterfunction register_core_audio_block() {
register_block_type( 'core/audio', array(
'style' => 'core-audio-block',
'editor_style' => 'core-audio-block-editor',
'editor_script' => 'core-audio-block',
'detect_files' => true, // Not sure if we need it all, but leaveing a note to better explain the idea.
) );
}
add_action( 'init', 'register_core_audio_block' ); |
…block assets The previous Webpack configuration was building individual block assets to /build/blocks/library/paragraph.js (/css) It has now been changed to /build/__block_paragraph.js (/css)
…oks to be CSS used by the editor Renamed style.scss in blocks/library/block to editor.scss since it looks to be CSS used by the editor. Also enqueued the same as the editor_style attribute for 'core/block' server-side.
My only concern is that if it's boilerplate for us, it's boilerplate for any plugin developer, and we ought not give ourselves too many shortcuts if there's a solution which could benefit everyone. And since these are meant to be proper script and style handles, when would they be registered? Could plugins still manipulate them? What is the naming convention which associates a script handle with the detected file? |
Will we risk any regression on #4514 where registration was moved to |
I thought about it as a general opt-in solution, rather than tailored to needs of Gutenberg (core). We should still provide an option which gives very granular control over registered assets as it is coded in this PR. I would like us to explore if there is a nice way to simplify it for recurring patterns we can observe for core blocks. Looking at Webpack config I can see that we are expecting to have
First, I missed one thing - dependencies of the styles and scripts. They weren’t included initially which tricked me a bit but it’s being fixed (02812b4). This becomes a weak point of my proposal, but in general, it is something that should be fixed in WordPress core sooner than later. It slowly becomes a pain point as we introduce new modules. To your point, I don’t think it would change the existing flow a lot. My expectation was to execute the logic which registers those assets at the beginning of the |
One blocker with shipping this functionality is that it will enqueue stylesheets and scripts for each We can use WordPress's inbuilt We can think of enqueueing This approach however has an issue. We can add some functionality in Specify the value of the Etag header as a hash of the script/style handles and their corresponding versions. So, if we get a request to load styles for paragraph and image block like this:
We can generate the Etag like this:
and supply this value in the This will make sure that the next time the plugin/theme developer changes the CSS / JS for a block type (meaning changes the version for the CSS / JS of a We can add this functionality in Maybe, this new functionality can be triggered if a specific URL parameter is included in the request to What do you guys think of this approach? Do you think we can go ahead with this? |
@kanishkdudeja - this is exactly what we discussed on Slack. It sounds reasonable. This is exactly what I would explore first as a simple solution. Personally, I would also try to use plugin version instead of generating the hash. Unless the plugin version is unreliable. |
Thanks for your comment @gziolo
Yes, I just wanted to keep @aduth @mcsf and @nb in loop here as well.
Thanks. I'm starting on this now :)
I'm not sure if we should use Ideally, |
I agree with this. I think your idea is valid and worth exploring. I'm not sure whether there is a backwards-compatibility worry to be concerned about here either. At worst, it would "bust" existing caches for those who have them? Proposing as a ticket on the Core Trac may also invite more feedback from those who might have reason to think that assigning the current WordPress version was a necessary decision. |
I've opened a ticket for this on the Have proposed this patch for the enhancement: https://core.trac.wordpress.org/attachment/ticket/43825/43825.patch |
…ild has run or not
Some related work (by @youknowriad) is close to being merged in #6351. Some of the work is common (importing dependencies from Once #6351 gets merged, I'll fix the conflicts and rebase the changes in this branch from the updated master branch. |
Pushed a commit which resolves conflicts as a result of merging changes from master (After #6351 got merged). This has led to some regressions in tests for this branch. Will fix those as well and post an update here once it's done. |
I had a discussion about this PR with @aduth today and we felt that it might be better to pause work on this for now since there is a major blocker with this PR going live and since this functionality doesn't seem to be a priority as of now. This PR will enqueue separate stylesheets and scripts for each But it looks like we'll have another problem. Both these scripts don't initialize plugins. So they don't have a list of So, either we could
What are your thoughts? |
Yes, this one is complex. Let's wait until we have a better mechanism for server-side registration of blocks and support for batching plugin related files in core. |
This pull request appears to have languished and will not be easily reconciled with master. Please feel free to reopen and rebase against the current master, or open a new pull request. |
This is continuation of work started by @aduth in #4841
Most of the work is complete. Some things which are left are: