Skip to content

Compile Blocks when passing block.json to Webpack entry. #65602

Description

@michaelbragg

What problem does this address?

I’m not sure if this is a bug, a feature request, or if it would contribute towards improved documentation.

We aim to compile our Gutenberg Blocks by declaring them inside a root webpack.config.js file instead of having multiple scripts for each block within the package.json.

Background: 


We are an agency that builds complete websites. The project repositories will include WP Core, 1st Party Theme, and multiple 1st Party Plugins. We must declare 20-30 individual scripts to compile each of our custom blocks, slots, variations, CSS, JS, etc. 
Our ideal solution would be to move these declarations outside the package.json file and into the Webpack config.


I’ve created a simplified version of our setup: https://github.com/michaelbragg/wp-webpack-config


Currently, the above code will compile


./public/wp-content/plugins/plugin-example/build/example-one
└ block.json
./public/wp-content/plugins/plugin-example/build/example-two
└ block.json

My understanding is that when --webpack-src-dir is passed to the wp-scripts command, it looks for directories containing a block.json and used this to compile the required files.
Is this functionality missing when a block.json file is passed to the entry within a Webpack config?

What is your proposed solution?

module.exports = [
	{
		…defaultConfig,
		name: "Example Plugin",
		entry: {
			"example-one": "./public/wp-content/plugins/plugin-example/src/Domain/Blocks/example-one/block.json",

			"example-two": "./public/wp-content/plugins/plugin-example/src/Domain/Blocks/example-two/block.json",
   			"slotfill": "./public/wp-content/plugins/plugin-example/src/Domain/Slots/slotfill",

 		  …
	},
	output: {
  		...defaultConfig.output,
  		path.resolve( __dirname, "./public/wp-content/plugins/plugin-example/build/" )
	},
…

];

Activity

  1. gziolo commented on Oct 7, 2024

    @gziolo
    Member

    The way the problem is described looks very similar to the existing issue:

    Is this functionality missing when a block.json file is passed to the entry within a Webpack config?

    It is a bit more nuanced. The entry points are for JavaScript/TypeScript files. I’m not entirely sure what happens when you pass the path to JSON file but if it is supported it would rather load the contents if of the file to output it as a JavaScript object.

  2. added
    [Status] DuplicateUsed to indicate that a current issue matches an existing one and can be closed
    on Oct 7, 2024
  3. michaelbragg commented on Nov 12, 2024

    @michaelbragg
    Author

    Thanks, @gziolo. I'm not sure this is exactly a duplicate of #55936, as I am actively looking to use a custom Webpack Config.

    I did more exploring into my issues and came across getWebPackEntryPoints() and thought this could be a solutions. This is the method that converts the --webpack-src-dir into the root directory to look for the block.json files.

    If getWebPackEntryPoints() was modified to accepts a parameter, This could be used to set the WP Src Directory for each Webpack object eg,

    module.exports = [
    	{
    		…defaultConfig,
    		name: "Example Plugin",
    		entry: {
    			...getWebpackEntryPoints( './public/wp-content/plugins/plugin-example/src/Domain/Blocks/' ),
       			"slotfill": "./public/wp-content/plugins/plugin-example/src/Domain/Slots/slotfill",

     		  …
    	},
    	output: {
      		...defaultConfig.output,
      		path.resolve( __dirname, "./public/wp-content/plugins/plugin-example/build/" )
    	},
    …

    ];

    Removing the need to pass --webpack-src-dir in the NPM script. At the same time, letting us have a different entry point per Webpack object.

    I would be interested to hear your thoughts on whether this solution could be viable.

  4. gziolo commented on Nov 13, 2024

    @gziolo
    Member

    I spent more time exploring the repository shared in the description and the config file in particular: https://github.com/michaelbragg/wp-webpack-config/blob/main/webpack.config.js

    One note is that every block.json file should get automatically copied from the source folder to the build folder when using the default config. As part of the process, also all JS files defined in block.json files should get detected as entry points. The part that is missing today is the ability to detect shared JavaScript and CSS files. I have a proposal to improve that, but your use case is more complex:

  5. self-assigned this
    on Nov 13, 2024
  6. added and removed
    [Status] DuplicateUsed to indicate that a current issue matches an existing one and can be closed
    on Nov 13, 2024
  7. removed their assignment
    on Aug 27, 2026
  8. youknowriad commented on Oct 15, 2026

    @youknowriad
    Contributor

    Thanks for the detailed example repo! This is the same area as #55936, which was closed as not planned. @wordpress/scripts is moving toward the more opinionated @wordpress/build (see #72032). For highly customized multi-plugin webpack setups, the recommendation is to pass JS entry points directly (#68251) or use your own tooling. Closing for that reason. If something specific is missing in the new build tooling, a focused issue against #72032 would be welcome.

  9. youknowriad commented on Oct 15, 2026

    @youknowriad
    Contributor

    The successor of this issue is #74542 basically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions