admin管理员组

文章数量:1122826

I regularly use @wordpress/scripts for building both blocks and non-block components. Recently, I started working in a new plugin that registers both some blocks and non-block components that add additional panels using the registerPlugin() function. This is the structure of my /src directory:

/src
    /blocks
        /my-first-block
            block.json
            ...
        /my-second-block
            block.json
            ...
        /my-third-block 
            block.json
            ...
    /components
        /my-first-panel
            index.js
        /my-second-panel
            index.js
    index.js
    style.css

This is the content of the root index.js file:

import { registerPlugin } from '@wordpress/plugins';

import FirstPanelOptionsPanelComponent from './components/my-first-panel';
import SecondPanelOptionsPanelComponent from './components/my-second-panel';
import './style.scss';

registerPlugin('first-panel-options-panel', {
    render: FirstPanelOptionsPanelComponent,
});

registerPlugin('second-panel-options-panel', {
    render: SecondPanelOptionsPanelComponent,
});

When you run the wp-scripts build script from the @wordpress/scripts package, it only builds the blocks located in the /src directory. However, if you specifically run wp-scripts build src/index.js, it builds the components but exclude the blocks.

Is there a way to build BOTH the blocks and the non-block components at once?

I regularly use @wordpress/scripts for building both blocks and non-block components. Recently, I started working in a new plugin that registers both some blocks and non-block components that add additional panels using the registerPlugin() function. This is the structure of my /src directory:

/src
    /blocks
        /my-first-block
            block.json
            ...
        /my-second-block
            block.json
            ...
        /my-third-block 
            block.json
            ...
    /components
        /my-first-panel
            index.js
        /my-second-panel
            index.js
    index.js
    style.css

This is the content of the root index.js file:

import { registerPlugin } from '@wordpress/plugins';

import FirstPanelOptionsPanelComponent from './components/my-first-panel';
import SecondPanelOptionsPanelComponent from './components/my-second-panel';
import './style.scss';

registerPlugin('first-panel-options-panel', {
    render: FirstPanelOptionsPanelComponent,
});

registerPlugin('second-panel-options-panel', {
    render: SecondPanelOptionsPanelComponent,
});

When you run the wp-scripts build script from the @wordpress/scripts package, it only builds the blocks located in the /src directory. However, if you specifically run wp-scripts build src/index.js, it builds the components but exclude the blocks.

Is there a way to build BOTH the blocks and the non-block components at once?

Share Improve this question edited Nov 16, 2023 at 11:05 leemon asked Nov 16, 2023 at 10:53 leemonleemon 2,0024 gold badges22 silver badges51 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 4

I'm pasting the answer I got from a developer in the Gutenberg repository.

Just add a webpack.config.js file at the root of your project with the following contents:

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

module.exports = {
    ...defaultConfig,
    entry: {
        ...defaultConfig.entry(),
        index: './src/index.js',
    },
};

This will build all the blocks in /src/blocks/ folder and also compile the /src/index.js file.

本文标签: