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

WP Scripts: Automatically detect entry points for the default JS and CSS files #66466

Draft
wants to merge 8 commits into
base: trunk
Choose a base branch
from

Conversation

gziolo
Copy link
Member

@gziolo gziolo commented Oct 25, 2024

What?

Fixes #55936.
Related to #65602.

Improves the default webpack configuration to become more flexible in how entry points get detected. Primarily, make it possible for the main CSS files get detected in the source directory and set as an entry point. Secondly, make it more likely that the JavaScript main file is set as an entry point if it isn't directly tied to any block.

Why?

It's been reported multiple times that WP Scripts is too much tied to block development. The expectation it works out of the box when writing JavaScript for plugins or styles for themes. The good example is the latest blog post from @justintadlock on WordPress Developer Blog: Getting and setting Block Binding values in the Editor. In particular, this PR tries to mitigate the need for a custom webpack config in the project like:

// WordPress webpack config.
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );

// Utilities.
const path = require( 'path' );

// Add any a new entry point by extending the webpack config.
module.exports = {
	...defaultConfig,
	...{
		entry: {
			'js/editor': path.resolve( process.cwd(), 'resources/js', 'editor.js' )
		}
	}
};

Taking into account the project structure explained in the article:

Screenshot 2024-11-10 at 11 39 40

With the changes proposed, the entry point could still be located in the resources/js/ folder, but the file would need to get renamed to index.js. Then the build command would become:

wp-scripts build --webpack-src-dir=resources/js --output-path=public

This would altogether remove the need for the custom config file.

An open question is whether we should also handle different names as entry points in addition to index.js. In this case, editor.js could be the alternative worth considering.

How?

It is based on the convention where JS file is index.js (following Node.js) and the CSS file is style.css (main stylesheet in themes).

Detection logic for the list of entry points with the updated webpack config. There are several steps:

  1. Use the legacy webpack 4 format passed as CLI arguments, example: wp-scripts build index.js. Using this option will set entry points based on the list of the paths passed.
  2. Scan block.json files for scripts listed inside editorScript, script and viewScript fields. If block.json is located in the root of the source directory, the scanning ends for backward compatibility.
  3. Adds src/index.* JavaScript file when exists.
  4. Adds src/style.* CSS file when exists.

Testing Instructions

npx wp-create-block example-block --no-wp-scripts
cd example-block
../node_modules/.bin/wp-scripts build

This covers the single block example in which case, steps (3) and (4) outlined in the "How?" section should be skipped because block.json is located in the root of the source directory.

Screenshot 2024-11-10 at 14 25 16

The build folder looks as follows:

Screenshot 2024-11-10 at 14 26 06

Documentation changes

The above-mentioned and other possible configurations are explained in the updated README file, too.

@gziolo gziolo self-assigned this Oct 25, 2024
@gziolo gziolo added [Tool] WP Scripts /packages/scripts [Type] Enhancement A suggestion for improvement. labels Oct 25, 2024
Copy link

github-actions bot commented Oct 25, 2024

Size Change: 0 B

Total Size: 1.82 MB

ℹ️ View Unchanged
Filename Size
build-module/a11y/index.min.js 482 B
build-module/block-library/file/view.min.js 447 B
build-module/block-library/image/view.min.js 1.78 kB
build-module/block-library/navigation/view.min.js 1.16 kB
build-module/block-library/query/view.min.js 742 B
build-module/block-library/search/view.min.js 616 B
build-module/interactivity-router/index.min.js 3.03 kB
build-module/interactivity/debug.min.js 17.2 kB
build-module/interactivity/index.min.js 13.6 kB
build/a11y/index.min.js 952 B
build/annotations/index.min.js 2.26 kB
build/api-fetch/index.min.js 2.32 kB
build/autop/index.min.js 2.12 kB
build/blob/index.min.js 579 B
build/block-directory/index.min.js 7.26 kB
build/block-directory/style-rtl.css 1 kB
build/block-directory/style.css 1 kB
build/block-editor/content-rtl.css 4.42 kB
build/block-editor/content.css 4.41 kB
build/block-editor/default-editor-styles-rtl.css 394 B
build/block-editor/default-editor-styles.css 394 B
build/block-editor/index.min.js 255 kB
build/block-editor/style-rtl.css 15.4 kB
build/block-editor/style.css 15.4 kB
build/block-library/blocks/archives/editor-rtl.css 84 B
build/block-library/blocks/archives/editor.css 83 B
build/block-library/blocks/archives/style-rtl.css 90 B
build/block-library/blocks/archives/style.css 90 B
build/block-library/blocks/audio/editor-rtl.css 149 B
build/block-library/blocks/audio/editor.css 151 B
build/block-library/blocks/audio/style-rtl.css 132 B
build/block-library/blocks/audio/style.css 132 B
build/block-library/blocks/audio/theme-rtl.css 134 B
build/block-library/blocks/audio/theme.css 134 B
build/block-library/blocks/avatar/editor-rtl.css 115 B
build/block-library/blocks/avatar/editor.css 115 B
build/block-library/blocks/avatar/style-rtl.css 104 B
build/block-library/blocks/avatar/style.css 104 B
build/block-library/blocks/button/editor-rtl.css 265 B
build/block-library/blocks/button/editor.css 265 B
build/block-library/blocks/button/style-rtl.css 555 B
build/block-library/blocks/button/style.css 555 B
build/block-library/blocks/buttons/editor-rtl.css 291 B
build/block-library/blocks/buttons/editor.css 291 B
build/block-library/blocks/buttons/style-rtl.css 345 B
build/block-library/blocks/buttons/style.css 345 B
build/block-library/blocks/calendar/style-rtl.css 240 B
build/block-library/blocks/calendar/style.css 240 B
build/block-library/blocks/categories/editor-rtl.css 132 B
build/block-library/blocks/categories/editor.css 131 B
build/block-library/blocks/categories/style-rtl.css 152 B
build/block-library/blocks/categories/style.css 152 B
build/block-library/blocks/code/editor-rtl.css 53 B
build/block-library/blocks/code/editor.css 53 B
build/block-library/blocks/code/style-rtl.css 139 B
build/block-library/blocks/code/style.css 139 B
build/block-library/blocks/code/theme-rtl.css 122 B
build/block-library/blocks/code/theme.css 122 B
build/block-library/blocks/columns/editor-rtl.css 108 B
build/block-library/blocks/columns/editor.css 108 B
build/block-library/blocks/columns/style-rtl.css 420 B
build/block-library/blocks/columns/style.css 420 B
build/block-library/blocks/comment-author-avatar/editor-rtl.css 124 B
build/block-library/blocks/comment-author-avatar/editor.css 124 B
build/block-library/blocks/comment-author-name/style-rtl.css 72 B
build/block-library/blocks/comment-author-name/style.css 72 B
build/block-library/blocks/comment-content/style-rtl.css 120 B
build/block-library/blocks/comment-content/style.css 120 B
build/block-library/blocks/comment-date/style-rtl.css 65 B
build/block-library/blocks/comment-date/style.css 65 B
build/block-library/blocks/comment-edit-link/style-rtl.css 70 B
build/block-library/blocks/comment-edit-link/style.css 70 B
build/block-library/blocks/comment-reply-link/style-rtl.css 71 B
build/block-library/blocks/comment-reply-link/style.css 71 B
build/block-library/blocks/comment-template/style-rtl.css 200 B
build/block-library/blocks/comment-template/style.css 199 B
build/block-library/blocks/comments-pagination-numbers/editor-rtl.css 122 B
build/block-library/blocks/comments-pagination-numbers/editor.css 121 B
build/block-library/blocks/comments-pagination/editor-rtl.css 228 B
build/block-library/blocks/comments-pagination/editor.css 217 B
build/block-library/blocks/comments-pagination/style-rtl.css 234 B
build/block-library/blocks/comments-pagination/style.css 231 B
build/block-library/blocks/comments-title/editor-rtl.css 75 B
build/block-library/blocks/comments-title/editor.css 75 B
build/block-library/blocks/comments/editor-rtl.css 837 B
build/block-library/blocks/comments/editor.css 836 B
build/block-library/blocks/comments/style-rtl.css 637 B
build/block-library/blocks/comments/style.css 637 B
build/block-library/blocks/cover/editor-rtl.css 655 B
build/block-library/blocks/cover/editor.css 654 B
build/block-library/blocks/cover/style-rtl.css 1.7 kB
build/block-library/blocks/cover/style.css 1.69 kB
build/block-library/blocks/details/editor-rtl.css 65 B
build/block-library/blocks/details/editor.css 65 B
build/block-library/blocks/details/style-rtl.css 86 B
build/block-library/blocks/details/style.css 86 B
build/block-library/blocks/embed/editor-rtl.css 331 B
build/block-library/blocks/embed/editor.css 331 B
build/block-library/blocks/embed/style-rtl.css 419 B
build/block-library/blocks/embed/style.css 419 B
build/block-library/blocks/embed/theme-rtl.css 133 B
build/block-library/blocks/embed/theme.css 133 B
build/block-library/blocks/file/editor-rtl.css 326 B
build/block-library/blocks/file/editor.css 326 B
build/block-library/blocks/file/style-rtl.css 278 B
build/block-library/blocks/file/style.css 279 B
build/block-library/blocks/footnotes/style-rtl.css 198 B
build/block-library/blocks/footnotes/style.css 197 B
build/block-library/blocks/form-input/editor-rtl.css 229 B
build/block-library/blocks/form-input/editor.css 229 B
build/block-library/blocks/form-input/style-rtl.css 357 B
build/block-library/blocks/form-input/style.css 357 B
build/block-library/blocks/form-submission-notification/editor-rtl.css 344 B
build/block-library/blocks/form-submission-notification/editor.css 341 B
build/block-library/blocks/form-submit-button/style-rtl.css 69 B
build/block-library/blocks/form-submit-button/style.css 69 B
build/block-library/blocks/form/view.min.js 470 B
build/block-library/blocks/freeform/editor-rtl.css 2.6 kB
build/block-library/blocks/freeform/editor.css 2.6 kB
build/block-library/blocks/gallery/editor-rtl.css 946 B
build/block-library/blocks/gallery/editor.css 951 B
build/block-library/blocks/gallery/style-rtl.css 1.83 kB
build/block-library/blocks/gallery/style.css 1.82 kB
build/block-library/blocks/gallery/theme-rtl.css 108 B
build/block-library/blocks/gallery/theme.css 108 B
build/block-library/blocks/group/editor-rtl.css 409 B
build/block-library/blocks/group/editor.css 409 B
build/block-library/blocks/group/style-rtl.css 103 B
build/block-library/blocks/group/style.css 103 B
build/block-library/blocks/group/theme-rtl.css 79 B
build/block-library/blocks/group/theme.css 79 B
build/block-library/blocks/heading/style-rtl.css 188 B
build/block-library/blocks/heading/style.css 188 B
build/block-library/blocks/html/editor-rtl.css 346 B
build/block-library/blocks/html/editor.css 347 B
build/block-library/blocks/image/editor-rtl.css 799 B
build/block-library/blocks/image/editor.css 799 B
build/block-library/blocks/image/style-rtl.css 1.59 kB
build/block-library/blocks/image/style.css 1.59 kB
build/block-library/blocks/image/theme-rtl.css 137 B
build/block-library/blocks/image/theme.css 137 B
build/block-library/blocks/latest-comments/style-rtl.css 355 B
build/block-library/blocks/latest-comments/style.css 354 B
build/block-library/blocks/latest-posts/editor-rtl.css 139 B
build/block-library/blocks/latest-posts/editor.css 138 B
build/block-library/blocks/latest-posts/style-rtl.css 509 B
build/block-library/blocks/latest-posts/style.css 510 B
build/block-library/blocks/list/style-rtl.css 107 B
build/block-library/blocks/list/style.css 107 B
build/block-library/blocks/loginout/style-rtl.css 61 B
build/block-library/blocks/loginout/style.css 61 B
build/block-library/blocks/media-text/editor-rtl.css 321 B
build/block-library/blocks/media-text/editor.css 320 B
build/block-library/blocks/media-text/style-rtl.css 558 B
build/block-library/blocks/media-text/style.css 556 B
build/block-library/blocks/more/editor-rtl.css 427 B
build/block-library/blocks/more/editor.css 427 B
build/block-library/blocks/navigation-link/editor-rtl.css 644 B
build/block-library/blocks/navigation-link/editor.css 645 B
build/block-library/blocks/navigation-link/style-rtl.css 192 B
build/block-library/blocks/navigation-link/style.css 191 B
build/block-library/blocks/navigation-submenu/editor-rtl.css 295 B
build/block-library/blocks/navigation-submenu/editor.css 294 B
build/block-library/blocks/navigation/editor-rtl.css 2.2 kB
build/block-library/blocks/navigation/editor.css 2.2 kB
build/block-library/blocks/navigation/style-rtl.css 2.25 kB
build/block-library/blocks/navigation/style.css 2.23 kB
build/block-library/blocks/nextpage/editor-rtl.css 392 B
build/block-library/blocks/nextpage/editor.css 392 B
build/block-library/blocks/page-list/editor-rtl.css 378 B
build/block-library/blocks/page-list/editor.css 378 B
build/block-library/blocks/page-list/style-rtl.css 175 B
build/block-library/blocks/page-list/style.css 175 B
build/block-library/blocks/paragraph/editor-rtl.css 236 B
build/block-library/blocks/paragraph/editor.css 236 B
build/block-library/blocks/paragraph/style-rtl.css 341 B
build/block-library/blocks/paragraph/style.css 340 B
build/block-library/blocks/post-author-biography/style-rtl.css 74 B
build/block-library/blocks/post-author-biography/style.css 74 B
build/block-library/blocks/post-author-name/style-rtl.css 69 B
build/block-library/blocks/post-author-name/style.css 69 B
build/block-library/blocks/post-author/editor-rtl.css 107 B
build/block-library/blocks/post-author/editor.css 107 B
build/block-library/blocks/post-author/style-rtl.css 188 B
build/block-library/blocks/post-author/style.css 189 B
build/block-library/blocks/post-comments-form/editor-rtl.css 96 B
build/block-library/blocks/post-comments-form/editor.css 96 B
build/block-library/blocks/post-comments-form/style-rtl.css 527 B
build/block-library/blocks/post-comments-form/style.css 528 B
build/block-library/blocks/post-content/style-rtl.css 61 B
build/block-library/blocks/post-content/style.css 61 B
build/block-library/blocks/post-date/style-rtl.css 62 B
build/block-library/blocks/post-date/style.css 62 B
build/block-library/blocks/post-excerpt/editor-rtl.css 71 B
build/block-library/blocks/post-excerpt/editor.css 71 B
build/block-library/blocks/post-excerpt/style-rtl.css 155 B
build/block-library/blocks/post-excerpt/style.css 155 B
build/block-library/blocks/post-featured-image/editor-rtl.css 729 B
build/block-library/blocks/post-featured-image/editor.css 726 B
build/block-library/blocks/post-featured-image/style-rtl.css 347 B
build/block-library/blocks/post-featured-image/style.css 347 B
build/block-library/blocks/post-navigation-link/style-rtl.css 215 B
build/block-library/blocks/post-navigation-link/style.css 214 B
build/block-library/blocks/post-template/editor-rtl.css 99 B
build/block-library/blocks/post-template/editor.css 98 B
build/block-library/blocks/post-template/style-rtl.css 399 B
build/block-library/blocks/post-template/style.css 398 B
build/block-library/blocks/post-terms/style-rtl.css 96 B
build/block-library/blocks/post-terms/style.css 96 B
build/block-library/blocks/post-time-to-read/style-rtl.css 70 B
build/block-library/blocks/post-time-to-read/style.css 70 B
build/block-library/blocks/post-title/style-rtl.css 162 B
build/block-library/blocks/post-title/style.css 162 B
build/block-library/blocks/preformatted/style-rtl.css 125 B
build/block-library/blocks/preformatted/style.css 125 B
build/block-library/blocks/pullquote/editor-rtl.css 134 B
build/block-library/blocks/pullquote/editor.css 134 B
build/block-library/blocks/pullquote/style-rtl.css 342 B
build/block-library/blocks/pullquote/style.css 342 B
build/block-library/blocks/pullquote/theme-rtl.css 167 B
build/block-library/blocks/pullquote/theme.css 167 B
build/block-library/blocks/query-pagination-numbers/editor-rtl.css 121 B
build/block-library/blocks/query-pagination-numbers/editor.css 118 B
build/block-library/blocks/query-pagination/editor-rtl.css 154 B
build/block-library/blocks/query-pagination/editor.css 154 B
build/block-library/blocks/query-pagination/style-rtl.css 237 B
build/block-library/blocks/query-pagination/style.css 237 B
build/block-library/blocks/query-title/style-rtl.css 64 B
build/block-library/blocks/query-title/style.css 64 B
build/block-library/blocks/query/editor-rtl.css 452 B
build/block-library/blocks/query/editor.css 451 B
build/block-library/blocks/quote/style-rtl.css 238 B
build/block-library/blocks/quote/style.css 238 B
build/block-library/blocks/quote/theme-rtl.css 233 B
build/block-library/blocks/quote/theme.css 236 B
build/block-library/blocks/read-more/style-rtl.css 138 B
build/block-library/blocks/read-more/style.css 138 B
build/block-library/blocks/rss/editor-rtl.css 101 B
build/block-library/blocks/rss/editor.css 101 B
build/block-library/blocks/rss/style-rtl.css 288 B
build/block-library/blocks/rss/style.css 287 B
build/block-library/blocks/search/editor-rtl.css 199 B
build/block-library/blocks/search/editor.css 199 B
build/block-library/blocks/search/style-rtl.css 672 B
build/block-library/blocks/search/style.css 671 B
build/block-library/blocks/search/theme-rtl.css 113 B
build/block-library/blocks/search/theme.css 113 B
build/block-library/blocks/separator/editor-rtl.css 100 B
build/block-library/blocks/separator/editor.css 100 B
build/block-library/blocks/separator/style-rtl.css 248 B
build/block-library/blocks/separator/style.css 248 B
build/block-library/blocks/separator/theme-rtl.css 195 B
build/block-library/blocks/separator/theme.css 195 B
build/block-library/blocks/shortcode/editor-rtl.css 286 B
build/block-library/blocks/shortcode/editor.css 286 B
build/block-library/blocks/site-logo/editor-rtl.css 806 B
build/block-library/blocks/site-logo/editor.css 803 B
build/block-library/blocks/site-logo/style-rtl.css 218 B
build/block-library/blocks/site-logo/style.css 218 B
build/block-library/blocks/site-tagline/editor-rtl.css 87 B
build/block-library/blocks/site-tagline/editor.css 87 B
build/block-library/blocks/site-tagline/style-rtl.css 65 B
build/block-library/blocks/site-tagline/style.css 65 B
build/block-library/blocks/site-title/editor-rtl.css 85 B
build/block-library/blocks/site-title/editor.css 85 B
build/block-library/blocks/site-title/style-rtl.css 143 B
build/block-library/blocks/site-title/style.css 143 B
build/block-library/blocks/social-link/editor-rtl.css 338 B
build/block-library/blocks/social-link/editor.css 338 B
build/block-library/blocks/social-links/editor-rtl.css 729 B
build/block-library/blocks/social-links/editor.css 727 B
build/block-library/blocks/social-links/style-rtl.css 1.51 kB
build/block-library/blocks/social-links/style.css 1.5 kB
build/block-library/blocks/spacer/editor-rtl.css 346 B
build/block-library/blocks/spacer/editor.css 346 B
build/block-library/blocks/spacer/style-rtl.css 48 B
build/block-library/blocks/spacer/style.css 48 B
build/block-library/blocks/table-of-contents/style-rtl.css 83 B
build/block-library/blocks/table-of-contents/style.css 83 B
build/block-library/blocks/table/editor-rtl.css 394 B
build/block-library/blocks/table/editor.css 394 B
build/block-library/blocks/table/style-rtl.css 640 B
build/block-library/blocks/table/style.css 639 B
build/block-library/blocks/table/theme-rtl.css 152 B
build/block-library/blocks/table/theme.css 152 B
build/block-library/blocks/tag-cloud/editor-rtl.css 144 B
build/block-library/blocks/tag-cloud/editor.css 144 B
build/block-library/blocks/tag-cloud/style-rtl.css 266 B
build/block-library/blocks/tag-cloud/style.css 265 B
build/block-library/blocks/template-part/editor-rtl.css 368 B
build/block-library/blocks/template-part/editor.css 368 B
build/block-library/blocks/template-part/theme-rtl.css 113 B
build/block-library/blocks/template-part/theme.css 113 B
build/block-library/blocks/term-description/style-rtl.css 126 B
build/block-library/blocks/term-description/style.css 126 B
build/block-library/blocks/text-columns/editor-rtl.css 95 B
build/block-library/blocks/text-columns/editor.css 95 B
build/block-library/blocks/text-columns/style-rtl.css 165 B
build/block-library/blocks/text-columns/style.css 165 B
build/block-library/blocks/verse/style-rtl.css 98 B
build/block-library/blocks/verse/style.css 98 B
build/block-library/blocks/video/editor-rtl.css 396 B
build/block-library/blocks/video/editor.css 397 B
build/block-library/blocks/video/style-rtl.css 192 B
build/block-library/blocks/video/style.css 192 B
build/block-library/blocks/video/theme-rtl.css 134 B
build/block-library/blocks/video/theme.css 134 B
build/block-library/classic-rtl.css 179 B
build/block-library/classic.css 179 B
build/block-library/common-rtl.css 1.08 kB
build/block-library/common.css 1.08 kB
build/block-library/editor-elements-rtl.css 75 B
build/block-library/editor-elements.css 75 B
build/block-library/editor-rtl.css 11.7 kB
build/block-library/editor.css 11.7 kB
build/block-library/elements-rtl.css 54 B
build/block-library/elements.css 54 B
build/block-library/index.min.js 221 kB
build/block-library/reset-rtl.css 472 B
build/block-library/reset.css 472 B
build/block-library/style-rtl.css 15 kB
build/block-library/style.css 15 kB
build/block-library/theme-rtl.css 708 B
build/block-library/theme.css 712 B
build/block-serialization-default-parser/index.min.js 1.12 kB
build/block-serialization-spec-parser/index.min.js 2.87 kB
build/blocks/index.min.js 52.7 kB
build/commands/index.min.js 16.1 kB
build/commands/style-rtl.css 955 B
build/commands/style.css 952 B
build/components/index.min.js 228 kB
build/components/style-rtl.css 12.4 kB
build/components/style.css 12.4 kB
build/compose/index.min.js 12.7 kB
build/core-commands/index.min.js 3.08 kB
build/core-data/index.min.js 73.9 kB
build/customize-widgets/index.min.js 11 kB
build/customize-widgets/style-rtl.css 1.35 kB
build/customize-widgets/style.css 1.35 kB
build/data-controls/index.min.js 641 B
build/data/index.min.js 8.97 kB
build/date/index.min.js 18 kB
build/deprecated/index.min.js 458 B
build/dom-ready/index.min.js 325 B
build/dom/index.min.js 4.66 kB
build/edit-post/classic-rtl.css 578 B
build/edit-post/classic.css 580 B
build/edit-post/index.min.js 13.7 kB
build/edit-post/style-rtl.css 2.76 kB
build/edit-post/style.css 2.75 kB
build/edit-site/index.min.js 222 kB
build/edit-site/posts-rtl.css 7.32 kB
build/edit-site/posts.css 7.33 kB
build/edit-site/style-rtl.css 13.2 kB
build/edit-site/style.css 13.2 kB
build/edit-widgets/index.min.js 17.7 kB
build/edit-widgets/style-rtl.css 4.09 kB
build/edit-widgets/style.css 4.09 kB
build/editor/index.min.js 106 kB
build/editor/style-rtl.css 9.59 kB
build/editor/style.css 9.61 kB
build/element/index.min.js 4.82 kB
build/escape-html/index.min.js 537 B
build/format-library/index.min.js 8.05 kB
build/format-library/style-rtl.css 476 B
build/format-library/style.css 476 B
build/hooks/index.min.js 1.65 kB
build/html-entities/index.min.js 445 B
build/i18n/index.min.js 3.58 kB
build/is-shallow-equal/index.min.js 526 B
build/keyboard-shortcuts/index.min.js 1.31 kB
build/keycodes/index.min.js 1.46 kB
build/list-reusable-blocks/index.min.js 2.13 kB
build/list-reusable-blocks/style-rtl.css 852 B
build/list-reusable-blocks/style.css 852 B
build/media-utils/index.min.js 3.58 kB
build/notices/index.min.js 946 B
build/nux/index.min.js 1.62 kB
build/nux/style-rtl.css 749 B
build/nux/style.css 745 B
build/patterns/index.min.js 7.35 kB
build/patterns/style-rtl.css 687 B
build/patterns/style.css 685 B
build/plugins/index.min.js 1.86 kB
build/preferences-persistence/index.min.js 2.06 kB
build/preferences/index.min.js 2.9 kB
build/preferences/style-rtl.css 554 B
build/preferences/style.css 554 B
build/primitives/index.min.js 829 B
build/priority-queue/index.min.js 1.54 kB
build/private-apis/index.min.js 961 B
build/react-i18n/index.min.js 630 B
build/react-refresh-entry/index.min.js 9.47 kB
build/react-refresh-runtime/index.min.js 6.76 kB
build/redux-routine/index.min.js 2.7 kB
build/reusable-blocks/index.min.js 2.55 kB
build/reusable-blocks/style-rtl.css 256 B
build/reusable-blocks/style.css 256 B
build/rich-text/index.min.js 10.1 kB
build/router/index.min.js 2.11 kB
build/server-side-render/index.min.js 1.94 kB
build/shortcode/index.min.js 1.4 kB
build/style-engine/index.min.js 2.04 kB
build/token-list/index.min.js 581 B
build/url/index.min.js 3.9 kB
build/vendors/react-dom.min.js 41.7 kB
build/vendors/react-jsx-runtime.min.js 556 B
build/vendors/react.min.js 4.02 kB
build/viewport/index.min.js 965 B
build/vips/index.min.js 36.2 kB
build/warning/index.min.js 250 B
build/widgets/index.min.js 7.16 kB
build/widgets/style-rtl.css 1.16 kB
build/widgets/style.css 1.16 kB
build/wordcount/index.min.js 1.03 kB

compressed-size-action

@gziolo gziolo added the [Status] In Progress Tracking issues with work in progress label Oct 25, 2024
@gziolo gziolo force-pushed the update/wp-scripts-detect-default-entry-points branch from 30223c6 to 068e305 Compare November 5, 2024 17:00
@gziolo gziolo force-pushed the update/wp-scripts-detect-default-entry-points branch from 2c1d41c to 8c25bad Compare November 10, 2024 09:59
@gziolo gziolo marked this pull request as ready for review November 10, 2024 10:48
@gziolo gziolo removed the [Status] In Progress Tracking issues with work in progress label Nov 10, 2024
Copy link

github-actions bot commented Nov 10, 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.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @xsonic, @james0r, @wasek23, @ghostintranslation, @daotw.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

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

Unlinked contributors: xsonic, james0r, wasek23, ghostintranslation, daotw.

Co-authored-by: gziolo <[email protected]>
Co-authored-by: sirreal <[email protected]>
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: bacoords <[email protected]>
Co-authored-by: jdamner <[email protected]>
Co-authored-by: t-hamano <[email protected]>
Co-authored-by: aaronware <[email protected]>
Co-authored-by: theaminuli <[email protected]>
Co-authored-by: mrleemon <[email protected]>

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

@gziolo gziolo requested a review from ndiego November 10, 2024 14:49
@gziolo gziolo changed the title Scripts: Automatically detect entry points for the default JS and CSS files WP Scripts: Automatically detect entry points for the default JS and CSS files Nov 10, 2024
@gziolo
Copy link
Member Author

gziolo commented Nov 10, 2024

I included the changelog entry with 7ce5696. I marked changes as breaking as it's very hard to predict whether all existing projects will work precisely the same. However, I put as much care as I could into understanding the implications to minimize the risk.

@youknowriad
Copy link
Contributor

Sorry if my question is out of line, I kind of lost track about how all these tools work over time. But why does wp-scripts build work on a "directory" as entry point instead of a file (most recent bundlers use files as entry points). There are probably valid reason, I guess some convention that we set up a long time ago.

So here's a proposal that may or may not be valid depending on the answer to the question above but what if we change that and require an entry point to the build command:

wp-scripts build src/index.js
wp-scripts build assets/style.css
wp-scripts build blocks/block.json

What use-cases we would be missing in this case?

@gziolo
Copy link
Member Author

gziolo commented Nov 14, 2024

@youknowriad, great questions. I must admit that given how long the WP Scripts tool is around, I also don’t fully remember which conventions we established in which order and when we over optimized for plugins with blocks 😅

What’s interesting webpack 4 supported the syntax you mentioned:

wp-scripts build index.js

That’s why we added a compatibility layer on top of webpack 5 to retain the functionality. Unfortunately, it doesn’t work with CSS or block metadata. What’s interesting, when you pass as many paths to files as you want they are used as entry points and we don’t use any automatic discovery for anything else. What would be compatible with your perspective is extending this format to support more asset types. Example:

wp-scripts build index.ts style.scss **/block.json

This is more or less the equivalent of the default behavior that I was trying to cover with the changes proposed. However, I’m happy to explore the alternative where you can pass paths to JS, CSS and block metadata explicitly of with globs that are further processed by the bundler and converted into entry points based on existing rules. I omitted src from file paths as it’s already the default directory for project files which can be changed with a CLI argument.

The most complex setup I have seen we would need to potentially cover is multiple plugins with multiple blocks, and multiple themes in one project. That’s rather something that requires a custom configuration, but the approach you shared would allow a lot of flexibility in collecting all necessary entry points by listing files in the command.

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.

This looks good, I left a couple notes, specifically I'd avoid supporting extensions lik .mjs right now as that would be a nice quick way to support modules.

packages/scripts/config/webpack.config.js Show resolved Hide resolved
packages/scripts/utils/config.js Show resolved Hide resolved
@youknowriad
Copy link
Contributor

That’s why we added a compatibility layer on top of webpack 5 to retain the functionality. Unfortunately, it doesn’t work with CSS or block metadata. What’s interesting, when you pass as many paths to files as you want they are used as entry points and we don’t use any automatic discovery for anything else. What would be compatible with your perspective is extending this format to support more asset types.

Yes, I think having a compatibility layer is fine as for me webpack is just an implementation details. I've never liked the fact that you can pass your webpack config and would prefer if we remove it entirely at some point but I guess it's ok to keep for now as it doesn't prevent us from exploring the possibility to pass any random entry point.

I'd love to avoid any --webpack-* something config.

@gziolo
Copy link
Member Author

gziolo commented Nov 15, 2024

@youknowriad, I'd like to ensure we have some shared understanding of the general direction. We should aim towards making the wp-scripts build configurable through the CLI params that with time become entirely independent from webpack allowing devs to define as many JS and CSS files as necessary as entry points. In addition to that, they should be able to pass paths to block.json files that would copy these files to the build folder, and JS and CSS files listed in metadata as paths should also get detected as entry points. CLI params that allow defining the source folder or the target build folder should also become completely independent from how webpack works.

A more comprehensive example where we could go:

wp-scripts build index.ts editor.js style.scss **/block.json --source-path assets --output-path public

How do you feel about the changes included in this PR? Should we leave it as and land it despite the minimal risk that some existing projects extending the default webpack config could be impacted when using the wp-scripts build and might see two new entry points in the output directory if they used the index.js and style.css in the source directory?

@youknowriad
Copy link
Contributor

wp-scripts build index.ts editor.js style.scss **/block.json --source-path assets --output-path public

In this example, I don't know why we need "source-path" at all? But yes, your summary makes a lot of sense to me.

How do you feel about the changes included in this PR? Should we leave it as and land it despite the minimal risk that some existing projects extending the default webpack config could be impacted when using the wp-scripts build and might see two new entry points in the output directory if they used the index.js and style.css in the source directory?

For me, if we strive to solve the former (the example you shared above), the current PR might be largely unnecessary. That said, if you feel like it's a good temporary solution, I don't mind it knowing that people will have to change later.

For the breaking changes, I think it's ok as long as the upgrade path is clear.

@gziolo
Copy link
Member Author

gziolo commented Nov 15, 2024

Excellent feedback, @youknowriad. Much appreciated. In the next few days, I will try to collect more feedback from folks actually using WP Scripts before deciding whether land these changes as the intermediate step or whether to go straight into the direction discussed above which will require more work.

In this example, I don't know why we need "source-path" at all?

We made some assumptions on how the project is structured in the past when using wp-scripts build (no file paths passed) on on the default Create Block template:

  • Source code lives in src directory.
  • Build folder lands in build directory.

These CLI args offer a way to change these defaults. However, after checking the variant when you explicitly pass the paths to JS files it indeed isn't necessary to provide the source folder. The only downside is
when the dev has the following command:

wp-scripts build src/assets/js/index.js

In that case, the build file would be put inside build/src/assets/js/index.js.

@sirreal
Copy link
Member

sirreal commented Nov 18, 2024

I agree with the direction that's described in the last few comments here.

I find abstraction over webpack is awkward in this tool, the implementation details related to webpack leak through in many places, yet webpack isn't fully exposed either. This makes it difficult to iterate and experiment, for example with script modules where non-webpack-based tooling may provide better alternatives.

One question I do have is regarding script modules. Right now, entrypoints can be passed as positional arguments and the tool knows how to do the right thing. This works with classic scripts, but there's no way to pass a JavaScript entrypoint that should be compiled as a script module. I don't have a great solution, I think we'll need to support an additional named argument that adds script module entrypoints. I hinted at using .mjs-like extensions, but I don't think that's the best solution and the extension will have other implications. We don't need to have that sorted out in this PR but it's something to keep in mind. @ryanwelcher brought this up recently in Core Slack.

@gziolo
Copy link
Member Author

gziolo commented Nov 18, 2024

I was looking at usage examples of build and start through the lens of removing the dependency on webpack referenced. These what is included in the docs:

{
	"scripts": {
		"build": "wp-scripts build",
		"build:custom": "wp-scripts build entry-one.js entry-two.js --output-path=custom",
		"build:copy-php": "wp-scripts build --webpack-copy-php",
		"build:custom-directory": "wp-scripts build --webpack-src-dir=custom-directory"
	}
}

What I found interesting is that the direction discussed is fully backward compatible, and it offers a unified interface that could also work with PHP files. In effect, build:copy-php could get refactored and when paired with improved build:custom-directory we could have something like:

{
	"scripts": {
		"build": "wp-scripts build index.js **/*.php --source-path=src --output-path=custom",
	}
}

@sirreal, I don't have a formed vision for improved script module support. It's definitely something that we have to explore as part of these efforts. The only way we can precisely assume script modules today is through viewScriptModule detected in the block.json file. The file extension like *.mjs could be another indicator. Defining type: module in the package.json file, could be another way to tell the bundler to treat the passed JavaScript entry points differently.

I find abstraction over webpack is awkward in this tool, the implementation details related to webpack leak through in many places, yet webpack isn't fully exposed either. This makes it difficult to iterate and experiment, for example with script modules where non-webpack-based tooling may provide better alternatives.

Yes, we should operate with the assumption that webpack is going to be replaced with Rspack, Vite, or something else in the near future.

@youknowriad
Copy link
Contributor

For scripts modules, we should also check how the bundlers do it. Most of them offer a --format argument or something like that to change the format of the output.

@gziolo
Copy link
Member Author

gziolo commented Nov 21, 2024

I talked to @ndiego and @justintadlock about the idea proposed by @youknowriad in #66466 (comment). They shared examples of custom webpack configs they used in projects. I validated how much the new CLI syntax could remove the need to deal with webpack internals, and I got very positively surprised how quickly I could map it to the idea discussed. Here is a summary.

Blog post example

This is how the revised command could look for the config overrides included in the blog post Getting and setting Block Binding values in the Editor.

npx wp-scripts build js/editor=./resources/js/editor.js --output-path=public

I was also surprised to learn that I could run the command successfully with the current version of WP Scripts.

Note: I tried also the following npx wp-scripts build editor.js --webpack-src-dir=resources/js --output-path=public/js. However, --webpack-src-dir isn't integrated with the syntax for file paths, they are all relative to the current working directory.

Social Sharing block example

For the Social Sharing block, the command in the future would look as follows:

npx wp-scripts build ./src/*/block.json ./src/*/utils.php

Note: All paths by default would get resolved from the current working directory, so src needs to be explicitly passed to avoid the case where they end up inside build/src/*.

As of today, the following should work, too:

npx wp-scripts build --webpack-copy-php

Note: It would ensure all PHP files get copied from src to build directory. Both are considered default settings today in the default webpack config when no files are listed. block.json files get automatically detected and related JS set as entry points.

Block Visibility plugin

Another config is for Block Visibility. In the future, it could be handled with the following:

npx wp-scripts build block-visibility-editor=./src/editor.js block-visibility-setting=./src/settings.js block-visibility-editor-styles=./src/styles/editor.scss block-visibility-contextual-indicator-styles=./src/styles/contextual-indicators.scss block-visibility-setting-styles=./src/styles/settings.scss

The custom config is required today, because there is no support for CSS as entry points. This PR adds this concept. I'm not entirely sure how the file path syntax fits here. It might even work out of the box for the syntax provided above. It won't work without small tweaks for a simplified version like npx wp-scripts build ./editor.css because it assumes the extension is .js.

X3P0: Ideas theme

A more real-world example of what is necessary to do with themes. Here’s the webpack.config.js. This one is very complex as it also handles fonts and media. Something like that might work one day:

npm run build css/blocks/*/*=./resources/scss/blocks/*/*.scss js/editor=./resources/js/editor.js css/*=./resources/scss/*.scss ./resources/{media,fonts}/*.*

Important note: I don’t know how far we can go with these globs. The challenge is mostly for the part that maps the paths to entry points.

@gziolo gziolo marked this pull request as draft November 21, 2024 12:13
@gziolo
Copy link
Member Author

gziolo commented Nov 21, 2024

I decided to switch this PR to the draft state for now. Based on my most recent comment, I will work on smaller tasks to improve the existing CLI command instead of trying to improve the automatic detection for entry points.

@youknowriad
Copy link
Contributor

I would love to understand more why you had to use source-path argument as ideally we shouldn't need it.

@gziolo
Copy link
Member Author

gziolo commented Nov 21, 2024

I would love to understand more why you had to use source-path argument as ideally we shouldn't need it.

Yes, it's optional. I used it to make the paths shorter 😅 I updated my comment to address your concern. We shouldn't need to promote it so heavily as it might be confusing. Still, it might be a handful option in some cases.

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] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scripts: Enhance the way entry points are detected in projects consisting of blocks and editor plugins
3 participants