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

Build: Split packages and blocks to their webpack configs #1412

Closed
wants to merge 11 commits into from
Closed

Build: Split packages and blocks to their webpack configs #1412

wants to merge 11 commits into from

Conversation

gziolo
Copy link
Member

@gziolo gziolo commented Jun 22, 2021

Trac ticket: https://core.trac.wordpress.org/ticket/53690

It aligns with the changes proposed added in Gutenberg: WordPress/gutenberg#33293.

The idea here is to split the growing webpack config into two parts:

  • blocks
  • packages

We need to add handling for JavaScript files that are going to be used with blocks on the frontend. They don't work quite well with the current setup for entry points created for packages because of reasons like

  • the output path is different
  • frontend scripts should not be exposed under wp globals

In general, it looks like the split is a good strategy because it nicely separates two independents parts: packages and blocks. There isn't that much overlap between configs anyway.

Add View Script support for block types

As part of the effort, this PR adds support for viewScript in block.json metadata file that is later translated to $view_script in WP_Block_Type class and exposed as view_script from the REST API endpoint for block types.

It would probably be better to have the changes related to the view script in its own ticket, but it's a a bit tricky because we the split for the webpack config into two parts doesn't conceptually make too much sense if the part for blocks don't handle JavaScript files 😅 I'm happy though to split it into two commits if that makes the validation process easier for everyone.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@gziolo gziolo self-assigned this Jun 22, 2021
),
} ),
new DependencyExtractionPlugin( {
injectPolyfill: true,
Copy link
Member Author

Choose a reason for hiding this comment

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

It looks like the option for combining assets into one file doesn't quite work with multiple webpack configs so I left it disabled for blocks for now since we have only one file for now. It will require upstream changes.

The issue is that it only outputs only one file for the config that was processed last ...

@gziolo gziolo requested review from youknowriad and desrosj June 22, 2021 14:36
@gziolo
Copy link
Member Author

gziolo commented Jun 22, 2021

@youknowriad and @desrosj, let me know what do you think. This is a prerequisite if we still want to wire the JavaScript file necessary for PDF embeds for the FIle block to WordPress 5.8.

@gziolo
Copy link
Member Author

gziolo commented Jun 24, 2021

fe0d7d0 provides the way to enqueue the view script for the File block on the front end:

Screen Shot 2021-06-24 at 18 35 17

@gziolo
Copy link
Member Author

gziolo commented Jun 25, 2021

This patch depends on #1430. Once the planned changes to WordPress npm packages are published to npm and committed to WP core, this branch can be rebased, retested and committed.

@@ -29,6 +29,9 @@ wp-tests-config.php
/src/wp-includes/css/*.min.css
/src/wp-includes/css/*-rtl.css
/src/wp-includes/blocks/**/*.css
/src/wp-includes/blocks/**/*.js
Copy link
Member Author

@gziolo gziolo Jun 25, 2021

Choose a reason for hiding this comment

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

The same changes need to be reflected in SVN for the file subfolder.

@gziolo
Copy link
Member Author

gziolo commented Jun 25, 2021

I can confirm that it works as expected with all the latest changes applied to the patch.

Inline PDF embed enabled

Screen Shot 2021-06-25 at 19 11 33

The view script is included in the HTML source of the page together with the polyfill script:

Screen Shot 2021-06-25 at 19 12 29

Note: It looks like wp-i18n that depends on wp-hooks is added even when there are no translations detected in the script. I guess it's the default behavior to assume that. It should be investigated separately.

Addressed with 446a3d5

Screen Shot 2021-06-25 at 19 28 37

Inline PDF embed disabled

Screen Shot 2021-06-25 at 19 11 37

The view script is not included in the HTML source as expected:

Screen Shot 2021-06-25 at 19 11 54

@youknowriad
Copy link
Contributor

This is looking good to me. @desrosj mind doing a quick review here?

@desrosj
Copy link
Contributor

desrosj commented Jun 29, 2021

I'm seeing the following notice locally:

Notice: register_block_script_handle was called incorrectly. The asset file for the "viewScript" defined in "core/file" block definition is missing. Please see Debugging in WordPress for more information. (This message was added in version 5.5.0.) in /var/www/src/wp-includes/functions.php on line 5535

Is this because an update to the file block needs to be published? Otherwise it seems to work well for me.

@desrosj
Copy link
Contributor

desrosj commented Jun 29, 2021

I did just notice that the Text link settings is really tall with this applied. It does not display this way before applying this PR and running the build script.

tall-text-link-settings

@youknowriad
Copy link
Contributor

I did just notice that the Text link settings is really tall with this applied. It does not display this way before applying this PR and running the build script.

I'm pretty sure that's unrelated, I did get these tall selects sometimes (outside this PR) but we were not able to reproduce consistently yet. There are probably multiple factors involved there.

Notice: register_block_script_handle was called incorrectly. The asset file for the "viewScript" defined in "core/file" block definition is missing. Please see Debugging in WordPress for more information. (This message was added in version 5.5.0.) in /var/www/src/wp-includes/functions.php on line 5535

This is definitely related to this PR, I need to investigate it more, I wish @gziolo was around :)

@youknowriad
Copy link
Contributor

I'm not able to see the notice, though it can be my docker config hiding it I don't know 🤔

@youknowriad
Copy link
Contributor

After several tries, I'm still not able to reproduce the notice. Let's continue monitor this.

@youknowriad
Copy link
Contributor

@gziolo gziolo deleted the update/split-webpack-config branch June 29, 2021 18:53
@desrosj
Copy link
Contributor

desrosj commented Jun 30, 2021

Unfortunately this caused a PHP notice that I was unable to solve before it was time to package RC1, so I've had to revert it and the related commits.

@gziolo
Copy link
Member Author

gziolo commented Jul 5, 2021

I will give it another try after WordPress 5.8 is out 👍🏻

@desrosj and @youknowriad, thank you for all the help 🙇🏻

@gziolo gziolo reopened this Jul 16, 2021
@gziolo
Copy link
Member Author

gziolo commented Jul 16, 2021

I reopened this branch and rebased it with the latest changes from trunk.

I can see the same notice you mentioned in the CI job that runs PHPUnit tests:

Screen Shot 2021-07-16 at 12 01 52

I will see how we can improve it since it's quite expected because the asset file is generated with a webpack build that doesn't run on CI before unit tests.

@gziolo
Copy link
Member Author

gziolo commented Jul 16, 2021

Committing the asset files (and removing from .gitignore) resolves the issue, but it makes the limitation of the weback plugin hightlighted in #1412 (comment) more visible.

I still think that should be fine as the first step.

@gziolo
Copy link
Member Author

gziolo commented Jul 19, 2021

@aristath and @youknowriad – do you think we should some automated handling for the view script? At the moment it's handled manually by the File block in render_callback. I personally think about enqueuing automatically the view script when a block is rendered and render_callback isn't provided. We can safely assume that in such a case the block author wants to have the view script to be loaded on the frontend when the block is present in the HTML.

@aristath
Copy link
Member

@aristath and @youknowriad – do you think we should some automated handling for the view script? At the moment it's handled manually by the File block in render_callback. I personally think about enqueuing automatically the view script when a block is rendered and render_callback isn't provided. We can safely assume that in such a case the block author wants to have the view script to be loaded on the frontend when the block is present in the HTML.

Automatically enqueueing the script when the block is rendered would make sense... I don't see why we'd require manually adding it to render_callback 👍

@gziolo
Copy link
Member Author

gziolo commented Jul 27, 2021

@aristath and @youknowriad – do you think we should some automated handling for the view script? At the moment it's handled manually by the File block in render_callback. I personally think about enqueuing automatically the view script when a block is rendered and render_callback isn't provided. We can safely assume that in such a case the block author wants to have the view script to be loaded on the frontend when the block is present in the HTML.

Automatically enqueueing the script when the block is rendered would make sense... I don't see why we'd require manually adding it to render_callback 👍

Added in 1791165. If the render_callback is provided then it has to be explicitly handled to account for the more complex cases like with the core blocks: File and Navigation.

@gziolo
Copy link
Member Author

gziolo commented Jul 28, 2021

@gziolo gziolo closed this Jul 28, 2021
@gziolo gziolo deleted the update/split-webpack-config branch July 28, 2021 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants