I assume the structure of gutenberg block when using npm init @wordpress/block my-block
to init your blockinit your block.
You have to register your own client side-side script in my-block.php
to be executed on front-end; one drawback: it is loaded on every page.
function create_block_my_block_init()
{
$client_js = 'build/client.js';
wp_register_script(
'create-block-my-block-block'client',
plugins_url($client_js, __FILE__),
$script_asset['dependencies'],
$script_asset['version']
);
$client_css = 'build/client.css';
wp_register_style(
'create-block-my-block-client',
plugins_url($client_css, __FILE__),
array(),
filemtime("$dir/$client_css")
);
...
register_block_type('create-block/my-block', array(
...
'script' => 'create-block-my-block-block'client',
'style' => 'create-block-my-block-client', // Override default and import custom scss file.
));
}
Note that you can't use build/style-index.css
/ style.scss
anymore or use register_block_style
for that purpose. But better import your custom style via import './custom.scss';
in your React component (here: MyComponent
).
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
module.exports = {
...defaultConfig,
entry: {
...defaultConfig.entry,
client: './src/client.js',
},
module: {
...defaultConfig.module,
rules: [
...defaultConfig.module.rules,
{
test: /\.(scss|css)$/,
use: ['style-loader', 'css-loader'],
},
]
},
};
See my full example here.